#include<iostream> using namespace std; class node { public: int data; node *next; node() { data=0; next=0; } node(int value,node *n=0) { data=value; next=n; } }; class link { public: node *head; link() { head=0; } void insert_last() { int value; cout<<"Enter Value="; cin>>value; //node *n; if(head==0) { head=new node(value); head->next=head; } else { node *tmp; tmp=head; while(tmp->next!=head->next) { tmp=tmp->next; } tmp->next=new node(value); tmp->next->next=head; } } void insert_first() { int value; node *n; cout<<"Enter a value="; cin>>value; if(head==0) { n=new node(value); n->next=head; head=n; } else { head=new node(value,head); } } void insert_pos_before() { int value,pos; cout<<"Enter Position to Insert="; cin>>pos; cout<<"Enter Element="; cin>>value; node *tmp=head; node *n=new node(value); if(pos==1) { head=new node(value,head); } else { for(int i=1;i<(pos-1);i++) { tmp=tmp->next; n->next=tmp->next; tmp->next=n; } } } void insert_pos_after() { int value,pos; cout<<"Enter Position to insert="; cin>>pos; cout<<"Enter Value="; cin>>value; node *n=new node(value); node *tmp=head; for(int i=1;i<pos;i++)> { tmp=tmp->next; n->next=tmp->next; tmp->next=n; } } void del_first() { if(head==0) { cout<<"List is Empty"<<endl; } else { head=head->next; } cout<<"First Node Deleted...!"<<endl; } void del_last() { if(head==0) { cout<<"List is Empty"<<endl; } else { node *tmp=head; while(tmp->next->next!=0) { tmp=tmp->next; } tmp->next=0; cout<<"Last Node Deleted...!"<<endl; } } void del_pos() { int p; if(head==0) { cout<<"List is Empty"<<endl; } else { node *tmp=head; cout<<"Insert Position="; cin>>p; for(int i=1;i<(p-1);i++) { tmp=tmp->next; } tmp->next=tmp->next->next; } cout<<"Node Deleted...!"<<endl; } void display() { node *tmp=head; cout<<"Address of Head="<<head<<endl; cout<<endl; if(head==NULL) { cout<<"List is Empty...!"<<endl; } else { while(tmp->next!=head) { cout<<"Value:"<<tmp->data<<endl; tmp=tmp->next; } cout<<"Value:"<<tmp->data<<" "<<"Next Address="<<tmp->next<<endl; } } void update() { int p; int value; if(head==0) { cout<<"List is Empty"<<endl; } else { node *tmp=head; cout<<"Insert Position of node="; cin>>p; cout<<"Insert new Value="; cin>>value; for(int i=1;i<p;i++)> { tmp=tmp->next; if(i==p) { break; } } tmp->data=value; cout<<"Value Updated in the node...!"<<endl; } } }; int main() { //node n; link l; int ch; char i; do { cout<<"**************** LINKED LIST ****************"<<endl; cout<<"1: INSERT NODE"<<endl; cout<<"2: REMOVE NODE"<<endl; cout<<"3: UPDATE VALUE IN NODE"<<endl; cout<<"4: DISPLAY"<<endl; cout<<"5: EXIT"<<endl; cout<<endl; cout<<"Enter your choice="; cin>>ch; switch(ch) { case 1: cout<<"A: INSERT NODE FIRST"<<endl; cout<<"B: INSERT NODE LAST"<<endl; /*cout<<"C: INSERT NODE BEFORE"<<endl; cout<<"D: INSERT NODE AFTER"<<endl;*/ cout<<"*****************************"<<endl; cout<<"Enter Choice="; cin>>i; switch(i) { case 'A': l.insert_first(); break; case 'B': l.insert_last(); break; /*case 'C': l.insert_pos_before(); break; case 'D': l.insert_pos_after(); break;*/ default: cout<<"Invalid Option...!"<<endl; break; } break; case 2: cout<<"A: DELETE NODE FIRST"<<endl; cout<<"B: DELETE NODE LAST"<<endl; cout<<"C: DELETE NODE FROM THE DESIRED POSITION"<<endl; cout<<"*****************************"<<endl; cout<<"Enter Choice="; cin>>i; switch(i) { case 'A': l.del_first(); break; case 'B': l.del_last(); break; case 'C': l.del_pos(); break; default: cout<<"Invalid Option...!"<<endl; break; } break; case 3: l.update(); break; case 4: l.display(); break; default: cout<<"Invalid Choice.."<<endl; break; } }while(ch>0 && ch<5); return 0; }
Quote:tmp=head; while(tmp->next!=head->next)
while
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)