/* single link list */
#include
#include
#include
#include
struct node
{
int data;
struct node *next;
};
typedef struct node node;
node *start=NULL;
void display();
void insertend();
void insertbeg();
void delend();
void delbeg();
void insertmid();
void delmid();
void modify();
void main()
{
int a;
clrscr();
printf(" THIS PROGRAM GIVES YOU THE CREATE EXCUTABLE LINKLIST\n");
do
{
printf("enter your choice\n");
printf("1.INSERT element at the END of linklist\n");
printf("2.INSERT element at the starting of linklist\n3.DELETE from");
printf("END\n4.DELETE from BEGINING\n5.INSERT at MIDDLE\n");
printf("6.DELETE from MIDDLE\n7.MODIFY any element\n8.EXIT\n");
fflush(stdin);
scanf("%d",&a);
switch(a)
{
case 1:
insertend();
display();
break;
case 2:
insertbeg();
display();
break;
case 3:
delend();
show();
break;
case 4:
delbeg();
show();
break;
case 5:
insertmid();
show();
break;
case 6:
delmid();
show();
break;
case 7:
modify();
show();
break;
case 8:
exit(0);
}
}
while(a!=8);
getch();
}
void insertend()
{
node *p,*q;
int item;
printf("enter your elements in the Group\n");
scanf("%d",&item);
p=(node *)malloc(sizeof(node));
p->data=item;
p->next=NULL;
if(start==NULL)
{
start=p;
}
else
{
q=start;
while(q->next!=NULL)
{
q=q->next;
}
q->next=p;
}
}
void show()
{
node *temp;
temp=start;
printf(" THE LINKLIST IS AS FOLLOWS :");
while(temp->next!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("%d->\n",temp->data);
}
void delend()
{
node *q,*p,*k;
q=start;
if(start->data==0)
//if we write here if(start==NULL)then it will not print
{ //the line.As here the rest portion of delend func delete the
printf("THERE IS NO ELEMENT IN THE LIST\n");//last value and remains it zero
}
else if(start->next==NULL)
{
k=start;
start=NULL;
free(k);
}
else
{
while(q->next->next!=NULL)
{
q=q->next;
}
p=q->next->next;
q->next=NULL;
free(p);
}
}
void insertbeg()
{
int item;
node *p,*q;
printf("enter the value which do you want to insert at Starting\n");
scanf("%d",&item);
p=start;
q=(node *)malloc(sizeof(node));
q->data=item;
q->next=p;
start=q;
}
void delbeg()
{
if(start==NULL)
{
printf("THERE IS NO ELEMENT IN THE LIST\n");
}
node *p;
p=start;
start=p->next;
free(p);
}
void insertmid()
{
int item1,item2;
node *p,*q,*k;
printf("enter the previous value after which you want to insert a new element\n");
scanf("%d",&item1);
printf("enter the value of new node\n");
scanf("%d",&item2);
q=(node *)malloc(sizeof(node));
q->data=item2;
q->next=NULL;
p=start;
while(p->data!=item1)
{
p=p->next;
}
k=p->next;
p->next=q;
q->next=k;
}
void delmid()
{
int item;
node *p,*q,*k;
printf("enter the previous value of that value which you want to delete\n");
scanf("%d",&item);
p=start;
while(p->data!=item)
{
p=p->next;
}
q=p->next->next;
k=p->next;
p->next=q;
free(k);
}
void modify()
{
int item1,item2;
node *p,*q;
printf("enter the value you want to modify\n");
scanf("%d",&item1);
printf("enter the new value\n");
scanf("%d",&item2);
p=start;
while(p->data!=item1)
{
p=p->next;
}
p->data=item2;
}
A program to create executable link list.
A program to create executable link list.
No comments:
Post a Comment