C++ programm for Creation ,Insertion and Display of Singly Linked List
Download the Code
Code for Creation ,Insertion and Display of SLL
You can find the detail theory here
#include<iostream> using namespace std; int create(); void display(); int insert(); int search(); int del(); struct student { int id ; student *next; }; student * start = NULL, *temp = NULL; int main() { int option; char choise; cout<<"\n\n\t\tLINKED LIST OPERATIONS \n"; do { cout<<"\n\t\t 1.Create Linked List\n"<<"\n\t\t 2.Display Linked List\n"<<"\n\t\t 3.Add value at given position\n"<<"\n\t\t 4.Search\n"<<"\n\t\t 5.Delete\n"<<"\n\t\t 6. Exit"; cout<<"\n\n\tEnter Your Choise : "; cin>>option; switch(option) { case 1: cout<<"\n\t\tCreating Linked List \n"; create(); break; case 2: cout<<"\n\t\tDisplaying Linked list \n"; display(); break; case 3: cout<<"\n\t\tAdd at given position :\n"; insert(); break; case 4: search(); break; case 5: del(); break; } cout<<"\n\tWant more operations ? "; cin>>choise; }while(choise=='y'); } int create() { int no_stud,roll; cout<<"\n\tEnter the number of students : "; cin>>no_stud; for(int i=1;i<=no_stud;i++) { cout<<"\tEnter roll number :"; cin>>roll; // CREATING NEW NODE student *newNode = new student; newNode->id= roll; newNode->next = NULL; if(start ==NULL) { start = newNode; } else { temp =start; while(temp->next!= NULL) { temp = temp->next; } temp->next = newNode; temp = temp->next; } } } void display() { temp = start; int count =0; while(temp!=NULL) { cout<<"\t\t\t\t\t"<<temp->id<<"\n"; temp = temp->next; count++; } cout<<"\tTotal node count : "<<count<<"\n"; } int insert() { int pos,roll,i = 2; cout<<"\n\tEnter position and data : "; cin>>pos>>roll; temp = start; student *newNode = new student; newNode->id= roll; newNode->next = NULL; if(pos==1) { start = newNode; newNode ->next = temp; temp = newNode; cout<<"\n\tNode inserted succesfully at 1st position !"; } else { while(temp->next!=NULL && pos!=i) {
temp = temp->next;
i++;
} newNode ->next = temp->next ; temp->next = newNode; cout<<"\n\tNode inserted succesfully at "<<pos<<"\n"; } } int search() { int data,pos=1; cout<<"\n\tEnter roll number : "; cin>>data; temp = start; while(temp!=NULL && temp->id!=data) { temp =temp->next; pos++; } cout<<"\n\t Record found at position "<<pos; } int del() { student *a = NULL,*prev; int data; cout<<"Enter data "; cin>>data; temp = start; if(start->id==data) { a = temp; start = start->next; temp = start; delete(a); cout<<"Node deleted Succesfully from position 1 "; } else { while(temp!=NULL&&temp->id!=data) { prev = temp; temp = temp->next; } prev->next=temp->next; delete(temp); cout<<"Node deleted Succesfully "; } }
Linked list implementation using C++
Output :
LINKED LIST OPERATIONS
1.Create Linked List
2.Display Linked List
3.Add value at given position
4.Search
5.Delete
6. Exit
Enter Your Choise : 1
Creating Linked List
Enter the number of students : 2
Enter roll number :1
Enter roll number :2
Want more operations ? y
Comments