Please Note The Question Is To Be Written
Please Note The Question Is To Be Written
PROGRAM 15 //Program to create a Linked List and display it #include < fstream . h > #include < stdio . h > #include < conio . h > struct Node { int data ; Node * next ; }; Node *first , *last , *latestnode ; void cleanup ( ) { Node *ptr = first , *t ; While ( ptr ) { t = ptr -> next ; delete ptr ; ptr = t ; } cout << "Linked List Cleaned........." ; getch ( ) ; } void main ( ) { int n , i ; clrscr ( ) ; cout << "Enter the no of nodes to be created::" ; cin >> n ; for ( i = 0 ; i < n ; ++i ) { latestnode = new Node ; cout << "\nEnter the data to be stored:: " ; cin >> latestnode -> data ; latestnode -> next = NULL ; if ( latestnode == NULL ) { cout << "No memory::" ;
getch ( ) ; cleanup ( ) ; } If ( i == 0 ) first = last = latestnode ; else { last -> next = latestnode ; last = latestnode ; } } cout << "\nLinked List is .............\n" ; Node *ptr = first ; while ( ptr ) { cout << ptr ->data << endl ; ptr = ptr -> next ; } getch ( ) ; cleanup ( ) ; } Output Enter the no of nodes to be created::4 Enter the data to be stored::1 Enter the data to be stored::2 Enter the data to be stored::3 Enter the data to be stored::4 Linked List is ............. 1 2 3 4 Linked List Cleaned.........
Please note that the algorithm is to be WRITTEN on the ruled side of the record. Algorithm Step 1 Define a self referential structure Node with members int data , Node * next Step 2 Declare pointers Node *first , *last , *latestnode Step 3 Define function cleanup ( ) Node *ptr = first , *t Loop till ptr == NULL t = ptr -> next de allocate memory to which ptr is pointing to ptr = t Step 4 Accept no of nodes to be created from the user into n Step 5 For I =0, i<n , ++I Allocate memory for a new node, latestnode points to it Store data into this node If i = 0, then first = last = latestnode Else last -> next = latestnode last = latestnode Step 6 Print the Linked List Node *ptr = first Loop till ptr = NULL Print ptr ->data ptr = ptr -> next Step 7 Call function cleanup ( ) Step 8 Stop