#include<stdio.h>
struct node
{
int data;
struct node *next;
};
typedef struct node * NODE;
NODE insert_front(NODE first)
{
NODE tmp;
int item;
tmp=(NODE)malloc(sizeof(struct node));
printf("Enter the item to insert at the beganning: ");
scanf("%d",&item);
tmp->data=item;
tmp->next=first;
printf("Inserted!\n");
return(tmp);
}
NODE insert_rear(NODE first)
{
NODE tmp,cur;
int item;
tmp=(NODE)malloc(sizeof(struct node));
printf("Enter the item to insert at the end: ");
scanf("%d",&item);
tmp->data=item;
tmp->next=NULL;
if(first==NULL)
{
printf("Inserted!\n");
return(tmp);
}
cur=first;
while(cur->next!=NULL)
cur=cur->next;
cur->next=tmp;
printf("Inserted!\n");
return(first);
}
NODE insert_bef_ele(NODE first)
{
NODE tmp,cur,pre;
int item,key;
tmp=(NODE)malloc(sizeof(struct node));
printf("Enter the item to be inserted: ");
scanf("%d",&item);
tmp->data=item;
tmp->next=NULL;
if(first==NULL)
{
printf("Inserted!\n");
return(tmp);
}
printf("Enter the key before which item to insert: ");
scanf("%d",&key);
if(first->data==key)
{
tmp->next=first;
printf("Inserted!\n");
return(tmp);
}
pre=NULL;
cur=first;
while(cur!=NULL)
{
if(cur->data==key)
{
tmp->next=cur;
pre->next=tmp;
printf("Inserted!\n");
return(first);
}
pre=cur;
cur=cur->next;
}
if(cur==NULL)
{
printf("Enter the proper key element to insert before!\n");
return(first);
}
}
NODE insert_at_pos(NODE first)
{
NODE tmp,cur,pre;
int item,pos,c=0;
tmp=(NODE)malloc(sizeof(struct node));
printf("Enter the position to which item to be inserted: ");
scanf("%d",&pos);
printf("Enter the item to be inserted at the position: ");
scanf("%d",&item);
tmp->data=item;
tmp->next=NULL;
if(pos==1)
{
tmp->next=first;
printf("Inserted!\n");
return(tmp);
}
pre=NULL;
cur=first;
while(cur!=NULL)
{
c++;
if(pos==c)
break;
pre=cur;
cur=cur->next;
}
if(cur==NULL)
{
printf("Enter proper position to insert!\n");
return(first);
}
else
{
tmp->next=cur;
pre->next=tmp;
printf("Inserted!\n");
}
return(first);
}
void display(NODE first)
{
NODE tmp;
if(first==NULL)
printf("List is Empty!!\n");
tmp=first;
printf("Items in List are: ");
while(tmp!=NULL)
{
printf("%d ",tmp->data);
tmp=tmp->next;
}
printf("\n");
}
main()
{
NODE first=NULL;
int ch;
while(ch)
{
printf("******LINKED-LIST********\n");
printf("1. Insert at the beginning\n");
printf("2. Insert at the end\n");
printf("3. Insert before a given element\n");
printf("4. Insert at the position\n");
printf("5. Display\n6. Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: first=insert_front(first);
break;
case 2: first=insert_rear(first);
break;
case 3: first=insert_bef_ele(first);
break;
case 4: first=insert_at_pos(first);
break;
case 5: display(first);
break;
case 6: exit(0);
}
}
}
struct node
{
int data;
struct node *next;
};
typedef struct node * NODE;
NODE insert_front(NODE first)
{
NODE tmp;
int item;
tmp=(NODE)malloc(sizeof(struct node));
printf("Enter the item to insert at the beganning: ");
scanf("%d",&item);
tmp->data=item;
tmp->next=first;
printf("Inserted!\n");
return(tmp);
}
NODE insert_rear(NODE first)
{
NODE tmp,cur;
int item;
tmp=(NODE)malloc(sizeof(struct node));
printf("Enter the item to insert at the end: ");
scanf("%d",&item);
tmp->data=item;
tmp->next=NULL;
if(first==NULL)
{
printf("Inserted!\n");
return(tmp);
}
cur=first;
while(cur->next!=NULL)
cur=cur->next;
cur->next=tmp;
printf("Inserted!\n");
return(first);
}
NODE insert_bef_ele(NODE first)
{
NODE tmp,cur,pre;
int item,key;
tmp=(NODE)malloc(sizeof(struct node));
printf("Enter the item to be inserted: ");
scanf("%d",&item);
tmp->data=item;
tmp->next=NULL;
if(first==NULL)
{
printf("Inserted!\n");
return(tmp);
}
printf("Enter the key before which item to insert: ");
scanf("%d",&key);
if(first->data==key)
{
tmp->next=first;
printf("Inserted!\n");
return(tmp);
}
pre=NULL;
cur=first;
while(cur!=NULL)
{
if(cur->data==key)
{
tmp->next=cur;
pre->next=tmp;
printf("Inserted!\n");
return(first);
}
pre=cur;
cur=cur->next;
}
if(cur==NULL)
{
printf("Enter the proper key element to insert before!\n");
return(first);
}
}
NODE insert_at_pos(NODE first)
{
NODE tmp,cur,pre;
int item,pos,c=0;
tmp=(NODE)malloc(sizeof(struct node));
printf("Enter the position to which item to be inserted: ");
scanf("%d",&pos);
printf("Enter the item to be inserted at the position: ");
scanf("%d",&item);
tmp->data=item;
tmp->next=NULL;
if(pos==1)
{
tmp->next=first;
printf("Inserted!\n");
return(tmp);
}
pre=NULL;
cur=first;
while(cur!=NULL)
{
c++;
if(pos==c)
break;
pre=cur;
cur=cur->next;
}
if(cur==NULL)
{
printf("Enter proper position to insert!\n");
return(first);
}
else
{
tmp->next=cur;
pre->next=tmp;
printf("Inserted!\n");
}
return(first);
}
void display(NODE first)
{
NODE tmp;
if(first==NULL)
printf("List is Empty!!\n");
tmp=first;
printf("Items in List are: ");
while(tmp!=NULL)
{
printf("%d ",tmp->data);
tmp=tmp->next;
}
printf("\n");
}
main()
{
NODE first=NULL;
int ch;
while(ch)
{
printf("******LINKED-LIST********\n");
printf("1. Insert at the beginning\n");
printf("2. Insert at the end\n");
printf("3. Insert before a given element\n");
printf("4. Insert at the position\n");
printf("5. Display\n6. Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: first=insert_front(first);
break;
case 2: first=insert_rear(first);
break;
case 3: first=insert_bef_ele(first);
break;
case 4: first=insert_at_pos(first);
break;
case 5: display(first);
break;
case 6: exit(0);
}
}
}
No comments:
Post a Comment