// Link List
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
struct link
{
int data;
struct link *next;
};
typedef struct link node;
node *head,*current,*nnode = NULL;
int main()
{
int data,ch,x,p;
node *k;
void createHead(int); void insertAtend(int); void traverse();
void insertAp(int,int); void insertBp(int,int);
int deleteNode(int); node* searchData(int);
printf("\t\t\t\t\t***MENU***\n\n\t\t1.CreateHead\t\t2.Insert at End\t\t3.Traverse List\n\n\n"
"\t\t4.Insert After Postion\t\t5.Insert Before Position\n\n\n\t\t6.Delete Node\t\t7.Search Data\n\n");
do
{
printf("\nEnter Your choice\n\n");scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter data to insert\n");scanf("%d",&data);
createHead(data);
break;
case 2:
printf("\nEnter Data to Insert at End\n\n");scanf("%d",&data);
insertAtend(data);
break;
case 3:
traverse();
break;
case 4:
printf("\nEnter Position And Data to Insert\n\n");scanf("%d%d",&p,&data);
insertAp(p,data);
break;
case 5:
printf("\nEnter Position And Data to Insert\n\n");scanf("%d%d",&p,&data);
insertBp(p,data);
break;
case 6:
printf("\nEnter Position From Where to Delete\n\n");scanf("%d",&p);
x=deleteNode(p);
printf("\nDeleted Data is :- %d",x);
break;
case 7:
printf("\nEnter Data to search in Link List\n\n");
scanf("%d",&data);
k=searchData(data);
if(k==NULL)
{
printf("\nData Not Found\n\n");
}
else
{
printf("\nData Found at Address = %u",k);
break;
}
case 8:
exit(1);
default:
printf("\nWrong Choice Try Again\n\n");
}
}while(ch!=8);
}
void createHead(int x)
{
if(head!=NULL)
printf("\n\t\t**Head already Created**\n\n");
else
{
nnode=(node*)malloc(sizeof(node));
nnode->data=x;nnode->next=NULL;
head=nnode;
}
current=nnode;
}
void insertAtend(int x)
{
if(head!=NULL)
{
nnode=(node*)malloc(sizeof(node));
nnode->data=x;nnode->next=NULL;
current->next=nnode;current=nnode;
}
else
printf("\n\t\t**First Create Head**\n\n");
}
void traverse()
{
node *i;
if(head==NULL)
printf("\n\t\t***There is No Data in Link List\n\n");
else
{
printf("\nData in Link List is :- ");
for(i=head;i!=NULL;i=i->next)
{
printf("%d\t",i->data);
}
}
}
void insertAp(int p,int x)
{
node *temp;
int i;
nnode=(node*)malloc(sizeof(node));
nnode->data=x;
temp=head;
for(i=0;i<p;i++)
{
temp=temp->next;
}
nnode->next=temp->next;
temp->next=nnode;
}
void insertBp(int p,int x)
{
node *temp;
int i;
nnode=(node*)malloc(sizeof(node));
nnode->data=x;
temp=head;
for(i=0;i<p-1;i++)
{
temp=temp->next;
}
nnode->next=temp->next;
temp->next=nnode;
}
int deleteNode(int p)
{
node *t,*q;
int i,k;
t=head;
for(i=1;i<p-1;i++)
{
t=t->next;
}
q=t->next;
t->next=t->next->next;
k=q->data;
return(k);
}
node* searchData(int x)
{
node *i;
for(i=head;i!=NULL;i=i->next)
{
if(i->data==x)
break;
}
return(i);
}
No comments:
Post a Comment