Introduction to Data Structure

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

CSCE 3110 Data Structures & Algorithm Analysis
Chapter 4 Lists Pointers Singly Linked Lists
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
CS Data Structures Chapter 4 Lists. Chain (1/3) Chain: Chain: A singly linked list in which the last node has a null link A singly linked list in.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
CSCI2100B Linked List Jeffrey
§1 Abstract Data Type (ADT)
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Bioinformatics Programming
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Data Structures: Doubly Linked List1 Doubly linked list l The linear linked list is accessed from the first node each time we want to insert or delete.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
1 Linked Lists Gordon College Prof. Brinton. 2 Linked List Basics Why use? 1.Efficient insertion or deletion into middle of list. (Arrays are not efficient.
Chapter 4.
CS Data Structures Chapter 4 Lists. Dynamically Linked Stacks and Queues (1/8)  When several stacks and queues coexisted, there was no efficient.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
CHAPTER 41 LISTS All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures.
CS Data Structures Chapter 4 Lists.
Chapter 4 Lists Fundamentals of Data Structures in C Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals.
Chapter 4 1. Why “linked lists” 2 IndexWord A[0]BAT A[1]CAT A[2]EAT … A[n]WAT Insert “FAT” ? Or delete something.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer
Reference: Vinu V Das, Principles of Data Structures using C and C++
CHAPTER 41 LISTS All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
Linked List (Part II). Introduction  Definition of equivalence relation: A relation ≡ over a set S, is said to be an equivalence relation over S iff.
Chap 4 Linked Lists. Review of Sequential Representations Previously introduced data structures, including array, queue, and stack, they all have the.
Data Structure in C Transparency No. 4-1 Copyright(c) 1997, Sungkyunkwan University Chapter #4: LISTS Fundamentals of Data Structure in C Horowitz, Sahni.
 Array ◦ sequential representation ◦ some operation can be very time-consuming (data movement) ◦ size of data must be predefined ◦ static storage allocation.
Review of Sequential Representations Previously introduced data structures, including array, queue, and stack, they all have the property that successive.
Chapter 4 (cont.) Additional Lists Operations Circular Lists The link field of the last node points to the first node in the list... BATCATFATWAT.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-1 Chapter 8 Hashing Introduction to Data Structure CHAPTER 8 HASHING 8.1 Symbol Table Abstract Data.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Copyright Networking Laboratory Chapter 4. LISTS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.
Linked List (Part I). Introduction  Weakness of storing an ordered list in array: Insertion and deletion of arbitrary elements are expensive. ○ Example:
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.
Array 10 GB Hard Disk 2 GB 4 GB2 GB 3 GB DATA 4 GB Free Store data in the form of Array (Continuous memory locations) Solution-1: No Solution. Memory Insufficient.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Dale Roberts, Lecturer Data Structure.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
UNIT-II Topics to be covered Singly linked list Circular linked list
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Software Learning Resource Service Platform CHAPTER 4 鏈結串列 (Linked List) 1.
Programming Application in Civil Engineering
Linked List :: Basic Concepts
Lectures linked lists Chapter 6 of textbook
Elementary Data Structures
Data Structure Dr. Mohamed Khafagy.
CSCE 3110 Data Structures & Algorithm Analysis
UNIT-3 LINKED LIST.
Data Structures 7th Week
EEE2108: Programming for Engineers Chapter 4. Linked Lists
Linked List Sudeshna Sarkar.
CSCE 3110 Data Structures & Algorithm Analysis
Introduction to Linked Lists
Linked List (Part I) Data structure.
Chapter 16 Linked Structures
Data Structures Chapter 4: Linked Lists.
Dynamic Data Structures
Presentation transcript:

Introduction to Data Structure CHAPTER 4 LINKED LISTS 4.1 Pointers 4.2 Singly Linked Lists 4.3 Dynamic Linked Stacks and Queues 4.4 Polynomials 4.5 Additional List Operations 4.6 Equivalence Relations 4.7 Sparse Matrices 4.8 Doubly Linked Lists

Contents Chapter 1 Basic Concepts Chapter 2 Arrays Chapter 3 Stacks and Queues Chapter 4 Linked Lists Chapter 5 Trees Chapter 6 Graph Chapter 7 Sorting Chapter 8 Hashing Chapter 9 Heap Structures Chapter 10 Search Structures

4.1 Pointer Pointer in C int i, *pi; pi = &i; i=10 or *pi=10 pi = malloc(sizeof(int)); /* assign to pi a pointer to int */ int i, *pi; i pi Some integer i pi 10 pi

4.2 Singly Linked Lists Ordered Lists Seq. eg. Non-seq. mapping: Insert “D” Data Link Sequential mapping (not suitable for insertion & deletion) Non-sequential: linked Operations - length - read all - retrieve i-th - store i-th - Insert - delete A C E G X Y insert D A C D E G X Y insert 1 2 3 4 5 6 7 head=1 A C E G X Y D 2 3 4 5 6 7 1 2 3 4 5 6 7

Singly Linked Lists: Delete “G” Non-seq. mapping: Delete “G” Data Link head=1 1 2 3 4 5 6 7 A C E G X Y D delete 2 7 4 5 6 3 5 1 2 3 4 5 6 7 delete G

Singly Linked Lists: Arrow Notation Insert “D” Delete “G” 1 2 A C Y head 6 …… 2 A 3 C E 1 D 7 head G

Singly Linked Lists vs. Array Successive items locate a fixed distance Disadvantages: data movements during insertion and deletion Possible solution Linked list More Examples: Figure 4.1 vs. Figure 4.2 (p.140) Figure 4.3 Figure 4.4

Defining a Linked List Node in C To represent a single node in C To define the structure of a node, we need to know the type of each of its field Example: ThreeLetterNode -- Class definition data link Structure data type: array or char or … pointer ThreeLetterNode typedef struct list_node *list_pointer; typedef struct list_node { char data[3]; list_pointer link; }; data link Some ThreeLetterNode data[0] data[1] data[2]

Example: A complex List Structure The definition of Node A The definition of Node B typedef struct list_nodea *nodea; typedef struct list_nodea { int data1; char data2; float data3; nodea linka; nodeb linkb; }; typedef struct list_nodeb *nodeb; typedef struct list_nodeb { int data; nodeb linkb; }; 55 ‘c’ 3.14 22 A possible configuration list_nodeb list_nodea

Example: A complex List Structure (cont.) The definition of Node A The definition of Node B typedef struct list_nodea *nodea; typedef struct list_nodea { int data1; char data2; float data3; nodea linka; nodeb linkb; }; typedef struct list_nodeb *nodeb; typedef struct list_nodeb { int data; nodeb linkb; }; 55 ‘c’ 3.14 list_nodea 22 23 Another possible configuration list_nodeb

Creating a New Node in C Assume the following node structure typedef struct list_node *list_pointer; typedef struct list_node { char data[3]; list_pointer link; }; Invoke malloc to obtain a storage to hold the node list_pointer ptr = NULL; ptr = (list_pointer) malloc(sizeof(list_node)); ptr NULL  ptr

Freeing a Node in C Assume the following node structure typedef struct list_node *list_pointer; typedef struct list_node { char data[3]; list_pointer link; }; Invoke function free to return the node’s storage free(ptr);  ptr

Example List Manipulation Operations in C Assume we have classes: ListNode and List Some example list manipulation operations: create2( ) // Create two nodes insert( ) delete( ) Inverting a list concatenating two lists … typedef struct list_node *list_pointer; typedef struct list_node { int data; list_pointer link; }; list_pointer ptr = NULL; List ptr … list_node

Example: Create a Two-node List list_pointer create2( ) { list_pointer first, second; first = (list_pointer)malloc(sizeof(list_node)); second = (list_pointer)malloc(sizeof(list_node)); second->link = NULL; second->data = 20; first->data = 10; first->link = second; return first; } p.143 10 0 first 20 NL NULL 10  first 20

Example: Inserting a Node void insert(list_pointer *ptr, list_pointer node) { list_pointer temp; temp = (list_pointer)malloc(sizeof(list_node)); /* create a new node */ if (IS_FULL(temp)) { fprintf(stder, “The memory is full\n”); exit(1); } temp->data = 50; if (*ptr) { temp->link = node->link; node->link = temp; } else { /* list ptr is empty */ temp->link = NULL; *ptr = temp; } } p.144 /* insert a new node with data = 50 into list ptr after node */ 50 NULL temp ptr node 50  10  20 NULL temp ptr . . .

Example: Deleting a Node void delete(list_pointer *ptr, list_pointer trail, list_pointer node) { if (trail) trail->link = node->link; else *ptr = (*ptr)->link; free(node); /* return the node to the system */ } /* delete the node after trail */ p.145 10  20 NULL 50  ptr node trail How about if trail = NULL?

Delete a Node: A Dangling Reference trail ptr BAT  CAT  MAT  SAT  VAT NULL dangling reference

4.3 Linked Stacks and Queues Stacks and Queues in Linked lists  NULL    top element link (a) Linked Stack front (b) Linked Queue rear F R Note: Compare with sequence queue

Linked m Stacks and m Queues Several stacks and queues co-exist: Sequence – not easy to handle “insert” and “delete” Linked – a good choice Representing n stacks Initial condidtion: top[i] = NULL, 0 ≤ i < MAX_STACKS Boundary conditions: top[i] = NULL iff ith stack is empty IS_FULL(temp) iff the memory is full #define MAX_STACKS 10 typedef struct { int key; /* other fields */ } element; typedef struct stack *stack_pointer; typedef struct stack { element item; stack_pointer link; }; stack_pointer top[MAX_STACKS]; top top i.e. NULL = 0  null value

Linked m Stacks and m Queues (cont.) Representing m queues #define MAX_QUEUES 10 typedef struct queue *queue_pointer; typedef struct queue { element item; queue_pointer link; }; queue_pointer front[MAX_QUEUES], rear[MAX_QUEUES]; Initial condidtion: front[i] = NULL, 0 ≤ i < MAX_QUEUES Boundary conditions: front[i] = NULL iff ith queue is empty IS_FULL(temp) iff the memory is full front rear i.e. NULL = 0  null value

Linked Stacks: Operations Add to a linked stack void add(stack_pointer *top, element item) { stack_pointer temp = (stack_pointer) malloc(sizeof (stack)); if (IS_FULL(temp)) { fprintf(stderr, “The memory is full\n”); exit(1); } temp->item = item; temp->link = *top; p.149 top temp

Linked Stacks: Operations (cont.) Delete from a linked stack element delete(stack_pointer *top) { stack_pointer temp = *top; element item; if (IS_EMPTY(temp)) { fprintf(stderr, “The memory is empty\n”); exit(1); } item = temp->item; *top = temp->link; free(temp); return item; p.149 temp top

Linked Queues: Operations Add into a linked queue void addq(queue_pointer *front, queue_pointer *rear, element item) { queue_pointer temp = (queue_pointer) malloc(sizeof (queue)); if (IS_FULL(temp)) { fprintf(stderr, “The memory is empty\n”); exit(1); } temp->item = item; temp->link = NULL; if (*front) (*rear)->link = temp; else *front = temp; *rear = temp; temp front rear p.151

Linked Queues: Operations (cont.) Delete from a linked queue element deleteq(queue_pointer *front) { queue_pointer temp = *front; element item; if (IS_EMPTY(temp)) { fprintf(stderr, “The memory is empty\n”); exit(1); } item = temp->item; *front = temp->link; free(temp); return item; p.151 temp front rear

4.4 Polynomials Consider a polynomial Tackle the reasonable-sized problem Node structure e.g. A = 3x14 + 2x8 + 1 Note: in Array term: represented by a node coef exp link A 3 14 2 8 1 ^ ^ 3 2 …… 1

Polynomial Representation Type declaration typedef struct poly_node *poly_pointer; typedef struct poly_node { int coef; int expon; poly_pointer link; }; poly_pointer a, b, d; p.152 coef exp link poly

Polynomial Representation: Example Examples 3 14 2 8 1 0 a null 8 14 -3 10 10 6 b null

Adding Polynomials : d = a + b Adding Polynomials: Figure 4:12 d = a + b Case 1: a->exp = b->exp a 3 14 2 8 1 0 a b 8 14 -3 10 10 6 b d 11 14 d

Adding Polynomials : d = a + b (cont.) Case 2: a->exp < b->exp 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 -3 10 d d

Adding Polynomials: d = a + b (cont.) Case 3: a->exp > b->exp 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 -3 10 2 8 d

Algorithm for adding two polynomials Adding Polynomials: Algorithm Algorithm for adding two polynomials (C in Program 4.10, p.154) … // copy rest of a // copy rest of b

Adding Polynomials: Complexity Analysis (1) Coefficient additions 0  additions  min(m, n) where m (n) denotes the number of terms in A (B). (2) Exponent comparisons extreme case em-1 > fm-1 > em-2 > fm-2 > … > e0 > f0 m+n-1 comparisons (3) Creation of new nodes m + n new nodes Summary: O(m+n), where m (n) denotes the number of terms in A (B).

Consider a Polynomial expression: Erasing Polynomials Consider a Polynomial expression: e (x) = a (x) * b (x) + d (x) polynomial a, b, d, e; a = read_poly(); // read and create polynomial a b = read_poly(); // read and create polynomial b d = read_poly(); // read and create polynomial c temp = pmult(a, b); e = padd(temp, d); print_poly(e) ; Before and after the above procedure been executed Free all nodes?

Erasing Polynomials (cont.) C program void earse(poly_pointer *ptr) { /* erase the polynomial pointed to by ptr */ poly_pointer temp; while (*ptr) { temp = *ptr; *ptr = (*ptr)->link; free(temp); } Erasing complexity: O(n), n: nonzero terms not efficient!! 3 14 2 8 1 0 ptr null temp

Circular List Representation Representing Polynomials as Circularly Linked Lists 3 14 2 8 1 0 a The operation for erasing polynomial would be very efficient by maintaining our own list of freed node, i.e., free pool (see next slide)

Circular List Representation (cont.) Concept of polynomial erasing with free storage pool ptr 14 3 2 8 1 avail temp 1. temp = ptr->link; 2. ptr->link = avail; 3. avail = temp; p.159 Erasing complexity: Circularly List: O(1) Chain List: O(n), n: nonzero terms

STORAGE POOL The STORAGE POOL (available pool/space, free space pool) the set of all node, which can be allocated a free space available for a new node Method 1 Method 2 AV n storage pool AV

STORAGE POOL: Array Method Method 1: Array Method 1 2 n storage pool AV procedure GETNODE(I) if AV > n then call NO_MORE_NODE; I  AV; AV  AV + 1; end GETNODE

STORAGE POOL: Linked Method Method 2: Linked Method the set of all nodes not currently in use is linked together. Procedure GETNODE(x) if AV=0 then call NO_MORE_NODE; x  AV; AV  LINK(AV); end procedure RET(x) LINK(x)  AV; AV  x; end AV AV X

Circular List Representation with Head Problem with circular linked lists: How to represent zero polynomial? Solution: introducing an additional Head Node (1) Zero (need a head node) a -1 Zero polynomial (2) Non-zero Polynomial a exp -1 3 14 2 8 1 0

Circular with Head: d = a + b exp -1 3 14 2 8 1 0 b exp -1 8 14 -3 10 10 6 d=a+b exp -1 11 14 … 1 0 See p. 161, Program 4.16

Algorithm for adding two polynomials Adding Polynomials: Algorithm Comparison Algorithm for adding two polynomials Chain Data Structure Circularly with Head Node (C in Program 4.10, p.154) (C in Program 4.16, p.161) … // copy rest of a // copy rest of b … ??? when ??? …

4.5 Additional List Operations Linked Lists Operations create insert delete get_node ret_node invert concatenate

Chain Linked List: Invert Inverting a Chain first 1 2 3 7 NULL … first 7 6 5 1 NULL … list_pointer invert(list_pointer lead) { list_pointer middle, trail; middle = NULL; while (lead) { trail = middle; middle = lead; lead = lead->link; /* lead moves to next node */ middle->link = trail; /* link middle to preceding node */ } return middle; } p.164, Program 4.17

Chain Linked List: Invert (cont.) first 1 2 3 7 NULL lead middle … lead = first; middle = NULL; first 1 2 3 7 NULL … middle lead trail = middle; middle = lead trail NULL first 1 2 3 … 7 NULL middle lead lead = lead->link; middle->link = trail; trail NULL

Chain Linked List: Invert (cont.) trail first 1 2 3 7 NULL lead middle lead = lead->link; middle->link = trail; trail = middle; middle = lead … … …

Chain Linked List: Invert (cont.) first 1 2 3 … 7 NULL trail middle lead lead = lead->link; middle->link = trail; NULL . . . first 7 6 5 1 NULL … middle first = middle; lead NULL

Chain Linked List: Concatenate . . . this 1 2 3 7 NULL p . . . b 11 12 13 17 NULL this 1 2 3 7 p b 11 12 13 17 NULL . . .

Circular Linked Lists: Adding New Node Adding new node to front or rear Problem: move down the whole list when add an item to the front. first 1 2 3 7 . . . first 1 2 3 7 . . . New x first 1 2 3 7 . . . New x rear front

Circular Lists: Adding New Node (cont.) Circular lists: with pointer to last node . . . 1 2 3 7 last 1 2 3 7 . . . last New x Add to front x->link = last->link; last->link = x; Add to rear ???

Circular Linked Lists (cont.) Inserting at the front [Program 4.19, p.165] void insert_front(list_pointer *ptr, list_pointer node) { if (IS_EMPTY(*ptr) { /* empty List */ *ptr = node ; node->link = node ; } else { node->link = (*ptr)->link ; (*ptr)->link = node ; } } ptr node . . . 1 2 3 7 ptr node . . . 1 2 3 7 ptr

Linked List for Sparse Matrix Circular linked list representation of a sparse matrix has two types of nodes: head node: tag, down, right, and next entry node: tag, down, row, col, right, value Head node i is the head node for both row i and column i. down tag right down tag row col right f i j aij next value Head node Entry node Setup for aij A 4x4 sparse matrix

Linked List for Sparse Matrix (cont.) 4 2 1 3 -4 12 11 -15 H0 H1 H2 H3 Matrix head

Doubly Linked List Motivation: Problem of singly linked lists To efficiently delete a node, we need to know its preceding node It’s hard to find the node precedes a node ptr. Solution: using doubly linked list Each node in a doubly linked list has at least three fields left link field (llink) data field (item) right link field (rlink) llink data rlink

Doubly Linked List (cont.) A head node is also used to implement operations more easily. llink item rlink Head Node llink item rlink Empty List

Doubly Linked List (cont.) Deletion from a doubly linked circular list llink item rlink Head Node

Doubly Linked List (cont.) Insertion into a doubly linked circular list node node newnode