0% found this document useful (0 votes)
11 views

Please Note The Question Is To Be Written

The document provides a C program to create a linked list and display it. The program defines a Node structure with data and next pointer fields. It declares pointers to traverse the list and allocates new nodes. The user inputs the number of nodes, and a for loop allocates each node and stores data. It links the nodes and prints the final list. A cleanup function is called at the end to deallocate the memory used by the linked list.

Uploaded by

ananthcjayan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Please Note The Question Is To Be Written

The document provides a C program to create a linked list and display it. The program defines a Node structure with data and next pointer fields. It declares pointers to traverse the list and allocates new nodes. The user inputs the number of nodes, and a for loop allocates each node and stores data. It links the nodes and prints the final list. A cleanup function is called at the end to deallocate the memory used by the linked list.

Uploaded by

ananthcjayan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Please note the question is to be written Q. Write a program to create a Linked List and display it.

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

You might also like