Nov
28
2008
28
2008
C++ Program to implement a single linked list
C++ Program to implement a single linked list and Perform the following operations.
/************************************************************** Author: Arun Vishnu M V Web: www.arunmvishnu.com Description: C++ Program to implement a single linked list and Perform the following operations 1: Insertioin 2: Deletion 3: Display ***************************************************************/ #include<conio.h> #include<iostream.h> #include<process.h> // Creating a NODE Structure struct node { int data; // data struct node *next,*start; // link to next node and previous node }; // Creating a class LIST class list { struct node *start; public: void create(); // to create a list void insert(); // insertion void del(); // deletion void show(); // show }; // Creating a new node void list::create() { struct node *nxt_node,*pre_node; int value,no,i; start=nxt_node=pre_node=NULL; cout<<"\nCREATING A NEW LIST.........\n\nHow many nodes : "; cin>>no; for(i=1;i<=no;i++) { cout<<"Enter "<<no<<" DATAs: "; cin>>value; nxt_node=new node; if(pre_node!=NULL) pre_node->next=nxt_node; if(start==NULL) start=nxt_node; nxt_node->data=value; nxt_node->next=NULL; pre_node=nxt_node; } cout<<"\nThe list is created!"; show(); } // Displaying LIST void list::show() { int data; struct node *ptr=start; cout<<"\n\nThe List is \n"; while(ptr!=NULL) { data=ptr->data; cout<<data<<" -> "; ptr=ptr->next; } cout<<"\b\b\b "; getch(); } // Insert node at any position void list::insert() { int position,dat; struct node *ptr_b,*ptr_f,*ptr,*nxt_node,*pre_node; cout<<"\nInsertion of a new node \n"; cout<<"Enter the DATA after which the new node is to be inserted.\n"; cout<<"[ If the data is not found the new node will be"; cout<<" created at first ]\n\t->: "; cin>>position; cout<<"Enter the data to insert: "; cin>>dat; ptr_b=start; ptr=new node; ptr->data=dat; if(start!=NULL) { while(ptr_b->next!=NULL && ptr_b->data!=position) { ptr_b=ptr_b->next; } } else { clrscr(); start=nxt_node=pre_node=NULL; nxt_node=new node; if(pre_node!=NULL) pre_node->next=nxt_node; if(start==NULL) start=nxt_node; nxt_node->data=dat; nxt_node->next=NULL; pre_node=nxt_node; return; } if(ptr_b->next==NULL && ptr_b->data!=position) //Insertion at first { ptr->next=start; start=ptr; } else if(ptr_b->next==NULL && ptr_b->data==position) //insertion at the end of list { ptr_b->next=ptr; ptr->next=NULL; } else //Insertion between two nodes { ptr_f=ptr_b->next; ptr->next=ptr_b->next; ptr_b->next=ptr; } cout<<"\nNew node is inserted!!"; getch(); } //End of insertion //Delete node from any position void list::del() { int position,dat; struct node *ptr_b,*ptr_f,*ptr,*pntr; cout<<"Enter the data to be beleted: "; cin>>position; ptr=start; while(ptr->next!=NULL && ptr->data!=position) { pntr=ptr; ptr=ptr->next; } if(ptr->next==NULL && ptr->data!=position) //Data not found { cout<<"\nData not found!!"; getch(); return; } else if(ptr->next==NULL && ptr->data==position) //Deletion from the end of list { ptr_b=pntr; if(pntr->next)// Only one node start=NULL; dat=ptr->data; ptr_b->next=NULL; } else //Deletion between two nodes or first node { dat=ptr->data; if(start==ptr) // delete first node { start=ptr->next; ptr_f=start; } else // Deletion between two nodes { dat=ptr->data; ptr_b=pntr; ptr_b->next=ptr->next; //ptr_f=ptr_b->next; } } delete ptr; cout<<"\nThe node is deleted!!\nData= "<<dat; getch(); } //End of deletion // Main function int main() { clrscr(); list l; l.create(); // to create a new node int choice; while(1) { cout<<"\n-------------------------------------------"; cout<<"\n\t\t LINKED LIST\n\n"; cout<<"1:Insertion\n2:Deletion\n3:Display List\n4:Exit"; cout<<"\nEnter your choice(1-4): "; cin>>choice; switch(choice) { case 1: // Insertion l.insert(); break; case 2: // Deletion l.del(); break; case 3: // Display l.show(); break; case 4: // Exit exit(0); break; default: cout<<"Please enter correct choice(1-4)!!"; getch(); break; } } return 0; } //---------------------- END--------------------
Related Posts
Latest Posts
- What is the real meaning of Facebook “Like”?
- Wish you and your family a very Happy and Prosperous New Year 2012!
- Import Orkut’s albums to Google+
- Download 100 Paid Android Apps(Like Fruit Ninja HD, BW, Smart Office etc. )For Free
- List Of Samsung Devices Getting Official Android 4.x Ice Cream Sandwich Update
- How to Restrict Facebook friends using lists
- How to clean iPhone/iPad/iPod backup files
- Apple’s Siri On iPhone 4S vs Microsoft’s Tellme On Windows Phone
- Your Facebook account hacked? Here’s how to fix it
- Indian Railway Enabled VRM System to travel without the printout of e-ticket
Tags
apple
c and cpp
Cheats
download
downloads
Entertainment
error
Film
friendship
Fun
gadgets
Games
gmail
google
greetings
india
Internet
iphone
java script
linux
love
mac
microsoft
mobile
News
office
onam
Personal
photos
Programming
review
Security
software
source code
technology
Tips & Tricks
video
videos
virus
web
web development
windows
windows 7
worm
yahoo
Recent Comments
- salman on C++ Program Program for Binary Search.
- Mithun on C++ Program to implement a QUEUE using linked list
- The Tamale Chica Chronicles has moved! | Tamale Chica Chronicles on Get redirection script
- Aparajita Chaudhuri on C++ Program implement a stack using an array
- Harta on Disable Adobe CS4 Activation Check

An article by
sir,
am doin my BE CSE..i did not understand the programs…plz explain line by line in detail…plz sir plz sir plz sir..i’ll be very grateful to u..