600 likes | 887 Views
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.
E N D
Introduction to Data Structure CHAPTER 4 LINKED LISTS 4.1 Pointers 4.2 Singly Linked Lists 4.3 Dynamic Linked Stacks and Queues4.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
Pointerin C inti, *pi; pi = &i; i=10 or *pi=10 pi = malloc(sizeof(int)); /* assign to pi a pointer to int */ i pi pi 4.1 Pointer inti, *pi; Some integer i pi 10
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 insert D insert 1 2 3 4 5 6 7 head=1 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 delete 5 1 2 3 4 5 6 7 delete G
G head …… 1 A 2 C Y 0 6 1 2 head 7 1 A 2 C 3 E 2 3 1 D 3 7 Singly Linked Lists: Arrow Notation • Arrow notation • Insert “D” • Delete “G”
Singly Linked Lists vs. Array • 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;}; • 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;}; list_nodea • Another possible configuration list_nodeb
NULL ptr ptr 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 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 list_node
first 10 10 0 20 NULL 20 NL p.143 20 20 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; } first 20 0
ptr 50 NULL temp node p.144 10 . . . ptr 20 NULL temp 50 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; }} Example: Inserting a Node /* insert a new node with data = 50 into list ptr after node */
10 20 NULL 50 trail node ptr p.145 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 */} Example: Deleting a Node /* delete the node after trail */ How about if trail = NULL?
CAT Delete a Node: A Dangling Reference • A dangling reference trail ptr MAT BAT VAT NULL SAT dangling reference
top front element link element link NULL NULL (a) Linked Stack (b) Linked Queue rear F R 4.3 Linked Stacks and Queues • Stacks and Queues in Linked lists Note: Compare with sequence queue
top 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]; 0 top i.e. NULL = 0 null value
rear front 0 front 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
temp top p.149 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 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; • } temp top
p.151 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 rear front
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; • } temp rear front
term: represented by a node 4.4 Polynomials • Consider a polynomial • Tackle the reasonable-sized problem • Node structure • e.g. A = 3x14 + 2x8 + 1 Note: in Array A ^
poly coef exp link p.152 Polynomial Representation • 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;
a null 1 0 3 14 2 8 b null 8 14 -3 10 10 6 Polynomial Representation: Example • Examples
d Adding Polynomials : d = a + b • Adding Polynomials: Figure 4:12 d = a + b Case 1: a->exp= b->exp a 2 8 314 1 0 a b -3 10 10 6 814 b d 11 14
a 2 8 1 0 3 14 a b -3 10 8 14 10 6 b d -3 10 11 14 d Adding Polynomials : d = a + b(cont.) Case 2: a->exp<b->exp
d Adding Polynomials: d = a + b(cont.) Case 3: a->exp>b->exp 2 8 1 0 3 14 a 8 14 -3 10 10 6 b -3 10 11 14 2 8
Algorithm for adding two polynomials Adding Polynomials: Algorithm (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 extreme case 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 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?
null 1 0 ptr 3 14 2 8 temp C program Erasing Polynomials (cont.) 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!!
a 2 8 1 0 3 14 Circular List Representation • Representing Polynomials as Circularly Linked Lists • The operation for erasing polynomial would be very efficient by maintaining our own list of freed node, i.e., free pool (see next slide)
avail 3 ptr 1 0 14 8 3 2 1 temp 2 avail p.159 Circular List Representation (cont.) • Concept of polynomial erasing with free storage pool 1. temp = ptr->link; 2. ptr->link = avail; 3. avail = temp; Erasing complexity:Circularly List: O(1) Chain List: O(n), n: nonzero terms
AV AV storage pool n 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 1Method 2
STORAGE POOL: Array Method • Method 1: Array Method 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 Methodthe 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) -1 a Zero polynomial (2) Non-zero Polynomial a exp -1 2 8 1 0 3 14
Circular with Head: d = a + b a exp -1 2 8 1 0 3 14 b exp -1 -3 10 10 6 8 14 d=a+b exp -1 … 1 0 11 14 See p. 161, Program 4.16
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
p.164, Program 4.17 7 1 NULL NULL first first 5 3 2 6 1 7 Chain Linked List: Invert 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;} • Inverting a Chain … …
lead middle NULL 7 7 NULL NULL first 3 3 2 2 first 1 1 Chain Linked List: Invert(cont.) … lead = first; middle = NULL; … middle lead trail = middle; middle = lead trail NULL 7 NULL first … 3 2 1 middle lead = lead->link;middle->link = trail; lead trail NULL
7 NULL first 3 2 1 lead middle trail = middle; middle = lead NULL trail trail trail middle 7 NULL first 3 2 1 lead = lead->link;middle->link = trail; NULL lead 7 NULL first 3 2 1 lead middle trail = middle; middle = lead NULL Chain Linked List: Invert (cont.) … … …
trail middle middle lead 1 NULL first 5 6 7 Chain Linked List: Invert (cont.) … 7 first 3 2 NULL 1 lead = lead->link;middle->link = trail; NULL . . . … first = middle; NULL lead
p 17 17 NULL NULL b b 13 13 12 12 11 11 . . . 7 this 3 2 1 p 7 NULL this 3 2 1 . . . Chain Linked List: Concatenate . . . . . .
. . . 7 first 3 2 1 . . . 7 first 3 2 1 0 New x rear front New x . . . 0 7 3 2 first 1 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.
New x . . . last 7 3 2 1 0 Circular Lists: Adding New Node (cont.) • Circular lists: with pointer to last node . . . last 7 3 2 1 Add to front x->link = last->link;last->link = x; Add to rear ???