Вы находитесь на странице: 1из 4

#include<conio.

h>
#include<iostream.h>
#include<stdlib.h>
void message();
void insertion_beg();
void insertion_end();
void insertion_pos();
void deletion_beg();
void deletion_end();
void deletion_pos();
void traverse();
struct ll{
struct ll *next;
int data;
};
struct ll *start=NULL;
void main(){
clrscr();
cout<<"Please enter 1 for insertion at beginning\n";
cout<<"Please enter 2 for insertion at ending\n";
cout<<"Please enter 3 for insertion at specific position\n";
cout<<"Please enter 4 for Deletion at beginning\n";
cout<<"Please enter 5 for Deletion at ending\n";
cout<<"Please enter 6 for Deletion at specific position\n";
cout<<"please enter 7 for traversal\n";
cout<<"please enter 8 for exit\n";
message();
getch();
}
void message(){
int choice;
cout<<"please enter your choice\n";
cin>>choice;
switch(choice){
case 1:
insertion_beg();
break;
case 2:
insertion_end();
break;
case 3:
insertion_pos();
break;
case 4:
deletion_beg();
break;
case 5:
deletion_end();
break;
case 6:
deletion_pos();
break;
case 7:
traverse();
break;
case 8:
exit(1);
break;
default:
cout<<"please enter valid choice";
message();
break;
}
}
void insertion_end(){
struct ll *node=new ll;
if(start==NULL){
start=node;
cout<<"please enter data\n";
cin>>start->data;
start->next=NULL;
}
else{
struct ll *p=start;//creating a temp pointer
while(p->next!=NULL){
p=p->next;
}
p->next=node;
node->next=NULL;
cout<<"please enter data\n";
cin>>node->data;

}
message();
}
void insertion_beg(){
struct ll *node=new ll;
if(start==NULL){
start=node;
cout<<"please enter data\n";
cin>>start->data;
start->next=NULL;
}
else{
node->next=start;
cout<<"enter data\n";
cin>>node->data;
start=node;
}
message();
}
void insertion_pos(){
struct ll * node= new ll;
int pos,i;
cout<<"please enter position\n";
cin>>pos;
if(start==NULL){
start=node;
cout<<"please enter data\n";
cin>>start->data;
start->next=NULL;
}
else{
struct ll *p=start;
struct ll *q;
i=1;
while(p->next!=NULL && pos!=i){
q=p;
p=p->next;
i=i+1;
}
if(pos==1){
insertion_beg();
}
else if(pos>i){
p->next=node;
node->next=NULL;
cout<<"please enter data\n";
cin>>node->data;
}
else{
cout<<"please enter data\n";
cin>>node->data;
node->next=p;
q->next=node;
}
}
message();
}
void deletion_end(){
//in this we are deleting last node
struct ll *p=start;//creating two temp pointer p and q
struct ll *q=NULL;
if(start==NULL){
cout<<"link list is empty\n";
}
else{
while(p->next!=NULL){
q=p;//q hold 1 less position than p
p=p->next;
}
if(q==NULL)
start=NULL;
else
q->next=NULL;
delete p;
}
message();
}
void deletion_beg(){
struct ll *p=start;
if(start->next==NULL){
delete p;
start=NULL;
}
else if(start==NULL){
cout<<"Link List is empty\n";
}
else{
start=p->next;
delete p;
}
message();
}
void deletion_pos(){
if(start==NULL){
cout<<"Link List is empty\n";
}
else{
struct ll *p=start;
struct ll *q;
int i=1,pos;
cout<<"please enter specific position\n";
cin>>pos;
while(p->next!=NULL && pos!=i){
q=p;
p=p->next;
i=i+1;
}
if(pos==1){
deletion_beg();
}
else if(pos>i){
cout<<"\nlist contain only"<<i<<"element\n";
}
else{
q->next=p->next;
delete p;
}
}
message();
}
void traverse(){
if(start==NULL){
cout<<"Link List Empty\n";
message();
}
else{
struct ll *p=start;
while(p->next!=NULL){
cout<<p->data<<endl;
p=p->next;
}
cout<<p->data<<endl;
message();
}
}

Вам также может понравиться