Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright 2000-2009 Networking Laboratory Chapter 4. LISTS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.

Similar presentations


Presentation on theme: "Copyright 2000-2009 Networking Laboratory Chapter 4. LISTS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer."— Presentation transcript:

1 Copyright 2000-2009 Networking Laboratory Chapter 4. LISTS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer Science Press, 2008 Fall 2009 Course, Sungkyunkwan University Hyunseung Choo choo@ece.skku.ac.kr

2 Fall 2009 Data Structures Singly Linked Lists  compose of data part and link part  link part contains address of the next element in a list  non-sequential representations  size of the list is not predefined  dynamic storage allocation and deallocation Networking Laboratory 2/57 batsatcatvat NULL ptr

3 Fall 2009 Data Structures Singly Linked Lists To insert the word mat between cat and sat 1) get a currently unused node (paddr) 2) set paddr’s data to mat 3) set paddr’s link to point to the address found in the link of the node cat 4) set the link of the node cat to point to paddr Networking Laboratory 3/57 batsatcatvat NULL ptr mat

4 Fall 2009 Data Structures Singly Linked Lists To delete mat from the list 1) find the element that immediately precedes mat, which is cat 2) set its link to point to mat’s link  no data movement in insert and delete operation Networking Laboratory 4/57 batsatcatvat NULL ptr mat

5 Fall 2009 Data Structures Singly Linked Lists Ex 4.1 [list of words ending in at]  define a node structure for the list  data field: character array  link field: pointer to the next node  self-referential structure typedef struct list_node *list_ptr; typedef struct list_node { char data[4]; list_ptr link; }; list_ptr ptr = NULL; Networking Laboratory 5/57

6 Fall 2009 Data Structures Singly Linked Lists Create a new node for our list then place the word bat into our list ptr=(list_ptr)malloc(sizeof(list_node)); strcpy(ptr->data,”bat”); ptr->link=NULL; Networking Laboratory 6/57 bat\0NULL ptr address of first node ptr->dataptr->link

7 Fall 2009 Data Structures Singly Linked Lists Ex 4.2 [two-node linked list]  create a linked list of integers typedef struct list_node *list_ptr; typedef struct list_node { int data; list_ptr link; }; list_ptr ptr = NULL; 1020 NULL ptr Networking Laboratory 7/57

8 Fall 2009 Data Structures Singly Linked Lists list_ptr create2() { list_ptr first, second; first = (list_ptr)malloc(sizeof(list_node)); second = (list_ptr)malloc(sizeof(list_node)); second->link=NULL; second->data=20; first->data=10; first->link=second; return first; } Networking Laboratory 8/57

9 Fall 2009 Data Structures Singly Linked Lists Ex 4.3 [list insertion]  determine if we have used all available memory: IS_FULL #define IS_FULL(ptr) (!(ptr))  Function call: insert(&ptr, node); Networking Laboratory 9/57 1020 NULL ptr node 50 temp

10 Fall 2009 Data Structures Singly Linked Lists void insert (list_ptr *pptr,list_ptr node) { list_ptr temp; temp=(list_ptr)malloc(sizeof(list_node)); if(IS_FULL(temp)) { fprintf(stderr,”The momory is full\n”); exit(1); } temp->data=50; if (*pptr) { temp->link = node->link; node->link = temp; } else { temp->link = NULL; *pptr = temp; } Networking Laboratory 10/57

11 Fall 2009 Data Structures Singly Linked Lists Ex 4.4 [list deletion]  ptr: point to the start of list  node: point to the node to be deleted  trail: point to the node that precedes node to be deleted delete(&ptr,NULL,ptr); delete(&ptr,ptr,ptr->link); Networking Laboratory 11/57 1050 ptr 20 NULL node 50 ptr 20 NULL trail = NULL (b) after deletion(a) before deletion 1050 ptr 20 NULL trail 10 ptr 20 NULL node (b) after deletion(a) before deletion

12 Fall 2009 Data Structures Singly Linked Lists void delete(list_ptr *pptr, list_ptr trail, list_ptr node) { if (trail) trail->link = node->link; else *pptr = (*pptr)->link; free(node); }  delete(&ptr,NULL,ptr); delete(&ptr,ptr,ptr->link); Ex 4.5 [printing out a list] void print_list(list_ptr ptr) { printf(“The list contains: “); for(; ptr; ptr = ptr->link) printf(“%4d”, ptr->data); printf(“\n”); } Networking Laboratory 12/57

13 Fall 2009 Data Structures Dynamically Linked Stacks And Queues #define MAX_STACKS 10 /* n=MAX_STACKS=10 */ typedef struct { int key; /* other fields here */ } element; typedef struct stack *stack_ptr; typedef struct stack { element item; stack_ptr link; }; stack_ptr top[MAX_STACKS]; NULL top element link ······ NULL front element link ······ rear (a) linked stack (b) linked queue Networking Laboratory 13/57

14 Fall 2009 Data Structures Dynamically Linked Stacks And Queues ······ NULL top[0] element link ······key NULL top[MAX_STACKS-1] ······ initial condition for n stacks top[i] = NULL, 0 ≤ i < MAX_STACKS boundary conditions top[i]==NULL iff the ith stack is empty IS_FULL(temp) iff the memory is full Networking Laboratory 14/57

15 Fall 2009 Data Structures Dynamically Linked Stacks And Queues Add to a linked stack void push(stack_ptr *ptop, element item) { stack_ptr temp = (stack_ptr)malloc(sizeof (stack)); if(IS_FULL(temp)) { fprintf(stderr,”The memory is full\n”); exit(1); } temp->item=item; temp->link=*ptop; *ptop = temp; }  #define IS_FULL(ptr) (!(ptr))  push(&top[stack_no], item); Networking Laboratory 15/57

16 Fall 2009 Data Structures Dynamically Linked Stacks And Queues Delete from a linked stack element pop(stack_ptr *ptop) { stack_ptr temp = *ptop; element item; if(IS_EMPTY(temp)) { fprintf(stderr,”The stack is empty\n”); exit(1); } item=temp->item; *ptop=temp->link; free(temp); return item; }  #define IS_EMPTY(ptr) (!(ptr))  item=pop(&top[stack_no]); Networking Laboratory 16/57

17 Fall 2009 Data Structures Dynamically Linked Stacks And Queues #define MAX_QUEUES 10 /* m=MAX_QUEUES=10 */ typedef struct queue *queue_ptr; typedef struct queue { element item; queue_ptr link; }; queue_ptr front[MAX_QUEUES],rear[MAX_QUEUES]; NULL front element link ······ rear (b) linked queue Networking Laboratory 17/57

18 Fall 2009 Data Structures Dynamically Linked Stacks And Queues NULL front[0] element link ······ rear[0] key NULL front[MAX_QUEUES-1] ······ rear[MAX_QUEUES-1] ······ initial conditon for n queues front[i]=NULL, 0 £ i < MAX_QUEUES boundary conditions front[i]==NULL iff the ith queue is empty IS_FULL(temp) iff the memory is full Networking Laboratory 18/57

19 Fall 2009 Data Structures Dynamically Linked Stacks And Queues Add to the rear of a linked queue void addq(queue_ptr *pfront, queue_ptr *prear, element item) { queue_ptr temp = (queue_ptr)malloc(sizeof(queue)); if(IS_FULL(temp)) { fprintf(stderr,”The memory is full\n”); exit(1); } temp->item=item; temp->link=NULL; if (*pfront) (*prear)->link=temp; else *pfront = temp; *prear = temp; }  addq(&front[queue_no], &rear[queue_no], item); Networking Laboratory 19/57

20 Fall 2009 Data Structures Dynamically Linked Stacks And Queues Delete from the front of a linked queue element deleteq(queue_ptr *pfront) { queue_ptr temp=*pfront; element item; if (IS_EMPTY(*pfront)) { fprintf(stderr,”The queue is empty\n”); exit(1); } item=temp->item; *pfront=temp->link; free(temp); return item; }  item=deleteq(&front[queue_no]);  comparison: array vs. linked list Networking Laboratory 20/57

21 Fall 2009 Data Structures Polynomials Representing polynomials as singly linked lists  A(x) = a m-1 x e m-1 + ··· + a 0 x e 0 typedef struct poly_node *poly_ptr; typedef struct poly_node { int coef; int expon; poly_ptr link; }; poly_ptr a,b,d; poly_node a = 3x 14 + 2x 8 + 1 b = 8x 14 - 3x 10 + 10x 6 Networking Laboratory 21/57 coefexponlink 3142810 NULL a 814-310 6 NULL b

22 Fall 2009 Data Structures Polynomials Adding polynomials  (a) a->expon == b->expon Networking Laboratory 22/57 3142810 NULL 814-310 6 NULL ba 1114 NULL drear

23 Fall 2009 Data Structures Polynomials  (b) a->expon expon Networking Laboratory 23/57 3142810 NULL 814-310 6 NULL ba -310 NULL d 1114 rear

24 Fall 2009 Data Structures Polynomials  (c) a->expon > b->expon Networking Laboratory 24/57 3142810 NULL 814-310 6 NULL ba 28 rear 1114-310 d

25 Fall 2009 Data Structures Polynomials  (d) a->expon expon Networking Laboratory 25/57 3142810 NULL 814-310 6 NULL ba 281114-310 6 NULL rear d

26 Fall 2009 Data Structures Polynomials  (e) b == NULL; 3142810 NULL 814-310 6 NULL ba 281114-310 rear 10610 NULL d Networking Laboratory 26/57

27 Fall 2009 Data Structures poly_ptr padd(poly_ptr a,poly_ptr b) { poly_ptr front,rear,temp; int sum; rear=(poly_ptr)malloc(sizeof(poly_node)); if(IS_FULL(rear)) { fprintf(stderr,”The memory is full\n”); exit(1);} front = rear; while(a && b) switch(COMPARE(a->expon,b->expon)) { case -1: /* a->expon expon */ attach(b->coef,b->expon,&rear); b = b->link; break; case 0: /* a->expon = b->expon */ sum = a->coef + b->coef; if(sum) attach(sum,a->expon,&rear); a = a->link; b = b->link; break; case 1: /* a->expon > b->expon */ attach(a->coef,a->expon,&rear); a = a->link; } Networking Laboratory 27/57 Polynomials

28 Fall 2009 Data Structures Polynomials poly_ptr padd(poly_ptr a,poly_ptr b) { · (continued from the previous slide) for(; a; a=a->link) attach(a->coef,a->expon,&rear); for(; b; b=b->link) attach(b->coef,b->expon,&rear); rear->link = NULL; temp=front; front=front->link; free(temp); return front; } Networking Laboratory 28/57

29 Fall 2009 Data Structures Polynomials Function attach() to create a new node and append it to the end of d void attach(float coe, int exp, poly_ptr *pptr) { poly_ptr temp; temp=(poly_ptr)malloc(sizeof(poly_node)); if(IS_FULL(temp)) { fprintf(stderr,”The memory is full\n”); exit(1); } temp->coef = coe; temp->expon = exp; (*pptr)->link = temp; *pptr=temp; } Networking Laboratory 29/57

30 Fall 2009 Data Structures Polynomials Analysis of padd where  m, n : number of terms in each polynomial  coefficient additions:  O(min{m, n})  exponent comparisons:  O(m + n)  creation of new nodes for d  O(m + n) Time complexity:  O(m + n) Networking Laboratory 30/57

31 Fall 2009 Data Structures Polynomials Erasing polynomials void erase(poly_ptr *pptr) { poly_ptr temp; while (*pptr) { temp = *pptr; *pptr = (*pptr)->link; free(temp); }  useful to reclaim the nodes that are being used to represent partial result such as temp(x) Networking Laboratory 31/57

32 Fall 2009 Data Structures Polynomials Allocating/deallocating nodes  how to preserve free node in a storage pool?  initially link together all free nodes into a list in a storage pool  avail: variable of type poly_ptr that points to the first node in list of free nodes Networking Laboratory 32/57 NULL avail initial available space list ······ 12n storage pool

33 Fall 2009 Data Structures Polynomials Allocating nodes poly_ptr get_node(void) { poly_ptr node; if (avail) { node = avail; avail = avail->link; } else { node = (poly_ptr)malloc(sizeof(poly_node)); if (IS_FULL(node)) { fprintf(stderr,”The memory is full\n”); exit(1); } return node; } Networking Laboratory 33/57

34 Fall 2009 Data Structures Polynomials Deallocating nodes void ret_node(poly_ptr ptr) { ptr->link = avail; avail = ptr; } Networking Laboratory 34/57

35 Fall 2009 Data Structures Polynomials void erase(poly_ptr *pptr) { poly_ptr temp; while (*pptr) { temp = *pptr; *pptr = (*pptr)->link; ret_node(temp); } traverse to the last node in the list:  O(n) where n: number of terms how to erase polynomial efficiently? how to return n used nodes to storage pool? Networking Laboratory 35/57

36 Fall 2009 Data Structures Polynomials Representing polynomials as circularly linked list  to free all the nodes of a polynomials more efficiently  modify list structure  the link of the last node points to the first node in the list  called circular list (  chain) Networking Laboratory 36/57 ptr

37 Fall 2009 Data Structures Polynomials Maintain our own list (as a chain) of nodes that has been freed  obtain effective erase algorithm void cerase(poly_ptr *pptr) { if (*pptr) { temp = (*pptr)->link; (*pptr)->link = avail; avail = temp; *pptr = NULL; }  independent of the number of nodes in a list: O(1) Networking Laboratory 37/57

38 Fall 2009 Data Structures Polynomials Circular list with head nodes  handle zero polynomials in the same way as nonzero polynomials ptr head node -- ptr head node -- (empty list) Networking Laboratory 38/57

39 Fall 2009 Data Structures Operations for Chains Inverting (or reversing) a chain  “in place” by using three pointers  lead, middle, trail typedef struct list_node *list_ptr; typedef struct list_node { char data; list_ptr link; }; list_ptr invert(list_ptr lead) { list_ptr middle, trail; middle = NULL; while (lead) { trail = middle; middle = lead; lead = lead->link; middle->link = trail; } return middle; } time: O(length of the list) Networking Laboratory 39/57

40 Fall 2009 Data Structures Operations for Chains NULL leadmiddletrail NULL leadtrailmiddle NULL lead NULL middle Networking Laboratory 40/57

41 Fall 2009 Data Structures Operations for Chains NULL leadmiddletrail NULL leadmiddletrail NULL leadmiddletrail NULL Networking Laboratory 41/57

42 Fall 2009 Data Structures Operations for Chains Concatenates two chains  produce a new list that contains ptr1 followed by ptr2 list_ptr concat(list_ptr ptr1,list_ptr ptr2) { list_ptr temp; if (IS_EMPTY(ptr1)) return ptr2; else { if (!IS_EMPTY(ptr2)) { for (temp=ptr1; temp->link; temp=temp->link); temp->link = ptr2; } return ptr1; } Networking Laboratory 42/57

43 Fall 2009 Data Structures Operations for Chains Finding the length of a list(chain) int length(list_ptr ptr) { int count = 0; while (ptr) { count++; ptr = ptr->link; } return count; } Insert a new node at the front or at the rear of the chain  front-insert: O(1),  rear-insert: O(n) Networking Laboratory 43/57 ptrx1x2x3 NULL

44 Fall 2009 Data Structures Operations for Chains Insert a new node at the front of a list(chain) void insert_front(list_ptr *pptr, list_ptr node) { if (IS_EMPTY(*pptr)) { *pptr = node; node->link = NULL; } else { node->link = *pptr; *pptr = node; } Networking Laboratory 44/57

45 Fall 2009 Data Structures Operations for Circularly Linked Lists (singly) circular linked lists Insert a new node at the front or at the rear  move down the entire length of ptr to insert at both front and rear:  insert-front : O(n)  insert-rear : O(n) Networking Laboratory 45/57 ptr x1x2x3

46 Fall 2009 Data Structures Operations for Circularly Linked Lists improve this better:  make ptr points to the last node insert a new node at the front or at the rear  front-insert : O(1)  rear-insert : O(1) Networking Laboratory 46/57 ptrx1x2x3

47 Fall 2009 Data Structures Operations for Circularly Linked Lists Insert a new node at the front of a circular list void insert_front(list_ptr *pptr, list_ptr node) { if (IS_EMPTY(*pptr)) { *pptr = node; node->link = node; } else { node->link = (*pptr)->link; (*pptr)->link = node; *pptr = node; /* for rear-insert */ } Networking Laboratory 47/57

48 Fall 2009 Data Structures Operations for Circularly Linked Lists Finding the length of 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; } Networking Laboratory 48/57

49 Fall 2009 Data Structures Doubly linked lists Problems of singly linked lists  move to only one way direction  hard to find the previous node  hard to delete the arbitrary node Doubly linked circular lists  doubly lists + circular lists  allow two links  two way direction Networking Laboratory 49/57

50 Fall 2009 Data Structures Doubly linked lists typedef struct node *node_ptr; typedef struct node { node_ptr llink; element item; node_ptr rlink; };  suppose that ptr points to any node in a doubly linked list ptr = ptr->llink->rlink = ptr->rlink->llink Networking Laboratory 50/57

51 Fall 2009 Data Structures Doubly linked lists Introduce dummy node, called, head  to represent empty list  make easy to implement operations  contains no information in item field Empty doubly linked circular list with head node Networking Laboratory 51/57 ptr

52 Fall 2009 Data Structures Doubly linked lists doubly linked circular list with head node Networking Laboratory 52/57 head node llinkitemrlink

53 Fall 2009 Data Structures Doubly linked lists Insertion into a doubly linked circular list void dinsert(node_ptr node,node_ptr newnode) { /* insert newnode to the right of node */ newnode->llink = node; newnode->rlink = node->rlink; node->rlink->llink = newnode; node->rlink = newnode; }  time: O(1) Networking Laboratory 53/57

54 Fall 2009 Data Structures Doubly linked lists Insertion into an empty doubly linked circular list Networking Laboratory 54/57 newnode node

55 Fall 2009 Data Structures Doubly linked lists Deletion from a doubly linked circular list void ddelete(node_ptr node,node_ptr deleted) { /* delete from the doubly linked list */ if (node == deleted) printf(“Deletion of head node ” “not permitted.\n”); else { deleted->llink->rlink = deleted->rlink; deleted->rlink->llink = deleted->llink; free(deleted); }  time complexity : O(1) Networking Laboratory 55/57

56 Fall 2009 Data Structures Doubly linked lists Deletion in a doubly linked list with a single node Networking Laboratory 56/57 deleted node

57 Fall 2009 Data Structures Doubly linked lists Doubly linked circular list  don’t have to traverse a list : O(1)  insert(delete) front/middle/rear is all the same Networking Laboratory 57/57


Download ppt "Copyright 2000-2009 Networking Laboratory Chapter 4. LISTS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer."

Similar presentations


Ads by Google