Data Structure in C Transparency No. 4-1 Copyright(c) 1997, Sungkyunkwan University Chapter #4: LISTS Fundamentals of Data Structure in C Horowitz, Sahni.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 4 Lists Pointers Singly Linked Lists
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.
CSCI2100B Linked List Jeffrey
Bioinformatics Programming
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
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.
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.
Introduction to Data Structure
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Introduction to Data Structures Systems Programming.
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
Polynomial Addition using Linked lists
 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.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
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.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
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 Lists EENG 212 ALGORITHMS And DATA STRUCTURES.
Linked List (Part I). Introduction  Weakness of storing an ordered list in array: Insertion and deletion of arbitrary elements are expensive. ○ Example:
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More Linking.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.
Linked List. LINKED LIST Link is collection of similar type of elements. There are two ways of maintaining a list in memory. The first way is store the.
Data Structure & Algorithms
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.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Pointers and Linked Lists
Lectures linked lists Chapter 6 of textbook
Elementary Data Structures
More Linking Up with Linked Lists
Data Structure Dr. Mohamed Khafagy.
CSCE 3110 Data Structures & Algorithm Analysis
UNIT-3 LINKED LIST.
Data Structures 7th Week
Stack and Queue APURBO DATTA.
EEE2108: Programming for Engineers Chapter 4. Linked Lists
Linked List Sudeshna Sarkar.
CSCE 3110 Data Structures & Algorithm Analysis
Linked List (Part I) Data structure.
Chapter 16 Linked Structures
General List.
Presentation transcript:

Data Structure in C Transparency No. 4-1 Copyright(c) 1997, Sungkyunkwan University Chapter #4: LISTS Fundamentals of Data Structure in C Horowitz, Sahni and Anderson-Freed Computer Science Press July, 1997

Data Structure in C Transparency No. 4-2 Copyright(c) 1997, Sungkyunkwan University Pointers array - sequential representation - some operation can be very time- consuming (data movement) - size of data must be predefined - static storage allocation and deallocation solution to the data movement in sequential representation - pointers: often called links

Data Structure in C Transparency No. 4-3 Copyright(c) 1997, Sungkyunkwan University Pointers for any type T in C - there is corresponding type pointer-to-T actual value of pointer type - an address of memory pointer operators in C - the address operator: & - the dereferencing(or indirection) operator: *

Data Structure in C Transparency No. 4-4 Copyright(c) 1997, Sungkyunkwan University Pointers dynamically allocated storage - C provides a mechanism, called a heap, for allocating storage at run-time int i,*pi; float f,*pf; pi = (int *)malloc(sizeof(int)); pf = (float *)malloc(sizeof(float)); *pi = 1024; *pf = 3.14; printf(“an integer=%d,a float=%f\n”,*pi,*pf); free(pi); free(pf);

Data Structure in C Transparency No. 4-5 Copyright(c) 1997, Sungkyunkwan University 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 batsatcatvat NULL ptr

Data Structure in C Transparency No. 4-6 Copyright(c) 1997, Sungkyunkwan University Singly Linked Lists to insert the word mat between cat and sat 1)get a node currently unused(paddr) 2)set the data of this node 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 batsatcatvat NULL ptr mat

Data Structure in C Transparency No. 4-7 Copyright(c) 1997, Sungkyunkwan University 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 batsatcatvat NULL ptr mat

Data Structure in C Transparency No. 4-8 Copyright(c) 1997, Sungkyunkwan University Singly Linked Lists required capabilities to make linked representations possible - a mechanism for defining a node’s structure, that is, the field it contains - a way to create new nodes when we need them - a way to remove nodes that we no longer need

Data Structure in C Transparency No. 4-9 Copyright(c) 1997, Sungkyunkwan University 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;

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Singly Linked Lists create new nodes for out list then place the word bat into our list ptr=(list_ptr)malloc(sizeof(list_node)); strcpy(ptr->data,”bat”); ptr->link=NULL; bat\0NULL ptr address of first node ptr->dataptr->link

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Singly Linked Lists list_pointer create2() { list_pointer 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; }

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Singly Linked Lists  Ex 4.3 [list insertion] determine if we have used all available memory: IS_FULL 1020 NULL ptr node 50 temp

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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; }

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Singly Linked Lists  Ex 4.4 [list deletion] deletion depends on the location of the node: more complicated assumption - 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

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Singly Linked Lists 1) the node to be deleted is the first node in the list 2) otherwise 1050 ptr 20 NULL trail 10 ptr 20 NULL node (b) after deletion(a) before deletion 1050 ptr 20 NULL node 50 ptr 20 NULL trail = NULL (b) after deletion(a) before deletion

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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); }

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Singly Linked Lists  Ex 4.5 [printing out a list] while not end of the list do print out data field; move to the next node; end; void print_list(list_ptr ptr) { printf(“The list contains: “); for(; ptr; ptr = ptr->link) printf(“%4d”, ptr->data); printf(“\n”); }

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Dynamically Linked Stacks And Queues representing stack / queue by linked list NULL top element link ······ NULL front element link ······ rear (a) linked stack (b) linked queue

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Dynamically Linked Stacks And Queues representing n stacks by linked lists #define MAX_STACKS 10 /* n=MAX_STACKS=10 */ typedef struct { int key; /* other fields */ } element; typedef struct stack *stack_ptr; typedef struct stack { element item; stack_ptr link; }; stack_ptr top[MAX_STACKS];

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Dynamically Linked Stacks And Queues ······ NULL top[0] element link ······key NULL top[1] ······ NULL top[MAX_STACKS-1] ······

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Dynamically Linked Stacks And Queues initial condition for stacks top[i] = NULL, 0  i < MAX_STAKCS boundary condition for n stacks - empty condition: top[i] = NULL iff the ith stack is empty - full condition: IS_FULL(temp) iff the memory is full

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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; }

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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; }

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Dynamically Linked Stacks And Queues representing m queues by linked lists #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];

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Dynamically Linked Stacks And Queues NULL front[0] element link ······ rear[0] key NULL front[1] ······ rear[1] NULL front[MAX_QUEUES-1] ······ rear[MAX_QUEUES-1] ······

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Dynamically Linked Stacks And Queues initial conditon for queues front[i]=NULL,0  i < MAX_QUEUES boundary condition for queues - empty condition: front[i]= NULL iff the ith queue is empty - full condition: IS_FULL(temp) iff the memory is full

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Dynamically Linked Stacks And Queues add to the rear of a linked queue void insert(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; }

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Dynamically Linked Stacks And Queues delete from the front of a linked queue element delete(queue_ptr *pfront) { queue_ptr temp=*pfront; element item; if (IS_EMPTY(*front)) { fprintf(stderr,”The queue is empty\n”); exit(1); } item=temp->item; *pfront=temp->link; free(temp); return item; }

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Dynamically Linked Stacks And Queues representing stacks/queues by linked lists - no data movement is necessary : O(1) - no full condition check is necessary - size is growing and shrinking dynamically

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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;

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Polynomials poly_node a = 3x x b = 8x x x 6 coefexponlink NULL a NULL b

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Polynomials adding polynomials (a) a->expon == b->expon NULL NULL ba 1114 NULL drear

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Polynomials (b) a->expon expon NULL NULL ba -310 NULL d 1114 rear

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Polynomials (c) a->expon > b->expon NULL NULL ba 28 rear d

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Polynomials (d) a->expon expon NULL NULL ba NULL rear d

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Polynomials (e) b == NULL; NULL NULL ba rear NULL d

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Polynomials 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;  (to be continued)

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Polynomials  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; }  (to be continued)

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Polynomials 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; }

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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; }

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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)

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Polynomials erasing polynomials e(x) = a(x) * b(x) + d(x) 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);

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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)

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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 freed nodes NULL avail initial available space list ······ 12n storage pool

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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; }

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Polynomials Deallocating nodes void ret_node(poly_ptr ptr) { ptr->link = avail; avail = ptr; }

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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 term - how to erase polynomial efficiently? how to return n used nodes to storage pool ?

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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) ptr

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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)

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Polynomials circular list with head nodes - handle zero polynomials in the same way as nonzero polynomials ptr head node -- ptr head node -- (empty list)

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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; };

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Operations for Chains Inverting a chain 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)

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Operations for Chains NULL leadmiddletrail NULL leadtrailmiddle NULL lead NULL middle

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Operations for Chains NULL leadmiddletrail NULL leadmiddletrail NULL leadmiddletrail NULL

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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; }

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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; }

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Operations for Chains insert a new node at the front or at the rear of the chain - move down the entire length of ptr to insert rear: front-insert : O(1) rear-insert : O(n) ptrx1x2x3 NULL

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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; }

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Operations for Chains Insert a new node at the front of a list(chain) void insert_front(list_ptr *pptr, list_ptr node) { node->link = *pptr; *pptr = node; } Insert a new node at the rear of a list(chain) - exercise

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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 both front and rear: insert-front : O(n) insert-rear : O(n) ptr x1x2x3

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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) ptrx1x2x3

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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 */ }

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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; }

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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 list - doubly lists + circular lists - allow two links - two way direction

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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 ptr

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Doubly linked lists doubly linked circular list with head node llinkitemrlink

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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)

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Doubly linked lists insertion into an empty doubly linked circular list newnode node

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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)

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University Doubly linked lists deletion in a doubly linked list with a single node deleted node

Data Structure in C Transparency No Copyright(c) 1997, Sungkyunkwan University 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