?
2013-04-21 13:59:49 UTC
#include
#include
#include
#include
#include
# define NULL 0
typedef struct node
{
int number;
struct node *next;
};
typedef struct node NODE;
NODE *start,*list,*rTail,*qTail,*temp,*temp1,*p,*q;
void Insert();
void Create();
void Display();
int Count();
void Update();
NODE* Copy();
void Split();
void Delete();
int count=0,pos,item,k;
int a[100],b[100],n;
void main()
{
int c;
char ch='y';
list=NULL;
clrscr();
do
{
printf("\n--------MENU---------");
printf("\n1.Create a link list");
printf("\n2.Display the link list");
printf("\n3.Count the number of elements");
printf("\n4.Update a node");
printf("\n5.Insert a node");
printf("\n6.Delete a node");
printf("\n7.Copy to another linked list");
printf("\n8.Split the linked list");
printf("\nEnter your choice");
scanf("%d",&c);
switch (c)
{
case 1:
Create(list);
Display(list);
break;
case 2:
Display(list);
break;
case 3:
count=count+Count(list);
Display(list);
printf("\nElement Number=%d\n",count);
break;
case 4:
Update(list);
Display(list);
break;
case 5:
Insert();
Display(list);
break;
case 6:
Delete(list);
Display(list);
break;
case 7:
Copy(list,start);
Display(list);
printf("\nThe Copy of the list\n");
Display(q);
break;
case 8:
Split();
Display(rTail);
printf("\n");
Display(qTail);
break;
default:
printf("\nYou have exited from the program");
exit(0);
}
fflush(stdin);
printf("\n Do you want to continue y/n");
scanf("%c",&ch);
}while(ch=='y' || ch=='Y');
}
void Create()
{
char ch='y';int n;
while(ch=='y' || ch=='Y')
{
printf("Enter an element");
scanf("%d",&n);
if(list==NULL)
{ p=(NODE *)malloc(sizeof(NODE));
p->next=NULL;
p->number=n;
list=p;
}
else
{ temp=list;
while(temp->next!=NULL)
{temp=temp->next;
}
p=(NODE *)malloc(sizeof(NODE));
p->number=n;
p->next= NULL;
temp->next=p;
} fflush(stdin);
printf("Do you want to add more data y/n ");
scanf("%c",&ch);
}
}
void Display(NODE *list)
{ NODE *temp;
temp=list;
while(temp!=NULL)
{
printf("%d\t",temp->number);
temp=temp->next;
}
}
int Count(NODE *list)
{ if(list==NULL)
return(0);
else
return (1+Count(list->next));
}
void Update(NODE *list)
{ printf("Enter the position of the node to be updated\n");
scanf("%d",&pos);
printf("Enter the element\n");
scanf("%d",&item);
temp=list;
for(k=0;k
{ printf("\n No such value in the list !!!");
return;
}
temp=temp->next;
}
temp->number=item;
}
NODE* Copy(NODE *list,NODE *q)
{ if(list!=NULL)
{
q=(NODE *)malloc(sizeof(NODE));
q->number=list->number;
q->next=NULL;
Copy(list->next,q->next) ;
}
}
void Delete(NODE *list)
{
printf("\nEnter the position of the node to be deleted");
scanf("%d",&pos);
temp=list;
if(pos==1)
list=list->next;
for(k=0;k
temp->next=(temp->next)->next;
}
void Insert()
{ int a;
printf("\nEnter the position of the node to be inserted");
scanf("%d",&pos);
printf("\nEnter the element to be inserted");
scanf("%d",&item);
temp=list;
p=(NODE *)malloc(sizeof(NODE));
p->number=item;
p->next=NULL;
if(pos==1)
{ p->next=temp;
list=p;
}
else{
for(a=0;a
p->next=(temp->next)->next;
temp->next=p;
temp=temp->next;
}
}