# Chapter 13. Pointers and Linked Lists

Save this PDF as:

Size: px
Start display at page:

## Transcription

1 Chapter 13 Pointers and Linked Lists

2 Overview 13.1 Nodes and Linked Lists 13.2 Stacks and Queues Slide 13-2

3 13.1 Nodes and Linked Lists

4 Nodes and Linked Lists n A linked list is a list that can grow and shrink while the program is running n A linked list is constructed using pointers n A linked list often consists of structs or classes that contain a pointer variable connecting them to other dynamic variables n A linked list can be visualized as items, drawn as boxes, connected to other items by arrows head end Slide 13-4

5 Nodes n The boxes in the previous drawing represent the nodes of a linked list n Nodes contain the data item(s) and a pointer that can point to another node of the same type n The pointers point to the entire node, not an individual item that might be in the node n The arrows in the drawing represent pointers Display 13.1 Slide 13-5

6 Implementing Nodes n Nodes are implemented in C++ as structs or classes n Example: A structure to store two data items and a pointer to another node of the same type, along with a type definition might be: struct ListNode { string item; int count; ListNode *link; }; This circular definition is allowed in C++ typedef ListNode* ListNodePtr; Slide 13-6

7 The head of a List n The box labeled head, in display 13.1, is not a node, but a pointer variable that points to a node n Pointer variable head is declared as: ListNodePtr head; Slide 13-7

8 Accessing Items in a Node n Using the diagram of 13.1, this is one way to change the number in the first node from 10 to 12: (*head).count = 12; n head is a pointer variable so *head is the node that head points to n The parentheses are necessary because the dot operator. has higher precedence than the dereference operator * Slide 13-8

9 The Arrow Operator n The arrow operator -> combines the actions of the dereferencing operator * and the dot operator to specify a member of a struct or object pointed to by a pointer n (*head).count = 12; can be written as head->count = 12; n The arrow operator is more commonly used Display 13.2 Slide 13-9

10 NULL n The defined constant NULL is used as n An end marker for a linked list n A program can step through a list of nodes by following the pointers, but when it finds a node containing NULL, it knows it has come to the end of the list n The value of a pointer that has nothing to point to n The value of NULL is 0 n Any pointer can be assigned the value NULL: double* there = NULL; Slide 13-10

11 To Use NULL n A definition of NULL is found in several libraries, including <iostream> and <cstddef> n A using directive is not needed for NULL Slide 13-11

12 Linked Lists n The diagram in Display 13.2 depicts a linked list n A linked list is a list of nodes in which each node has a member variable that is a pointer that points to the next node in the list n The first node is called the head n The pointer variable head, points to the first node n The pointer named head is not the head of the list it points to the head of the list n The last node contains a pointer set to NULL Slide 13-12

13 Building a Linked List: The node definition n Let's begin with a simple node definition: struct Node { int data; Node *link; }; typedef Node* NodePtr; Slide 13-13

14 Building a Linked List: Declaring Pointer Variable head n With the node defined and a type definition to make or code easier to understand, we can declare the pointer variable head: NodePtr head; n head is a pointer variable that will point to the head node when the node is created Slide 13-14

15 Building a Linked List: Creating the First Node n To create the first node, the operator new is used to create a new dynamic variable: head = new Node; n Now head points to the first, and only, node in the list Slide 13-15

16 Building a Linked List: Initializing the Node n Now that head points to a node, we need to give values to the member variables of the node: head->data = 3; head->link = NULL; n Since this node is the last node, the link is set to NULL Slide 13-16

17 Function head_insert n It would be better to create a function to insert nodes at the head of a list, such as: n void head_insert(nodeptr& head, int the_number); n n The first parameter is a NodePtr parameter that points to the first node in the linked list The second parameter is the number to store in the list n head_insert will create a new node for the number n n The number will be copied to the new node The new node will be inserted in the list as the new head node Slide 13-17

18 Pseudocode for head_insert n Create a new dynamic variable pointed to by temp_ptr n Place the data in the new node called *temp_ptr n Make temp_ptr's link variable point to the head node n Make the head pointer point to temp_ptr Display 13.3 Slide 13-18

19 Translating head_insert to C++ n The pseudocode for head_insert can be written in C++ using these lines in place of the lines of pseudocode: n NodePtr temp_ptr; //create the temporary pointer temp_ptr = new Node; // create the new node n temp_ptr->data = the_number; //copy the number n temp_ptr->link = head; //new node points to first node head = temp_ptr; // head points to new // first node Display 13.4 Slide 13-19

20 An Empty List n A list with nothing in it is called an empty list n An empty linked list has no head node n The head pointer of an empty list is NULL head = NULL; n Any functions written to manipulate a linked list should check to see if it works on the empty list Slide 13-20

21 Losing Nodes n You might be tempted to write head_insert using the head pointer to construct the new node: head = new Node; head->data = the_number; n Now to attach the new node to the list n The node that head used to point to is now lost! Display 13.5 Slide 13-21

22 Memory Leaks n Nodes that are lost by assigning their pointers a new address are not accessible any longer n The program has no way to refer to the nodes and cannot delete them to return their memory to the freestore n Programs that lose nodes have a memory leak n Significant memory leaks can cause system crashes Slide 13-22

23 Searching a Linked List n To design a function that will locate a particular node in a linked list: n We want the function to return a pointer to the node so we can use the data if we find it, else return NULL n The linked list is one argument to the function n The data we wish to find is the other argument n This declaration will work: NodePtr search(nodeptr head, int target); Slide 13-23

24 Function search n Refining our function n We will use a local pointer variable, named here, to move through the list checking for the target n The only way to move around a linked list is to follow pointers n We will start with here pointing to the first node and move the pointer from node to node following the pointer out of each node Display 13.6 Slide 13-24

25 Pseudocode for search n n n Make pointer variable here point to the head node while(here does not point to a node containing target AND here does not point to the last node) { make here point to the next node } If (here points to a node containing the target) return here; else return NULL; Slide 13-25

26 Moving Through the List n The pseudocode for search requires that pointer here step through the list n How does here follow the pointers from node to node? n When here points to a node, here->link is the address of the next node n To make here point to the next node, make the assignment: here = here->link; Slide 13-26

27 A Refinement of search n The search function can be refined in this way: here = head; while(here->data!= target && here->link!= NULL) { here = here->next; } Check for last node if (here->data = = target) return here; else return NULL; Slide 13-27

28 Searching an Empty List n Our search algorithm has a problem n If the list is empty, here equals NULL before the while loop so n here->data is undefined n here->link is undefined n The empty list requires a special case in our search function n A refined search function that handles an empty list is shown in Display 13.7 Slide 13-28

29 Pointers as Iterators n n An iterator is a construct that allows you to cycle through the data items in a data structure to perform an action on each item n An iterator can be an object of an iterator class, an array index, or simply a pointer A general outline using a pointer as an iterator: Node_Type *iter; for (iter = Head; iter!= NULL; iter = iter->link) //perform the action on the node iter points to n Head is a pointer to the head node of the list Slide 13-29

30 Iterator Example n Using the previous outline of an iterator we can display the contents of a linked list in this way: NodePtr iter; for (iter = Head; iter!= NULL; iter = iter->link) cout << (iter->data); Slide 13-30

31 Inserting a Node Inside a List n To insert a node after a specified node in the linked list: n n Use another function to obtain a pointer to the node after which the new node will be inserted n Call the pointer after_me Use function insert, declared here to insert the node: void insert(nodeptr after_me, int the_number); Display 13.8 Slide 13-31

32 Inserting the New Node n Function insert creates the new node just as head_insert did n We do not want our new node at the head of the list however, so n We use the pointer after_me to insert the new node Slide 13-32

33 Inserting the New Node n This code will accomplish the insertion of the new node, pointed to by temp_ptr, after the node pointed to by after_me: temp_ptr->link = after_me->link; after_me->link = temp_ptr; head after_me 5 2 temp_ptr Slide 13-33

34 Caution! n The order of pointer assignments is critical n If we changed after_me->link to point to temp_ptr first, we would loose the rest of the list! n The complete insert function is shown in Display 13.9 Slide 13-34

35 Function insert Again n Notice that inserting into a linked list requires that you only change two pointers n This is true regardless of the length of the list n Using an array for the list would involve copying as many as all of the array elements to new locations to make room for the new item n Inserting into a linked list is often more efficient than inserting into an array Slide 13-35

36 Removing a Node n To remove a node from a linked list n Position a pointer, before, to point at the node prior to the node to remove n Position a pointer, discard, to point at the node to remove n Perform: before->link = discard->link; n The node is removed from the list, but is still in memory n Return *discard to the freestore: delete discard; Display Slide 13-36

37 Assignment With Pointers n If head1 and head2 are pointer variables and head1 points to the head node of a list: head2 = head1; causes head2 and head1 to point to the same list n There is only one list! n If you want head2 to point to a separate copy, you must copy the list node by node or overload the assignment operator appropriately Slide 13-37

38 Variations on Linked Lists n n Many other data structures can be constructed using nodes and pointers Doubly-Linked List n n Each node has two links, one to the next node and one to the previous node Allows easy traversal of the list in both directions struct Node { int data; Node *forward_link; Node *back_link; }; Display Slide 13-38

39 Binary Tree n A tree is a data structure that looks like an upside-down tree with the root at the top n No cycles n In a binary tree each node has at most two links struct TreeNode { int data; TreeNode *left_link; TreeNode *right_link; }; Display Slide 13-39

40 Linked List of Classes n The preceding examples created linked lists of structs. We can also create linked lists using classes. n Logic to use a class is identical except the syntax of using and defining a class should be substituted in place of that for a struct n Interface and Definition for a Node Class Display Display (1-2) Slide 13-40

41 Program using the Node class n We can create a linked list of numbers using the Node class. Display (1-3) Slide 13-41

42 Section 13.1 Conclusion n Can you n Write type definitions for the nodes and pointers in a linked list? Call the node type NodeType and call the pointer type PointerType. The linked lists will be lists of letters. n Explain why inserting into an array can be less efficient than inserting into a linked list? Slide 13-42

43 13.2 Stacks and Queues

44 A Linked List Application n A stack is a data structure that retrieves data in the reverse order the data was stored n If 'A', 'B', and then 'C' are placed in a stack, they will be removed in the order 'C', 'B', and then 'A' n A stack is a last-in/first-out data structure like the stack of plates in a cafeteria; adding a plate pushes down the stack and the top plate is the first one removed Display Slide 13-44

45 Program Example: A Stack Class n We will create a stack class to store characters n Adding an item to a stack is pushing onto the stack n Member function push will perform this task n Removing an item from the stack is popping the the item off the stack n Member function pop will perform this task n Display contains the stack class interface Slide 13-45

46 Using the stack Class n Display (1-2) stack class demonstrates the use of the Slide 13-46

47 Function push n The push function adds an item to the stack n It uses a parameter of the type stored in the stack void push(char the_symbol); n n Pushing an item onto the stack is precisely the same task accomplished by function head_insert of the linked list For a stack, a pointer named top is used instead of a pointer named head Slide 13-47

48 Function pop n The pop function returns the item that was at the top of the stack char pop( ); n n n Before popping an item from a stack, pop checks that the stack is not empty pop stores the top item in a local variable result, and the item is "popped" by: top = top->link; n A temporary pointer must point to the old top item so it can be "deleted" to prevent a memory leak pop then returns variable result Slide 13-48

49 Empty Stack n An empty stack is identified by setting the top pointer to NULL top = NULL; Slide 13-49

50 The Copy Constructor n Because the stack class uses a pointer and creates new nodes using new, a copy constructor is needed n The copy constructor (a self-test exercise) must make a copy of each item in the stack and store the copies in a new stack n Items in the new stack must be in the same position in the stack as in the original Slide 13-50

51 The stack destructor n Because function pop calls delete each time an item is popped off the stack, ~stack only needs to call pop until the stack is empty char next; while(! empty ( ) ) { next = pop( ); } Slide 13-51

52 stack Class Implementation n The stack class implementation is found in Display (1) Display (2) Slide 13-52

53 A Queue n A queue is a data structure that retrieves data in the same order the data was stored n If 'A', 'B', and then 'C' are placed in a queue, they will be removed in the order A', 'B', and then C' n A queue is a first-in/first-out data structure like the checkout line in a supermarket Display Slide 13-53

54 Queue Class Implementation n The queue class is implemented in a manner similar to the stack except there are two pointers, one to track the front of the list, and one to track the back of the list n Interface: Display (1-2) n Program using the Queue Class: Display (1-2) n Implementation: Display (1-3) Slide 13-54

55 Section 13.2 Conclusion n Can you n Give the definition of member function push? Create a definition for the stack class copy constructor? n Know when to use a queue vs. a stack? n Create a definition for the queue class copy constructor? Slide 13-55

56 Chapter End Slide 13-56

57 Display 13.1 Back Next Slide 13-57

58 Display 13.2 Back Next Slide 13-58

59 Display 13.3 Back Next Slide 13-59

60 Display 13.4 Back Next Slide 13-60

61 Display 13.5 Back Next Slide 13-61

62 Display 13.6 Back Next Slide 13-62

63 Display 13.7 Back Next Slide 13-63

64 Display 13.8 Back Next Slide 13-64

65 Display 13.9 Back Next Slide 13-65

66 Display Back Next Slide 13-66

67 Display Back Next Slide 13-67

68 Display Back Next Slide 13-68

69 Display Back Next Slide 13-69

70 Display (1/2) Back Next Slide 13-70

71 Display (2/2) Back Next Slide 13-71

72 Display (1/3) Back Next Slide 13-72

73 Display (2/3) Back Next Slide 13-73

74 Display (3/3) Back Next Slide 13-74

75 Display Back Next Slide 13-75

76 Display Back Next Slide 13-76

77 Display (1/2) Back Next Slide 13-77

78 Display (2/2) Back Next Slide 13-78

79 Display (1/2) Back Next Slide 13-79

80 Display (2/2) Back Next Slide 13-80

81 Display Back Next Slide 13-81

82 Display (1/2) Back Next Slide 13-82

83 Display (2/2) Back Next Slide 13-83

84 Display (1/2) Back Next Slide 13-84

85 Display (2/2) Back Next Slide 13-85

86 Display (1/3) Back Next Slide 13-86

87 Display (2/3) Back Next Slide 13-87

88 Display (3/3) Back Next Slide 13-88

15 Pointers and Linked Lists 15.1 Nodes and Linked Lists 828 Nodes 828 Linked Lists 834 Inserting a Node at the Head of a List 835 Pitfall:Losing Nodes 839 Searching a Linked List 840 Pointers as Iterators

Linked Lists Ch 17 (Gaddis) Introduction to Linked Lists A data structure representing a A series of nodes chained together in sequence CS 3358 Summer I 2012 Jill Seaman - Each node points to one other

### MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push) -----------------------------------------

=============================================================================================================================== DATA STRUCTURE PSEUDO-CODE EXAMPLES (c) Mubashir N. Mir - www.mubashirnabi.com

### 7.1 Our Current Model

Chapter 7 The Stack In this chapter we examine what is arguably the most important abstract data type in computer science, the stack. We will see that the stack ADT and its implementation are very simple.

Linked Lists Linked Lists, Queues, and Stacks CSE 10: Introduction to C Programming Fall 200 Dynamic data structure Size is not fixed at compile time Each element of a linked list: holds a value points

### Data Structures Using C++ 2E. Chapter 5 Linked Lists

Data Structures Using C++ 2E Chapter 5 Linked Lists Doubly Linked Lists Traversed in either direction Typical operations Initialize the list Destroy the list Determine if list empty Search list for a given

### DATA STRUCTURES USING C

DATA STRUCTURES USING C QUESTION BANK UNIT I 1. Define data. 2. Define Entity. 3. Define information. 4. Define Array. 5. Define data structure. 6. Give any two applications of data structures. 7. Give

### KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

KITES TECHNOLOGY 360 Degree Solution www.kitestechnology.com/academy.php info@kitestechnology.com technologykites@gmail.com Contact: - 8961334776 9433759247 9830639522.NET JAVA WEB DESIGN PHP SQL, PL/SQL

### Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Linked Lists, Stacks, Queues, Deques It s time for a chainge! Learning Goals After this unit, you should be able to... Differentiate an abstraction from an implementation. Define and give examples of problems

### Data Structures Using C++ 2E. Chapter 5 Linked Lists

Data Structures Using C++ 2E Chapter 5 Linked Lists Test #1 Next Thursday During Class Cover through (near?) end of Chapter 5 Objectives Learn about linked lists Become aware of the basic properties of

### Comp 245 Data Structures. Stacks

Comp 245 Data Structures Stacks What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO random access to other elements within the stack An

### Output: 12 18 30 72 90 87. struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

50 20 70 10 30 69 90 14 35 68 85 98 16 22 60 34 (c) Execute the algorithm shown below using the tree shown above. Show the exact output produced by the algorithm. Assume that the initial call is: prob3(root)

### UNIVERSITI MALAYSIA SARAWAK KOTA SAMARAHAN SARAWAK PSD2023 ALGORITHM & DATA STRUCTURE

STUDENT IDENTIFICATION NO UNIVERSITI MALAYSIA SARAWAK 94300 KOTA SAMARAHAN SARAWAK FAKULTI SAINS KOMPUTER & TEKNOLOGI MAKLUMAT (Faculty of Computer Science & Information Technology) Diploma in Multimedia

### Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Definition Course: Programming II - Abstract Data Types The ADT Stack The ADT Stack is a linear sequence of an arbitrary number of items, together with access procedures. The access procedures permit insertions

### Curriculum Map. Discipline: Computer Science Course: C++

Curriculum Map Discipline: Computer Science Course: C++ August/September: How can computer programs make problem solving easier and more efficient? In what order does a computer execute the lines of code

### 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Answer the following 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ 2) Which data structure is needed to convert infix notations to postfix notations? Stack 3) The

LINKED DATA STRUCTURES 1 Linked Lists A linked list is a structure in which objects refer to the same kind of object, and where: the objects, called nodes, are linked in a linear sequence. we keep a reference

### Glossary of Object Oriented Terms

Appendix E Glossary of Object Oriented Terms abstract class: A class primarily intended to define an instance, but can not be instantiated without additional methods. abstract data type: An abstraction

### Stacks. Linear data structures

Stacks Linear data structures Collection of components that can be arranged as a straight line Data structure grows or shrinks as we add or remove objects ADTs provide an abstract layer for various operations

### Common Data Structures

Data Structures 1 Common Data Structures Arrays (single and multiple dimensional) Linked Lists Stacks Queues Trees Graphs You should already be familiar with arrays, so they will not be discussed. Trees

### BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security & BSc. (Hons.) Software Engineering Cohort: BIS/05/FT BCNS/05/FT BSE/05/FT Examinations for 2005-2006 / Semester

### 10CS35: Data Structures Using C

CS35: Data Structures Using C QUESTION BANK REVIEW OF STRUCTURES AND POINTERS, INTRODUCTION TO SPECIAL FEATURES OF C OBJECTIVE: Learn : Usage of structures, unions - a conventional tool for handling a

### 12 Abstract Data Types

12 Abstract Data Types 12.1 Source: Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT).

### Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Quiz 4 Solutions Q1: What value does function mystery return when called with a value of 4? int mystery ( int number ) { if ( number

### QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

QUEUES A queue is simply a waiting line that grows by adding elements to its end and shrinks by removing elements from the. Compared to stack, it reflects the more commonly used maxim in real-world, namely,

### A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Binary Search Trees 1 The general binary tree shown in the previous chapter is not terribly useful in practice. The chief use of binary trees is for providing rapid access to data (indexing, if you will)

### DATA STRUCTURE - STACK

DATA STRUCTURE - STACK http://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm Copyright tutorialspoint.com A stack is an abstract data type ADT, commonly used in most programming

### Data Structure with C

Subject: Data Structure with C Topic : Tree Tree A tree is a set of nodes that either:is empty or has a designated node, called the root, from which hierarchically descend zero or more subtrees, which

### Chapter 3: Restricted Structures Page 1

Chapter 3: Restricted Structures Page 1 1 2 3 4 5 6 7 8 9 10 Restricted Structures Chapter 3 Overview Of Restricted Structures The two most commonly used restricted structures are Stack and Queue Both

Linked List as an ADT (cont d.) Default constructor Initializes list to an empty state Destroy the list Deallocates memory occupied by each node Initialize the list Reinitializes list to an empty state

### Analysis of a Search Algorithm

CSE 326 Lecture 4: Lists and Stacks 1. Agfgd 2. Dgsdsfd 3. Hdffdsf 4. Sdfgsfdg 5. Tefsdgass We will review: Analysis: Searching a sorted array (from last time) List ADT: Insert, Delete, Find, First, Kth,

### Inheritance and Virtual Functions. Linked lists.

Inheritance and Virtual Functions. Linked lists. 26th CS427 Lecture 12.2, 11am, 22nd March 2012 CS427 Inheritance and Virtual Functions. Linked lists. 1/27 In today s class 1 Recall... Inheritance 2 Replacing

### Bhakta Kavi Narsinh Mehta University, Junagadh

Bhakta Kavi Narsinh Mehta University, Junagadh Draft Syllabus for B.Sc. (Computer Science) Bachelor of Science (Computer Science) (Semester - 1) Effective From June - 2016 B.Sc. (C.S.) (Semester - 1) CS-101:

### The C Programming Language course syllabus associate level

TECHNOLOGIES The C Programming Language course syllabus associate level Course description The course fully covers the basics of programming in the C programming language and demonstrates fundamental programming

### Data Structures Using C++ 2E. Chapter 7 Stacks

Data Structures Using C++ 2E Chapter 7 Stacks Linked Implementation of Stacks Disadvantage of array (linear) stack representation Fixed number of elements can be pushed onto stack Solution Use pointer

### Data Structure [Question Bank]

Unit I (Analysis of Algorithms) 1. What are algorithms and how they are useful? 2. Describe the factor on best algorithms depends on? 3. Differentiate: Correct & Incorrect Algorithms? 4. Write short note:

### Object Oriented Software Design II

Object Oriented Software Design II Introduction to C++ Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 20, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

### 1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D. base address 2. The memory address of fifth element of an array can be calculated

### Object-Oriented Programming, Iouliia Skliarova

Object-Oriented Programming, Iouliia Skliarova Data types define the way you use the storage (memory) in the programs. By specifying a data type, you tell the sompiler how to create a particular piece

### C Primer. Fall Introduction C vs. Java... 1

CS 33 Intro Computer Systems Doeppner C Primer Fall 2016 Contents 1 Introduction 1 1.1 C vs. Java.......................................... 1 2 Functions 1 2.1 The main() Function....................................

### Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

CS 110B - Rule Storage Classes Page 18-1 Attributes are distinctive features of a variable. Data type, int or double for example, is an attribute. Storage class is another attribute. There are four storage

### BM267 - Introduction to Data Structures

BM267 - Introduction to Data Structures 3. Elementary Data Structures Ankara University Computer Engineering Department BLM267 1 Objectives Learn about elementary data structures - Data structures that

### Collections & Data Structures. CS 3: Computer Programming in Java

Collections & Data Structures CS 3: Computer Programming in Java Objectives The concept of a collection Separating the interface from the implementation Dynamic data structures Linked lists Queues and

### Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010 Problem 1. In each of the following question, please specify if the statement

### Helpful C++ Transitions. Systems Programming

Helpful C++ Transitions A Few Helpful Slides #ifndef Objects using Classes 2 Avoiding Duplicate Headers #ifndef PRODUCT_H #define PRODUCT_H #endif 3 A Node Class Class Node { public: Node (string s); private:

### SYSTEMS PROGRAMMING C++ INTRODUCTION

Faculty of Computer Science / Institute of Systems Architecture / Operating Systems SYSTEMS PROGRAMMING C++ INTRODUCTION Alexander Warg WHY C++? C++ is the language that allows to express ideas from the

### Algorithms and Data Structures Written Exam Proposed SOLUTION

Algorithms and Data Structures Written Exam Proposed SOLUTION 2005-01-07 from 09:00 to 13:00 Allowed tools: A standard calculator. Grading criteria: You can get at most 30 points. For an E, 15 points are

### Integrating the C++ Standard Template Library Into the Undergraduate Computer Science Curriculum

Integrating the C++ Standard Template Library Into the Undergraduate Computer Science Curriculum James P. Kelsh James.Kelsh@cmich.edu Roger Y. Lee lee@cps.cmich.edu Department of Computer Science Central

### 5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

1. The advantage of.. is that they solve the problem if sequential storage representation. But disadvantage in that is they are sequential lists. [A] Lists [B] Linked Lists [A] Trees [A] Queues 2. The

### Sources: On the Web: Slides will be available on:

C programming Introduction The basics of algorithms Structure of a C code, compilation step Constant, variable type, variable scope Expression and operators: assignment, arithmetic operators, comparison,

### C++FA 5.1 PRACTICE MID-TERM EXAM

C++FA 5.1 PRACTICE MID-TERM EXAM This practicemid-term exam covers sections C++FA 1.1 through C++FA 1.4 of C++ with Financial Applications by Ben Van Vliet, available at www.benvanvliet.net. 1.) A pointer

Lecture 11 Doubly Linked Lists & Array of Linked Lists In this lecture Doubly linked lists Array of Linked Lists Creating an Array of Linked Lists Representing a Sparse Matrix Defining a Node for a Sparse

### Data Structures and Data Manipulation

Data Structures and Data Manipulation What the Specification Says: Explain how static data structures may be used to implement dynamic data structures; Describe algorithms for the insertion, retrieval

### Language Examples of ADT: C++

Language Examples of ADT: C++ Based on C struct type and Simula 67 classes All of the class instances of a class share a single copy of the member functions Each instance of a class has its own copy of

### Stacks. The stack ADT Stack Implementation. Stack Examples. using arrays using generic linked lists using List ADT. EECS 268 Programming II 1

Stacks The stack ADT Stack Implementation using arrays using generic linked lists using List ADT Stack Examples 1 Stacks and Queues Linear data structures each item has specific first, next, and previous

### BCS2B02: OOP Concepts and Data Structures Using C++

SECOND SEMESTER BCS2B02: OOP Concepts and Data Structures Using C++ Course Number: 10 Contact Hours per Week: 4 (2T + 2P) Number of Credits: 2 Number of Contact Hours: 30 Hrs. Course Evaluation: Internal

### Data Structures. Level 6 C30151. www.fetac.ie. Module Descriptor

The Further Education and Training Awards Council (FETAC) was set up as a statutory body on 11 June 2001 by the Minister for Education and Science. Under the Qualifications (Education & Training) Act,

### Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Semantic Analysis Scoping (Readings 7.1,7.4,7.6) Static Dynamic Parameter passing methods (7.5) Building symbol tables (7.6) How to use them to find multiply-declared and undeclared variables Type checking

### Standard printing function in C is printf Prints everything numbers, strings, etc. May be complex to use. Standard C library is called libc

Arrays and Structs and Pointers, Oh My! Programming in C Input and output Using printf Standard input and output Pointers Arrays Structures Combining these things together Arrays and Structs and Pointers,

### C++ INTERVIEW QUESTIONS

C++ INTERVIEW QUESTIONS http://www.tutorialspoint.com/cplusplus/cpp_interview_questions.htm Copyright tutorialspoint.com Dear readers, these C++ Interview Questions have been designed specially to get

### WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER Course Outline (2015) Basic Programming With Procedural & Object Oriented Concepts (C, C++) Training Office# Road: 11, House: 1 A, Nikunja 2, Khilkhet,

### Arrays. Arrays, Argument Passing, Promotion, Demotion

Arrays Arrays, Argument Passing, Promotion, Demotion Review Introduction to C C History Compiling C Identifiers Variables Declaration, Definition, Initialization Variable Types Logical Operators Control

### PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

PROBLEM SOLVING WITH SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON Addison Wesley Boston San Francisco New York London

### A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION Tao Chen 1, Tarek Sobh 2 Abstract -- In this paper, a software application that features the visualization of commonly used

### Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Dr. Martin O. Steinhauser University of Basel Graduate Lecture Spring Semester 2014 Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct Friday, 7 th March

### Lecture 12 Doubly Linked Lists (with Recursion)

Lecture 12 Doubly Linked Lists (with Recursion) In this lecture Introduction to Doubly linked lists What is recursion? Designing a node of a DLL Recursion and Linked Lists o Finding a node in a LL (recursively)

### Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Definition Course: Programming II - Abstract Data Types The ADT Queue The ADT Queue is a linear sequence of an arbitrary number of items, together with access procedures. The access procedures permit addition

### Common Syntax and Semantic Errors

C H A P T E R 2 Common Syntax and Semantic Errors 2.1 CHAPTER OBJECTIVES To understand the fundamental characteristics of syntax and semantic errors To be able to identify specific common syntax and semantic

### PES Institute of Technology-BSC QUESTION BANK

PES Institute of Technology-BSC Faculty: Mrs. R.Bharathi CS35: Data Structures Using C QUESTION BANK UNIT I -BASIC CONCEPTS 1. What is an ADT? Briefly explain the categories that classify the functions

### Course MS10975A Introduction to Programming. Length: 5 Days

3 Riverchase Office Plaza Hoover, Alabama 35244 Phone: 205.989.4944 Fax: 855.317.2187 E-Mail: rwhitney@discoveritt.com Web: www.discoveritt.com Course MS10975A Introduction to Programming Length: 5 Days

### Cpt S 223. School of EECS, WSU

Abstract Data Types 1 Topics Abstract Data Types (ADTs) Some basic ADTs: Lists Stacks Queues 2 Primitive Data Type vs. Abstract Data Types Primitive DT: ADT: programmer progra ammer Interface (API) e.g.,

### ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION 2008-10-23/5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION 2008-10-23/5:15-6:45 REC-200, EVI-350, RCH-106, HH-139 Instructions: No aides. Turn off all electronic media and store them under your desk. If

### Circular Linked List. Algorithms and Data Structures

Circular Linked List EENG212 Algorithms and Data Structures Circular Linked Lists In linear linked lists if a list is traversed (all the elements visited) an external pointer to the listmust be preserved

### Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

EECS 281: Data Structures and Algorithms The Foundation: Data Structures and Abstract Data Types Computer science is the science of abstraction. Abstract Data Type Abstraction of a data structure on that

### CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team Lecture Summary In this lecture, we learned about the ADT Priority Queue. A

### csci 210: Data Structures Stacks and Queues

csci 210: Data Structures Stacks and Queues Summary Topics stacks and queues as abstract data types implementations arrays linked lists analysis and comparison application: searching with stacks and queues

### Compiler Design. Type Checking

Compiler Design Type Checking Static Checking Token Stream Parser Abstract Syntax Tree Static Checker Decorated Abstract Syntax Tree Intermediate Code Generator Intermediate Code Static (Semantic) Checks

### Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C 1 An essential part of any embedded system design Programming 2 Programming in Assembly or HLL Processor and memory-sensitive

### Symbol Tables. Introduction

Symbol Tables Introduction A compiler needs to collect and use information about the names appearing in the source program. This information is entered into a data structure called a symbol table. The

### Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Data Structures, Practice Homework 3, with Solutions (not to be handed in) 1. Carrano, 4th edition, Chapter 9, Exercise 1: What is the order of each of the following tasks in the worst case? (a) Computing

### 1 2-3 Trees: The Basics

CS10: Data Structures and Object-Oriented Design (Fall 2013) November 1, 2013: 2-3 Trees: Inserting and Deleting Scribes: CS 10 Teaching Team Lecture Summary In this class, we investigated 2-3 Trees in

Unordered Linked Lists Derive class unorderedlinkedlist from the abstract class linkedlisttype Implement the operations search, insertfirst, insertlast, deletenode See code on page 292 Defines an unordered

### A linear structure is an ordered (e.g., sequenced) arrangement of elements.

Lists, Stacks, and Queues 1 A linear structure is an ordered (e.g., sequenced) arrangement of elements. There are three common types of linear structures: list stack queue random insertion and deletion

### International Journal Of Engineering Research & Management Technology

International Journal Of Engineering Research & Management Technology ISSN: 2348-4039 September- 2014 Volume 1, Issue-5 Dynamic Implementation Using Linked List Karuna Department of Information and Technology

### Lecture Notes on Binary Search Trees

Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 17 October 23, 2014 1 Introduction In this lecture, we will continue considering associative

### Lecture Notes on Binary Search Trees

Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning Lecture 17 March 17, 2010 1 Introduction In the previous two lectures we have seen how to exploit the structure

### ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology) Subject Description: This subject deals with discrete structures like set theory, mathematical

### Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Name: Class: Date: Exam #1 - Prep True/False Indicate whether the statement is true or false. 1. Programming is the process of writing a computer program in a language that the computer can respond to

### An Incomplete C++ Primer. University of Wyoming MA 5310

An Incomplete C++ Primer University of Wyoming MA 5310 Professor Craig C. Douglas http://www.mgnet.org/~douglas/classes/na-sc/notes/c++primer.pdf C++ is a legacy programming language, as is other languages

### 2. Names, Scopes, and Bindings

2. Names, Scopes, and Bindings Binding, Lifetime, Static Scope, Encapsulation and Modules, Dynamic Scope Copyright 2010 by John S. Mallozzi Names Variables Bindings Binding time Language design issues

### Node-Based Structures Linked Lists: Implementation

Linked Lists: Implementation CS 311 Data Structures and Algorithms Lecture Slides Monday, March 30, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org

### NIKLAUS WIRTH Algorithms + Data Structures = Programs

java04.frm Page 171 Saturday, August 26, 2000 6:03 PM 171 Linked Lists The simplest way to interrelate or link a set of elements is to line them up in a single list... For, in this case, only a single

### Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure 1 Dynamic Memory Allocation in C/C++ Motivation /* a[100] vs. *b or *c */ Func(int array_size) double k, a[100], *b, *c; b = (double

### Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Embedded Systems A Review of ANSI C and Considerations for Embedded C Programming Dr. Jeff Jackson Lecture 2-1 Review of ANSI C Topics Basic features of C C fundamentals Basic data types Expressions Selection

### PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

1. Distinguish & and && operators. PART-A Questions 2. How does an enumerated statement differ from a typedef statement? 3. What are the various members of a class? 4. Who can access the protected members

Linked Lists and Iterators Chapter 4 Click to proceed How To View This Presentation This presentation is arranged in outline format. To view the slides in proper order For each slide Read the entire slide

### OBJECT ORIENTED PROGRAMMING IN C++

OBJECT ORIENTED PROGRAMMING IN C++ For Off Campus BSc Computer Science Programme UNIT 1 1. The goal of programmers is to develop software that are. A. Correct B. Reliable and maintainable C. Satisfy all

### The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

The Tower of Hanoi Recursion Solution recursion recursion recursion Recursive Thinking: ignore everything but the bottom disk. 1 2 Recursive Function Time Complexity Hanoi (n, src, dest, temp): If (n >