/* CH4PR1.C: Program to maintain a linked list */ #include <stdio.h> #include <conio.h> #include <alloc.

h> /* structure containing a data part and link part */ struct node { int data ; struct node * link ; } ; void append ( struct node **, int ) void addatbeg ( struct node **, int void addafter ( struct node *, int, void display ( struct node * ) ; int count ( struct node * ) ; void delete ( struct node **, int ) ; ) ; int ) ; ;

void main( ) { struct node *p ; p = NULL ; /* empty linked list */ printf append append append append append ( ( ( ( ( ( "\nNo. &p, 14 &p, 30 &p, 25 &p, 42 &p, 17 of elements in the Linked List = %d", count ( p ) ) ; ) ; ) ; ) ; ) ; ) ;

display ( p ) ; addatbeg ( &p, 999 ) ; addatbeg ( &p, 888 ) ; addatbeg ( &p, 777 ) ; display ( p ) ; addafter ( p, 7, 0 ) ; addafter ( p, 2, 1 ) ; addafter ( p, 5, 99 ) ; display ( p ) ; printf ( "\nNo. of elements in the Linked List = %d", count ( p ) ) ; delete ( &p, 99 ) ; delete ( &p, 1 ) ; delete ( &p, 10 ) ; display ( p ) ; printf ( "\nNo. of elements in the Linked List = %d", count ( p ) ) ; } /* adds a node at the end of a linked list */ void append ( struct node **q, int num ) { struct node *temp, *r ;

r -> data = num . i < loc . } else { temp = *q . int num ) { struct node *temp . } } /* adds a new node at the beginning of the linked list */ void addatbeg ( struct node **q. *q = temp .if ( *q == NULL ) /* if the list is empty. /* go to last node */ while ( temp -> link != NULL ) temp = temp -> link . return . r -> data = num . create first node */ { temp = malloc ( sizeof ( struct node ) ) . temp -> link = *q . r -> link = NULL . /* add new node */ temp = malloc ( sizeof ( struct node ) ) . temp -> data = num . int num ) { struct node *temp. temp -> link = r . } /* adds a new node after the specified number of nodes */ void addafter ( struct node *q. temp -> link = NULL . /* add node at the end */ r = malloc ( sizeof ( struct node ) ) . temp = q . /* if end of linked list is encountered */ if ( temp == NULL ) { printf ( "\nThere are less than %d elements in list". temp -> data = num . int i . lo c ) . . /* skip to desired portion */ for ( i = 0 . i++ ) { temp = temp -> link . } } /* insert new node */ r = malloc ( sizeof ( struct node ) ) . *r . int loc. *q = temp .

temp -> link = r . } /* displays the contents of the linked list */ void display ( struct node *q ) { printf ( "\n" ) . *temp .r -> link = temp -> link . /* deletes the intermediate nodes in the linked list */ else old -> link = temp -> link . /* traverse the entire linked list */ while ( q != NULL ) { printf ( "%d ". } /* traverse the linked list till the last node is reached */ else { . c++ . q = q -> link . } /* deletes the specified node from the linked list */ void delete ( struct node **q. /* free the memory occupied by the node */ free ( temp ) . return . } } /* counts the number of nodes present in the linked list */ int count ( struct node * q ) { int c = 0 . int num ) { struct node *old. /* traverse the entire linked list */ while ( q != NULL ) { q = q -> link . q -> data ) . } return c . while ( temp != NULL ) { if ( temp -> data == num ) { /* if node to be deleted is the first node in the linked list */ if ( temp == *q ) *q = temp -> link . temp = *q .

num ) . } . /* go to the next node */ } } printf ( "\nElement %d not found". /* old points to the previous node */ temp = temp -> link .old = temp .

Sign up to vote on this title
UsefulNot useful