/*Serial No.137 [swami119.cpp]*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
typedef struct node
{
int info;
struct node * next;
}node;
void insert_at_end(node **head,int n)
{
node *temp,*q;
temp=(node *) malloc(sizeof(node));
temp->info=n;
temp->next=NULL;
if(*head==NULL)
{
*head=temp;
return;
}
q=*head;
while(q->next!=NULL)
{
q=q->next;
}
q->next=temp;
}
void display(node *head)
{
if(head==NULL)
{
printf("list is empty");
return;
}
while(head!=NULL)
{
printf("%d ",head->info);
head=head->next;
}
}
void insert_at_beg(node **head, int n)
{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->info=n;
temp->next=*head;
*head=temp;
}
void insert_position(node *head,int pos_value)
{
int ele,flag=1;
while(head->info!=pos_value)
{
head=head->next;
if(head==NULL)
{
flag=2;break;
}
}
if(flag==1)
{
printf("\nEnter the no to insert= ");
scanf("%d",&ele);
node *temp=(node *)malloc(sizeof(node));
temp->info=ele;
temp->next=head->next;
head->next=temp;
}
else
printf("\nValue u entered is not in list.\n");
}
void delete_val(node **head,int n)
{
node *q=*head,*temp;
if(q->info==n)
{
temp=q;
*head=temp->next;
}
else
{
while(q->next!=NULL)
{
if(q->next->info==n)
{
temp=q->next;
q->next=temp->next;
break;
}
q=q->next;
}
if(q->next==NULL)
{
printf("\nNo. is not in list.\n\t!! If it is last node then it has been deleted.!!");
return;
}
}
free(temp);
}
void Reverse_list(node **head)
{
node *f,*s,*t;
f=*head;
s=f->next;
t=s->next;
f->next=NULL;
while(s!=NULL)
{
s->next=f;
f=s;
s=t;
t=t->next;
}
*head=f;
}
void main()
{
node *start1=NULL;
node *start2=NULL;
int choice,ele,pos_value,option;
clrscr();
do
{
printf("\n\t[1] Work with First List\n");
printf("\n\t[2] Work with Second List\n");
printf("\n\t[3] Concatenate Second to First List\n");
printf("\n\t[7] Exit\n");
printf("\nEnter your choice= ");
scanf("%d",&option);
if(option==1)
{
do
{
printf("\n\t[1] Create\\Insert at end");
printf("\n\t[2] Create\\Insert at Begining");
printf("\n\t[3] Insert at Position");
printf("\n\t[4] Delete");
printf("\n\t[5] Display");
printf("\n\t[6] Reverse List");
printf("\n\t[7] Exit");
printf("\n\t\tEnter ur choice= ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nEnter the element=");
scanf("%d",&ele);
insert_at_end(&start1,ele);
break;
case 2:printf("\nEnter the element=");
scanf("%d",&ele);
insert_at_beg(&start1,ele);
break;
case 3:printf("\nEnter the element after which u want to insert= ");
scanf("%d",&pos_value);
insert_position(start1,pos_value);
break;
case 4:printf("\nEnter the element which u want to delete= ");
scanf("%d",&ele);
delete_val(&start1,ele);
break;
case 5:printf("\nElements in list are:\n");
display(start1);
break;
case 6:printf("\nReversing link list...:\n");
Reverse_list(&start1);
break;
case 7:break;
}
}while(choice!=7);
}
if(option==2)
{
do
{
printf("\n\t[1] Create\\Insert at end");
printf("\n\t[2] Create\\Insert at Begining");
printf("\n\t[3] Insert at Position");
printf("\n\t[4] Delete");
printf("\n\t[5] Display");
printf("\n\t[6] Reverse List");
printf("\n\t[7] Exit");
printf("\n\t\tEnter ur choice= ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nEnter the element=");
scanf("%d",&ele);
insert_at_end(&start2,ele);
break;
case 2:printf("\nEnter the element=");
scanf("%d",&ele);
insert_at_beg(&start2,ele);
break;
case 3:printf("\nEnter the element after which u want to insert= ");
scanf("%d",&pos_value);
insert_position(start2,pos_value);
break;
case 4:printf("\nEnter the element which u want to delete= ");
scanf("%d",&ele);
delete_val(&start2,ele);
break;
case 5:printf("\nElements in list are:\n");
display(start2);
break;
case 6:printf("\nReversing link list...:\n");
Reverse_list(&start2);
break;
case 7:break;
}
}while(choice!=7);
}
if(option==3)
{
node *add;
add=start1;
while(add->next!=NULL)
{
add=add->next;
}
add->next=start2;
start2=NULL;
}
}while(option!=7);
}
No comments:
Post a Comment
If you have any doubt, feel free to ask...