Linked LISTS q sequential representation of ordered lists

  • Slides: 16
Download presentation
Linked LISTS q sequential representation of ordered lists – adequate for many operations including

Linked LISTS q sequential representation of ordered lists – adequate for many operations including insertion or deletion of elements from a stack or queue. – operations such as insertion and deletion of arbitrary elements become expensive. q linked representation of ordered lists – successive items of a list may be placed anywhere in memory. – associated with each list element is a node which contains both a data component and a pointer, called link, to the next item in the list. data 1 node data 2 datan Ù

q Dynamically Allocated Storage – malloc and free /* <alloc. h> */ int i,

q Dynamically Allocated Storage – malloc and free /* <alloc. h> */ int i, *pi; float f, *pf; pi = (int *) malloc(sizeof(int)); pf = (float *) malloc(sizeof(float)); *pi = 1024; *pf = 3. 14; printf(“n integer = %d, a float = %fn”, *pi, *pf); free(pi); free(pf);

Singly linked lists q Linked lists representation (1) the nodes do not reside in

Singly linked lists q Linked lists representation (1) the nodes do not reside in sequential locations (2) the locations of the nodes may change on different runs ptr bat cat sat vat NULL

(ex) Insertion and Deletion in linked lists ptr bat cat sat vat NULL mat

(ex) Insertion and Deletion in linked lists ptr bat cat sat vat NULL mat ptr bat cat mat sat vat NULL

q Creation of a list typedef struct list_node *list_pointer; typedef struct list_node { char

q Creation of a list typedef struct list_node *list_pointer; typedef struct list_node { char data[4]; list_pointer link; }; list_pointer ptr = NULL; ptr = (list_pointer)malloc(sizeof (list_node)) Ÿ Ÿ Ÿ self-referential structure create a pointer to a type that does not get exist NULL: defined in stdio. h(K&RC) or stddef. h(ANSI C)

(ex) Insertion (1) Two node list ptr 10 20 NULL (2) Insert a new

(ex) Insertion (1) Two node list ptr 10 20 NULL (2) Insert a new node temp after a node : insert(&ptr, node) ptr 10 20 NULL node 50 temp

List_pointer create 2( ) { /* create a linked list with two nodes */

List_pointer create 2( ) { /* create a linked list with two nodes */ 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; } Program 4. 2 : create a two-node list

(ex) Deletion q delete(ptr, trail, node) : delete node from the list ptr, where

(ex) Deletion q delete(ptr, trail, node) : delete node from the list ptr, where trail points to the node that precedes node (1) delete(&ptr, NULL, ptr) ptr node trail = NULL 10 50 ptr 20 NULL (a) before deletion 50 20 NULL (b) after deletion (2) delete(&ptr, ptr->link) ptr trail 10 node 50 (a) before deletion ptr 20 NULL 10 20 NULL (b) after deletion

void insert (list_pointer *ptr , list_pointer node) { /* insert a new node with

void insert (list_pointer *ptr , list_pointer node) { /* insert a new node with data =50 into the list ptr after node*/ List_pointer temp; temp=(list_pointer)malloc (sizeof(list_node)); if (IS_FULL(temp)) { fprintf(stderr, ” The memory is fulln”); exit(1); } temp -> data =50; if (*ptr) { temp -> link =node -> link; node ->link=temp; } else { temp->link =NULL; *ptr = temp; } q } Program 4. 3 simple insert into front of list

void delete(list_pointer *ptr , list-pointer trail , list-pointer node) { /* delete node from

void delete(list_pointer *ptr , list-pointer trail , list-pointer node) { /* delete node from the list , trail is the preceding node ptr is the head of the list */ if (trail) trail -> link =node -> link ; else *ptr = (*ptr) - > link ; free(node); } program 4. 4 : deletion from a list

void print_list (list_pointer ptr) { printf (“ The list contains : “ ); for

void print_list (list_pointer ptr) { printf (“ The list contains : “ ); for (; ptr = ptr - > link) Printf(“%4 d “ , ptr -> data); printf (“n”); } q program 4. 5 : Printing a list

Doubly Linked Lists We can move easily only in the direction of the links

Doubly Linked Lists We can move easily only in the direction of the links in singly linked list. q To move in either direction, it is useful to have doubly linked lists. q typedef struct node *node_pointer; typedef struct node { node_pointer llink; element item; node_pointer rlink; } ptr = ptr->llink->rlink = ptr->rlink->llink

(ex) Doubly linked circular list llink item rlink Head Node ptr

(ex) Doubly linked circular list llink item rlink Head Node ptr

(ex) Insertion and Deletion node new node deleted

(ex) Insertion and Deletion node new node deleted

void dinsert ( node_pointer node, node_pointer newnode ) { /* insert newnode to the

void dinsert ( node_pointer node, node_pointer newnode ) { /* insert newnode to the right of node */ newnode -> llink = node ; newnode -> rlink = node -> rlink ; node -> rlink -> llink = newnode ; node -> rlink = newnode ; }

void ddelete ( node_pointer node, node_pointer deleted ) { /* delete from the doubly

void ddelete ( node_pointer node, node_pointer 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 ) ; } }