Linked Lists ... Node. ptr. cat. hat. vat. fat. bat NULL
|
|
- Emery Allen
- 7 years ago
- Views:
Transcription
1 6. Linked List
2 Linked Lists A Linked List consists of many nodes. Each node consists of two parts: LINK and DATA A LINK contains address of (= points to) the next node. Address of nodes is not sequential. ( Address of nodes may change on run time.) Size of a linked list is not predefined. There is a pointer pointing to the first node of a linked list. There is a special address value (= ) at the last node. ptr Node bat cat fat hat... vat
3 Linked Lists Array Sequential representation Insert & Delete : Inefficient due to data shift - O(n) Size of data must be predefined Static storage allocation (during compile time) bat cat fat hat vat Linked List ptr Non-sequential representation Insert & Delete : Efficient due to avoiding data shift - O(1) Size of data needs not to be predefined Dynamic storage allocation (during run time) bat cat fat hat... vat
4 Allocation / Deallocation (Memory) Allocation Get a currently available node. malloc function ex: p =... malloc (sizeof (... )) p A new address is assigned to the pointer variable p! (Memory) Deallocation Return a no longer unused node; free function ex: free(p) p Deallocate the used node!
5 Pointers (Revisited) : Example int i, *pi ; float f, *pf ; i = 1024; f = 3.14; /*static allocation*/ pi = (int *) malloc (sizeof (int)); /*dynamic allocation*/ pf = (float *) malloc (sizeof (float)); /*dynamic alloc.*/ *pi = 1024; *pf = 3.14; printf (*pi,*pf); free (pi); /*dynamic deallocation*/ free (pf); /*dynamic deallocation*/
6 Simple Insertion ptr To Insert eat between cat and fat bat cat fat hat... vat ptr 1 Newly allocate a node by a pointer variable p, and store eat at the node s DATA. 2 Assign the node after cat, which contains fat to the LINK of p. 3 Assign p to the LINK of the node cat. bat cat p 1 3 eat fat 2 hat... vat
7 Simple Deletion ptr To delete fat from the list bat cat fat hat... vat 1 Assign the LINK of fat node to the LINK of cat node 2 Return (deallocate) the fat node by using free ptr 2 free( ) bat cat fat hat... vat 1
8 Node Definition Define the node, named list_node, by using struct Each list_node consist of two fields, DATA and LINK list_ptr is a pointer type pointing to the next list_node ptr is a pointer variable defined on list_ptr, initially typedef struct list_node *list_ptr; typedef struct list_node { int data; list_ptr link; ; list_ptr ptr = ; /*Initialize ptr */ list_prt type ptr (=) DATA field LINK field data link list_node type
9 Basic Operations All the variables p, q, r, are list_ptr type! Initialize Pointer Value : p = p= p->link Move to the next node : p = p -> LINK p Assign Pointer value : Sharing the same node q = p, q = r, p q Compare Pointer Values : only permit ==,!= If (p == q) then, If (p!= q) then, If (p == ) then, If (p!= ) then, If (p -> LINK == ) then.. 9
10 Linked List with Node(s) Linked List with a Single Node ptr = (list_ptr) malloc (sizeof (list_node)); ptr -> data = 10; ptr -> link = ; ptr ptr->data 10 ptr->link Linked List with Two Nodes list_pointer create 2( ) { list_ptr first, second; first = (list_ptr) malloc (sizeof(list_node)); second = (list_ptr) malloc (sizeof (list_node)); second -> link = ; second -> data = 20; first -> data = 10; first -> link = second; return first ; first 10 second 20
11 Insert To do this, three variables are required. ptr : pointing to the first node in a list ins : pointing to the insert position new : pointing to the new node Tow cases are considered. Case 1 : list is empty (i.e., prt is ) Case 2 : list is not empty (i.e., prt is not ) ptr [before] [before] [after] prt ptr = new ptr ins [after] new 25 11
12 Insert addr1 addr2 void Insert (list_ptr *ptr, list_ptr ins) { list_ptr new; new = (list_ptr) malloc (sizeof (list_node)); new -> data = 25; If (*ptr!= ) { new -> link = ins -> link; ins -> link = new; /In case, list is not empty/ else { /In case, list becomes empty/ { new -> link = ; *ptr = new; addrz *ptr addrx ptr To change the ptr value within a function, we need to use pointer!
13 Delete To this end, three variables are also required. ptr : pointing to the first node in a list del : pointing to the delete position pre : pointing to the preceding node w.r.t. the delete node Tow cases exist w.r.t. the delete position Case 1 : the first node in a list (i.e., prev is ) ptr Case 2 : other nodes except the first node (i.e., prt is not ) del pre = ptr delete ptr pre del ptr pre delete 20
14 Delete void Delete (list_ptr *ptr, list_ptr pre, list_ptr del) { If (pre!= ) /In case, the delete node is not the first node/ pre -> link = del -> link; else /In case, the delete node is the first node/ *ptr = (*ptr) -> link; free (del); /Deallocate the delete node/
15 Traverse Print out All Nodes in a List! temp void Traverse-List (list_ptr ptr) { list_ptr temp; temp = ptr while (temp!= ) { printf (temp -> data); temp = temp -> link; ptr D F A... G A D F G What if we use while (temp->link!= )?
16 Linked Stack top Linked Stack / Queue Linked Queue front rear
17 Push : Stack Push (stack_ptr *top, int item) { stack_ptr temp; temp = (stack_ptr)malloc(sizeof (stack)); temp -> data = item; temp -> link = *top; *top = temp; top It does not need stackfull condition! temp item
18 Pop : Stack item=10 top Pop (stack_ptr *top) { stack_ptr temp; int item; temp = *top; If (*top == ) return stack_empty(); item = temp -> data; *top = temp -> link; free(temp); return item; Surely, stack-empty condition is needed. The temp is used for deallocating the delete node; otherwise, numerous garbage are created! temp Free 70
19 Insert : Queue Insert (queue_ptr *front, queue_ptr *rear, int item) { queue_ptr temp; temp = (queue_ptr)malloc(sizeof(queue)); temp -> item = item; temp -> link = ; If (front == ) *front = temp; /Queue Empty/ else *rear -> link = temp; /Queue Not Empty/ *rear = temp; front It does not need Queuefull condition! rear temp item
20 Delete : Queue Delete (queue_ptr *front) { queue_ptr temp; temp = *front; if (*front == ) return queue_empty(); item = temp -> item; *front = temp -> link; free(temp); Clearly, Queue-empty condition is required. The temp is used for deallocating the delete node; otherwise, numerous garbage are created! item=10 front rear temp 10 Free
21 Multiple Stacks Consider Multiple Stacks. (i.e., Num. of stacks n > 1) Method 1: Use of a single Array Method 2: Use of n Arrays Problems: Unpredictable Max. size of each stack! Dynamic change of each stack size! A Not easy to coordinate each stack! Inefficient memory utilization! n-2 n-3 n-2 n-1 Stack 1 Stack 2 Stack k Solution: Use of Linked List! A1 A2 Ak l-1 Stack 1 Stack 2 Stack k
22 Multiple Stacks : Use of Linked List Node Structure for n (>1) Stacks define MAX_SATCKS 10 typedef struct stack *stack_ptr; typedef struct stack { int stack_ptr stack_ptr item; link; top[max_stacks]; Empty Condition for n stacks top[i] == if i-th stack is empty Full Condition for n stacks Not required! (as long as memory is available)
23 Linked Multiple Stacks top[0] Multiple Stacks [Stack 0] D F A... G top[1] [Stack 1] top[n-1] [Stack n-1] B Z C... M P F... K Y Push & Pop take place independently at each stack Each stack size is different from each other, and dynamically varies Memory utilization is very efficient!
24 Polynomial Representation Represent the (Symbolic) Polynomial using Linked List A(x) = a m-1 x e m a 0 x e 0 (where e m-1 > e m-1 >... e 0 >= 0) typedef struct poly_node *poly_ptr; typedef struct poly_node { int coef; int exp; poly_ptr link; poly_ptr a, b, d; poly_node : coef exp link Each term is represented by a node Each node contains a coefficient, an exponent, and a pointer to the next term
25 Polynomial Representation Ex: a = 3x x a b b = 8x 14 3x x
26 Adding Polynomials Consider Two Polynomials as follows: A(x) = a m-1 x e m a 0 x e 0 (pointed by a) B(x) = b n-1 x f n b 0 x f 0 (pointed by b) To Add them, Three Cases must be Considered! Case 1: a->exp == b->exp; add (a->coef + b->coef) a = a->link; b = b->link; Case 2: a->exp < b->exp; b = b->link; Case 3: a->exp > b->exp; a = a->link;
27 CASE 1. a->exp == b->exp Adding Polynomials a [before] a [after] b A(x) = 3 x x x 0 b B(x) = 8 x 14 3 x x 6 result d rear D(x) = 11 x 14
28 CASE 2. a->exp < b->exp Adding Polynomials [before] a a [after] A(x) = 3 x x x 0 b b d B(x) = 8 x 14 3 x x 6 rear d rear D(x) = 11 x 14 3 x 10
29 CASE 3. a->exp > b->exp [before] a Adding Polynomials [after] a A(x) = 3 x x x 0 b b d B(x) = 8 x 14 3 x x 6 rear d rear D(x) = 11 x 14 3 x x 8
30 Case 2! Adding Polynomials When either (a==) or (b==), The comparison terminates! a b b= d rear rear
31 Adding Two Polynomials Adding Polynomials poly_ptr padd( poly_ptr a, ploy_ptr b) { ploy_ptr d, rear; int sum; rear = (poly_ptr) malloc( sizezof(poly_node) ); d = rear; while (a && b) /Until both a & b are not / if ( a->exp < b->exp ) { /Case 2/ attatch (b->coef, b->exp, &rear); b = b->link; else if ( a->exp > b->exp ) { /Case 3/ attach (a->coef, a->exp, &rear); a = a->link; else { /Case 1/ if (sum = a->coef + b->coef) attach(sum, a->exp, &rear) a = a->link; b = b->link;
32 Adding Polynomials poly_ptr padd( poly_ptr a, ploy_ptr b) { /* copy rest of List a and then List b */ for (; a; a = a->link) attach (a->coef, a->exp, &rear); for (; b; b = b->link) attach (b->coef, b->exp, &rear); rear->link = ; temp = d; d = d->link; free (temp); /* delete extra initial node */ return d; void attach(int coefficient, int exponent, poly_ptr *ptr) { poly_ptr temp; temp = (poly_ptr) malloc( sizeof(poly_node) ); temp->coef = coefficient; temp->exp = exponent; (*ptr)->link = temp; /* attach temp to the node pointed by ptr */ *ptr = temp; /* ptr is updated to point to the new node (temp) */
33 Performance Analysis Adding Polynomials m, n : the number of terms in each polynomial A (x) = a m-1 x e m a 0 x e 0 B (x) = b n-1 x f n b 0 x f 0 where e m-1 >... > e 0, f n-1 >... > f 0 Number of Coefficient Additions: O(min{m, n) When none of the exponents is equal 0 When exponents of one polynomial become a subset of exponents of the other min{m, n Number of Exponent Comparisons: O(m+n) When e m-1 > f n-1 > e m-2 > f n-2 >... > e 0 > f 0 m+n-1 Max. number of executions is bounded by m+n O(m+n)
34 What is Garbage? A node that memory has been allocated, but not necessary any more. How to Handle it? Method 1: Return garbage to the storage by calling free(), and reuse it when necessary. Method 2: Connect and manage all garbage in a linked list (called available list) Motivation: Let s do not call unnecessary malloc() and free() functions! Implementation Garbage List Define a pointer variable avail in front of the first node! Define two functions for getting nodes and returning nodes: Get_Node, Return_Node avail List of Available Nodes...
35 Garbage List: Get_Node Take out an available Node from the Available List! poly_ptr Get_Node () { poly_ptr new; /A pointer pointing to a new node/ if (avail!= ) { /If any available node exists/ new = avail; avail = avail -> link; /Take out the first node/ else { /If no available node exists/ new = (poly_ptr) malloc (sizeof (poly_node)); /Request it to the system/ return new; /Return the new node/ avail avail new
36 Garbage List: Return_Node Return an unnecessary node back to the Available List! Return_Node (poly_ptr ptr) { /ptr points to the unnecessary node (i.e., garbage)/ ptr -> link = avail; /Connect the garbage to the available list/ avail = ptr; /Update the avail to point to the first node/ avail ptr avail
37 Ex: e(x) = a(x) * b(x) + d(x) Garbage List! Garbage List: Example poly_ptr a, b, d, e; a = read_poly ( ); b = read_poly ( ); d = read_poly ( ); temp = pmult (a, b); e = padd (temp, d); print_poly (e); a b d temp e The list node temp is not used any more after the addition That is, it produces n (>1) garbage! How to effectively reuse them? 37
38 Garbage List Handling: Method 1 Method 1 Return back n garbage to storage pool by calling free() function! (But, ptr is a pointer that points to the first node of the list) ptr Program Domain... Garbage List free() System Domain Storage Pool 38
39 Garbage List Handling: Method 1 Return_To_System (poly_ptr *ptr) { /ptr points to the first of the garbage list/ poly_ptr temp; while (*ptr!= ) { temp = *ptr; /temp follows up ptr/ *ptr = *ptr -> link; /ptr moves to the next/ free (temp); /return by calling free function/ temp ptr ptr free( ) Return all nodes back to the storage pool by doing a loop from the first to the last in the list Since free() function is called n times, O(n); Inefficient!
40 Garbage List Handling: Method 2 Method 2: Return n garbage to an available list without calling free function (But, ptr is a pointer that points to the first node of the list) ptr Program Domain... Garbage List return avail Program Domain... List of Available Nodes
41 ptr= ptr Return_To_Avail (poly_ptr *ptr, ploy_ptr *avail) { poly_ptr temp; /ptr points to the first of the garbage list/ If (ptr!= ) { temp = *ptr ; while (temp ->link!= ) /until the last node is found/ temp = temp -> link; temp -> link = avail; /connect the garbage list to the avail list/ *avail = *ptr; /let the avail point to the first node again/ *ptr = ; /reinitialize the garbage list by empty/ Garbage List Handling: Method 2 temp avail No free function call is brought forth. But, it needs to find the address of the last node of the list, starting from the first node, O(n); Inefficient!
42 Garbage List Handling: Method 3 Method 3: First, change the Garbage List Structure as follows: The last node points to the first node of the list, i.e., Circular Linked List Second, return the Circular Garbage List to the Singular Available List! ptr avail Program Domain Circular Garbage List return Program Domain Singular List of Available Nodes
43 Garbage List Handling: Method 3 Circular-Return (poly_ptr *ptr, poly_ptr *avail) { poly_ptr temp; if (*ptr!= ) { /if the garbage list is non-empty/ temp = (*ptr) -> link; /temp points to the next node/ (*ptr) -> link = *avail; /connect the first node to the avail list/ *avail = temp; *ptr = ; ptr= ptr /let the avail point to the first node again/ avail temp No need to search for the last node (i.e., )! All nodes are returned at a time. O(1) is guaranteed, regardless of the number of nodes: Efficient!
44 Some Useful Operations Concatenate Two linked lists. Invert a linked list. Find the length of a Circular List. Insert at front and rear.
45 Concatenating Linked Lists Concatenate Tow Listed Lists pointed to by ptr1 and ptr2; prt1 = (a 1, a 2,..., a m ), ptr2 = (b 1, b 2,..., b n ) list_ptr concat (list_ptr ptr1, ptr2) { list_ptr temp; if (ptr1 == ) return ptr2; /if the first list is empty/ else if (ptr2!= ) { temp = ptr1; while (temp -> link!= ) temp = temp -> link; /Move for finding the last node of the first list/ return ptr1; temp -> link = ptr2; /connect the first & second lists/ ptr1 ptr2 temp temp temp
46 Invert Invert a Given list(a 1, a 2,..., a n ) into a new list(a n, a n-1,..., a 1 ) Three Pointer Variables are needed. lead: point to the first of list middle: point to the preceding node of the lead trail: point to the preceding node of the middle before lead a 1 a 2 a... 3 a n-1 a n after middle= lead a 1 trail= middle a 2 trail middle trail middle a... 3 a n-1 middle a n trail middle lead lead lead lead lead=
47 Invert list_ptr Invert (list_ptr lead) { /invert the list pointed to by lead/ list_ptr middle, trail; middle = ; while (lead!= ) { trail = middle; /trail follows up middle/ middle = lead; /middle follows up lead/ lead = lead -> link; /lead moves to the next/ middle -> link = trail; /connect to the preceding node/ return middle;
48 Length of List Count the Number of Nodes in a Circular List int Length (list_ptr ptr) { list_ptr temp; int count = 0; if (ptr!= ) { temp = ptr; do { count++; temp = temp -> link; while (temp!= ptr) return count; ptr count=1 count=2 count=n temp temp temp Since temp==ptr, Stop!
49 Length of List Count the Number of Nodes in a Circular List int Length (list_ptr ptr) { list_ptr temp; int count = 0; if (ptr!= ) { temp = ptr; while (temp!= ptr) {/error! never executed/ return count; count++; temp = temp -> link; ptr count=1 count=2 count=n temp temp temp Since temp==ptr, Stop!
50 Insert : front / rear Insert a Node at the Front and at the Rear. ptr For the Singly Linked List (prt points to the first of list) a 1 a 2 a... 3 a n-1 a n Insert at Front & Insert at Rear: What to do?? Problem?? For the Circular Linked List (prt points to the first of list) ptr a 1 a 2 a... 3 a n-1 a n Insert at Front & Insert at Rear: What to do?? Problem??
51 Insert : front / rear Insert a Node at the Front and at the Rear. Consider Modified Circular Linked List (prt points to the last node of list) Inserting a node at both the Front and the Rear is done with ease. ptr a 1 a 2... a n-1 a n ins a 0 - ptr ins a n+1 - Insert at Front Insert at Rear
52 Insert : front / rear Insert_front_rear (list_ptr *ptr, list_ptr ins) { /ins is the insert node, ptr points to the last node/ if (ptr == ) { /if list is empty/ *ptr = ins; ins -> link = ins; /Circular List of length=1/ else { /list is non-empty/ ins -> link = (*ptr) -> link; (*ptr) -> link = ins; /insert the node/ *ptr = ins; /ptr points to the last node/ What if *ptr=ins is deleted?
53 Doubly Linked Lists Problems of Singly Linked Lists Move to only one way direction (i.e., left to right) Not easy to delete arbitrary nodes (i.e., hard to find the address of previous node; In worst case, O(n)) Solution: Doubly Linked Lists (Circular) linked lists + Double Links = Doubly (Circular) Linked Lists Allow two links for each node; Move to Two way direction; both left and right move ptr LLINK DATA RLINK Previous Node Next Node prt
54 Node Definition typedef struct list_node *list_ptr; typedef struct list_node { list_ptr LLINK; int DATA; list_ptr RLINK; list_ptr ptr = ; /*Initialize ptr */ ptr = (list_ptr) malloc (sizeof (list_node)); Previous Node prt LLINK DATA RLINK Next Node
55 Doubly Linked Lists Basic Structure of Doubly Linked List a 1 a 2 a n Doubly Circular Linked List Transform the basic structure into the circular linked list in order to insert and delete nodes easily Employ an additional dummy node, termed head The data field of head is always empty The head node exists all the times even when list is empty LLINK DATA RLINK Previous Node Next Node head
56 Doubly Circular Linked Lists Doubly Circular Linked Lists It starts from the head, and the last & the first nodes point to the head RLINK and LLINK of head point to the first and the last nodes, respectively. If ptr points to an arbitrary node, we have the following rules: ptr -> LLINK -> RRLINK = ptr = ptr -> RLINK -> LLINK LLINK head RLINK a 1 a 2 a n a 3 ptr
57 Examples Doubly Circular Linked Lists LLINK RLINK head a 1 a 2 a n a 3 Case I. List consists of n (n > 1) nodes.. a 1 LLINK. -. head RLINK LLINK. -. head RLINK Case II. Single node list (i.e., n=1) Case III. Empty list (i.e., n=0)
58 Insert Two Pointer Variables are required. ins : it points to the insert position new : it points to the newly allocated node Insert (node_ptr ins, node_ptr new) { new -> LLINK = ins; new -> RLINK = ins -> RLINK; ins -> RLINK -> LLINK = new; ins -> RLINK = new; new a 1 before ins after I. Empty Case LLINK LLINK head RLINK head RLINK ins
59 II. Non-Empty Case before Insert LLINK. -. head RLINK.. a 1 a 2 a n a 3 after ins LLINK. -. head RLINK. a 1 a 2 a 3 a n. 4 ins a 1. new
60 Delete Two Pointer Variables are also required! Delete (node_ptr head, node_ptr del) { head: it points to the first node of the list del: it points to the delete node Different from Singly lists, it does not need to know the preceding node if (del == head) printf ( Do not delete ) /if list is empty; i.e., only head node/ else { del -> LLINK -> RLINK = del -> RLINK; del -> RLINK -> LLINK = del -> LLINK; free (del); del I. Single Node Case before after 2 LLINK LLINK a head RLINK head RLINK 1
61 II. Multiple Node Case before Delete LLINK. -. head RLINK.. a 1 a 2 a n a 3 after del LLINK. -. head RLINK 1.. a 1 a 2 a n 2 del free( ) 3 a 3
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
More informationLinked Lists Linked Lists, Queues, and Stacks
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
More information10CS35: 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
More informationAnalysis 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,
More informationPES 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
More informationLecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists
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
More informationLinked 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
More informationData 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
More informationWhat is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation
Stacks and Queues What is a Stack? Stores a set of elements in a particular order Accessed in Last-In In-First-Out (LIFO) fashion Real life examples: Pile of books PEZ dispenser Cup trays in cafeteria
More informationQuestions 1 through 25 are worth 2 points each. Choose one best answer for each.
Questions 1 through 25 are worth 2 points each. Choose one best answer for each. 1. For the singly linked list implementation of the queue, where are the enqueues and dequeues performed? c a. Enqueue in
More informationDATA 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
More informationData 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
More informationLecture 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)
More informationCommon 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
More informationAbstract 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
More information5. 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
More informationKITES 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
More information1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)
Chapter 7 Data Structures for Computer Graphics (This chapter was written for programmers - option in lecture course) Any computer model of an Object must comprise three different types of entities: 1.
More informationAtmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : 572365, 576681 1
Data Structure By Ajay Raiyani Yogidham, Kalawad Road, Rajkot. Ph : 572365, 576681 1 Linked List 4 Singly Linked List...4 Doubly Linked List...7 Explain Doubly Linked list: -...7 Circular Singly Linked
More informationQueues. Manolis Koubarakis. Data Structures and Programming Techniques
Queues Manolis Koubarakis 1 The ADT Queue A queue Q of items of type T is a sequence of items of type T on which the following operations are defined: Initialize the queue to the empty queue. Determine
More informationMemory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation
Dynamic Storage Allocation CS 44 Operating Systems Fall 5 Presented By Vibha Prasad Memory Allocation Static Allocation (fixed in size) Sometimes we create data structures that are fixed and don t need
More informationStacks. 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
More informationMAX = 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
More informationCSE 211: Data Structures Lecture Notes VII
CSE 211: Data Structures Lecture Notes VII LINKED LISTS In the previous lectures we have seen the representation of ordered lists using an array and sequential mapping. These representations had the property
More informationShort 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
More informationData 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:
More informationLinear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta
Linear DT-1: Restricted Lists Stacks, Queues tul Gupta Restricted Lists Stack Queue Circular queue Priority queue General Lists rrays Linked list Circular list Doubly linked list Linear DTs 1 Stacks Using
More informationData 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
More informationModule 2 Stacks and Queues: Abstract Data Types
Module 2 Stacks and Queues: Abstract Data Types A stack is one of the most important and useful non-primitive linear data structure in computer science. It is an ordered collection of items into which
More informationSTACKS,QUEUES, AND LINKED LISTS
STACKS,QUEUES, AND LINKED LISTS Stacks Queues Linked Lists Double-Ended Queues Case Study: A Stock Analysis Applet 1 Stacks Astack is a container of objects that are inserted and removed according to the
More informationBSc (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
More informationIntroduction to Data Structures and Algorithms
Introduction to Data Structures and Algorithms Chapter: Elementary Data Structures(1) Lehrstuhl Informatik 7 (Prof. Dr.-Ing. Reinhard German) Martensstraße 3, 91058 Erlangen Overview on simple data structures
More informationLINKED DATA STRUCTURES
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
More informationECE 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
More informationList, Stack and Queue. Tom Chao Zhou CSC2100B Data Structures Tutorial 3
List, Stack and Queue Tom Chao Zhou CSC2100B Data Structures Tutorial 3 Outline Structure Linked List Overview Implementation Stack Overview Implementation Queue Overview Implementation Structure A collection
More informationSequential Data Structures
Sequential Data Structures In this lecture we introduce the basic data structures for storing sequences of objects. These data structures are based on arrays and linked lists, which you met in first year
More informationData 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
More informationQuiz 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
More informationKrishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C
Tutorial#1 Q 1:- Explain the terms data, elementary item, entity, primary key, domain, attribute and information? Also give examples in support of your answer? Q 2:- What is a Data Type? Differentiate
More informationADTs,, Arrays, Linked Lists
1 ADTs,, Arrays, Linked Lists Outline and Required Reading: ADTs ( 2.1.2) Arrays ( 1.5) Linked Lists ( 4.3.1, 4.3.2) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic Abstract Data Type (ADT) 2 abstract
More informationSystem Software Prof. Dr. H. Mössenböck
System Software Prof. Dr. H. Mössenböck 1. Memory Management 2. Garbage Collection 3. Linkers and Loaders 4. Debuggers 5. Text Editors Marks obtained by end-term exam http://ssw.jku.at/misc/ssw/ 1. Memory
More informationSt S a t ck a ck nd Qu Q eue 1
Stack and Queue 1 Stack Data structure with Last-In First-Out (LIFO) behavior In Out C B A B C 2 Typical Operations Pop on Stack Push isempty: determines if the stack has no elements isfull: determines
More informationStorage 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
More informationC Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell
C Dynamic Data Structures University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell Data Structures A data structure is a particular organization of data in memory. We want to
More informationStack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1
Stack & Queue Explain Array in detail One Dimensional Array Simplest data structure that makes use of computed address to locate its elements is the onedimensional array or vector; number of memory locations
More informationAlgorithms and Data Structures
Algorithms and Data Structures Part 2: Data Structures PD Dr. rer. nat. habil. Ralf-Peter Mundani Computation in Engineering (CiE) Summer Term 2016 Overview general linked lists stacks queues trees 2 2
More informationCourse: 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
More informationUnit 1. 5. Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]
Unit 1 1. Write the following statements in C : [4] Print the address of a float variable P. Declare and initialize an array to four characters a,b,c,d. 2. Declare a pointer to a function f which accepts
More informationUniversidad Carlos III de Madrid
Universidad Carlos III de Madrid Algorithms and Data Structures (ADS) Bachelor in Informatics Engineering Computer Science Department Lists, Stacks and Queues. Authors: Isabel Segura Bedmar April 2011
More informationOutput: 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)
More information\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,
Elementary Data Structures \Mankinds's progress is measured by the number of things we can do without thinking." Elementary data structures such as stacks, queues, lists, and heaps will be the \of-the-shelf"
More informationThe 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
More informationBasic Data Structures and Algorithms
Tutorial 3 Basic Data Structures and Algorithms THINGS TO LOOK FOR 3.0 INTRODUCTION 3.1 Array Based Containers Definition and uses of containers. Array and list based containers. Designing and building
More informationTREE BASIC TERMINOLOGIES
TREE Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing hierarchical relationship between the grand father and his children and
More informationStack Allocation. Run-Time Data Structures. Static Structures
Run-Time Data Structures Stack Allocation Static Structures For static structures, a fixed address is used throughout execution. This is the oldest and simplest memory organization. In current compilers,
More informationEE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)
EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE) UNIT I LINEAR STRUCTURES Abstract Data Types (ADT) List ADT array-based implementation linked list implementation cursor-based linked lists
More informationChapter 5 Names, Bindings, Type Checking, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Scope Scope and Lifetime Referencing Environments Named
More informationPointers and Linked Lists
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
More informationData Structures Using C++
Data Structures Using C++ 1.1 Introduction Data structure is an implementation of an abstract data type having its own set of data elements along with functions to perform operations on that data. Arrays
More information1. 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
More informationLecture 18-19 Data Types and Types of a Language
Lecture 18-19 Data Types and Types of a Language April 29, 2014 Data Types and Types of a Language Data, Data Types and Types Type: Generalities Type Systems and Type Safety Type Equivalence, Coercion
More informationCircular 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
More informationClass Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction
Class Overview CSE 326: Data Structures Introduction Introduction to many of the basic data structures used in computer software Understand the data structures Analyze the algorithms that use them Know
More informationQueues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic
Queues Outline and Required Reading: Queues ( 4. except 4..4) COSC, Fall 3, Section A Instructor: N. Vlajic Queue ADT Queue linear data structure organized according to first-in/first-out (FIFO) principle!
More informationCourse: 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
More informationAbstract Data Types. Chapter 2
Chapter 2 Abstract Data Types The second idea at the core of computer science, along with algorithms, is data. In a modern computer, data consists fundamentally of binary bits, but meaningful data is organized
More informationCS 2412 Data Structures. Chapter 3 Queues
CS 2412 Data Structures Chapter 3 Queues 3.1 Definitions A queue is an ordered collection of data in which all additions to the collection are made at one end (called rare or tail) and all deletions from
More informationProgramming with Data Structures
Programming with Data Structures CMPSCI 187 Spring 2016 Please find a seat Try to sit close to the center (the room will be pretty full!) Turn off or silence your mobile phone Turn off your other internet-enabled
More informationQueue ADT. March 16, 2000
Queue ADT March 6, 2000 The Queue ADT Queue represent another form of linear data structure. As in the case of the list, the idea of a queue is to represent sequences of elements, all having the same type.
More information22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department http://www.cs.uiowa.edu/~hzhang/c31/
22c:31 Algorithms Ch3: Data Structures Hantao Zhang Computer Science Department http://www.cs.uiowa.edu/~hzhang/c31/ Linear Data Structures Now we can now explore some convenient techniques for organizing
More informationCHAPTER 4 ESSENTIAL DATA STRUCTRURES
CHAPTER 4 ESSENTIAL DATA STRUCTURES 72 CHAPTER 4 ESSENTIAL DATA STRUCTRURES In every algorithm, there is a need to store data. Ranging from storing a single value in a single variable, to more complex
More informationChapter 5 Functions. Introducing Functions
Chapter 5 Functions 1 Introducing Functions A function is a collection of statements that are grouped together to perform an operation Define a function Invoke a funciton return value type method name
More informationBoolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)
Boolean Expressions, Conditions, Loops, and Enumerations Relational Operators == // true if two values are equivalent!= // true if two values are not equivalent < // true if left value is less than the
More informationUnordered Linked Lists
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
More informationGUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: 3330704)
GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT Course Curriculum DATA STRUCTURES (Code: 3330704) Diploma Programme in which this course is offered Semester in which offered Computer Engineering,
More informationLecture 11 Array of Linked Lists
Lecture 11 Array of Linked Lists In this lecture Array of Linked Lists Creating an Array of Linked Lists Representing a Sparse Matrix Defining a Node for Sparse Matrix Exercises Solutions An Array of Linked
More informationCoding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.
Coding Rules Section A: Linux kernel style based coding for C programs Coding style for C is based on Linux Kernel coding style. The following excerpts in this section are mostly taken as is from articles
More informationData 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,
More informationScoping (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
More informationSources: 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,
More informationAlgorithms and Data Structures
Algorithms and Data Structures Lists and Arrays Marcin Sydow Web Mining Lab PJWSTK Marcin Sydow (Web Mining Lab PJWSTK) Algorithms and Data Structures 1 / 35 Topics covered by this lecture: Linked Lists
More informationLecture 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
More informationIS0020 Program Design and Software Tools Midterm, Feb 24, 2004. Instruction
IS0020 Program Design and Software Tools Midterm, Feb 24, 2004 Name: Instruction There are two parts in this test. The first part contains 50 questions worth 80 points. The second part constitutes 20 points
More informationQueue Implementations
Queue Implementations as double 1 2 as double MCS 360 Lecture 17 Introduction to Data Structures Jan Verschelde, 1 October 2010 Queue Implementations as double 1 2 as double a circular as double A can
More informationC++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
More informationData Structures Fibonacci Heaps, Amortized Analysis
Chapter 4 Data Structures Fibonacci Heaps, Amortized Analysis Algorithm Theory WS 2012/13 Fabian Kuhn Fibonacci Heaps Lacy merge variant of binomial heaps: Do not merge trees as long as possible Structure:
More informationINTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY
INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY A PATH FOR HORIZING YOUR INNOVATIVE WORK A REVIEW ON THE USAGE OF OLD AND NEW DATA STRUCTURE ARRAYS, LINKED LIST, STACK,
More informationC++ 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
More informationLecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()
CS61: Systems Programming and Machine Organization Harvard University, Fall 2009 Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc() Prof. Matt Welsh October 6, 2009 Topics for today Dynamic
More informationDATA 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
More informationData 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
More informationEmbedded 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
More informationLecture Notes on Stacks & Queues
Lecture Notes on Stacks & Queues 15-122: Principles of Imperative Computation Frank Pfenning, André Platzer, Rob Simmons Lecture 9 February 12, 2013 1 Introduction In this lecture we introduce queues and
More informationBCS2B02: 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
More informationObject 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
More informationData Structures and Algorithms!
Data Structures and Algorithms! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 2! 1 Motivating Quotation!!! Every program depends on algorithms
More informationData Structures and Algorithms Lists
Data Structures and Algorithms Lists Chris Brooks Department of Computer Science University of San Francisco Department of Computer Science University of San Francisco p.1/19 5-0: Abstract Data Types An
More informationThe Advantages of Dan Grossman CSE303 Spring 2005, Lecture 25
CSE 303: Concepts and Tools for Software Development Dan Grossman Spring 2005 Lecture 25 Memory-Management Idioms Dan Grossman CSE303 Spring 2005, Lecture 25 1 No tools or rule today Review: Java and C
More informationCOMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.
COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms 1 Activation Records activation declaration location Recall that an activation
More informationNode-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
More information