#include <stdio.h>
#include <conio.h>
struct llnode
{
int info;
struct node *next;
};
typedef struct llnode node;
node* create()
{
node *tp;
int no;
printf("Enter the info for new node: ");
scanf("%d",&no);
tp =(node *)malloc(sizeof(node));
tp->info=no;
tp->next=NULL;
return tp;
}
void insert_last(node *head,node *newnode)
{
node *tp=head;
if(tp!=NULL)
{
while(tp->next!=NULL)
tp=tp->next;
tp->next=newnode;
}
else
{
printf("LIST IS EMPTY, CANNOT INSERT");
getch();
}
}
void traverse(node *head)
{
node *tp=head;
printf("Traverse\n-->");
if(tp!=NULL)
{
printf("\t%d \n",tp->info);
while(tp->next!=NULL)
{
tp=tp->next;
printf("\t%d \n",tp->info);
}
}
getch();
}
void count(node *head)
{
int count=1;
node *tp=head;
if(tp!=NULL)
{
while(tp->next!=NULL)
{
count++;
tp=tp->next;
}
printf("No. of nodes are: %d \n",count);
}
getch();
}
node* insert_first(node *head,node *newnode)
{
if(head==NULL)
{
head=newnode;
}
else
{
newnode->next=head;
head=newnode;
}
return head;
}
void insert_at(node *head,node *newnode)
{
int pos,i;
node *tp=head;
printf("Enter the position(greater than 0) where you want to insert new node: ");
scanf("%d",&pos);
for(i=1;i<pos;i++)
{
tp=tp->next;
}
newnode->next=tp->next;
tp->next=newnode;
}
void delete_next(node *head,int key)
{
node *tmp;
node *tp=head;
while(tp->next!=NULL && tp->info!=key)
tp=tp->next;
if(tp->next==NULL && tp->info==key)
printf("Cannot delete next node ");
else if(tp->next==NULL && tp->info!=key)
printf("Key doesn't exist ");
else if(tp->next!=NULL && tp->info==key)
{
tmp=tp->next;
tp->next=tmp->next;
free(tmp);
}
getch();
}
node* delete_first(node *head)
{
node *tp=head;
head=head->next;
free(tp);
return head;
}
node* delete_last(node *head)
{
node *tp=head,*tmp;
if(head==NULL)
return head;
else if(head->next==NULL)
{
tmp=head;
head=NULL;
free(tmp);
return head;
}
else
{
while(tp->next!=NULL)
{
tmp=tp;
tp=tp->next;
}
tmp->next=NULL;
free(tp);
return head;
}
}
void delete_at(node *head)
{
int pos,i;
node *tp=head,*tmp;
if(head==NULL||head->next==NULL)
printf("CANNOT DELETE NODE");
else
{
printf("Enter the position(greater than 0) from which you want to delete node: ");
scanf("%d",&pos);
for(i=1;i<pos;i++)
{
tp=tp->next;
}
tmp=tp->next;
tp->next=tmp->next;
free(tmp);
}
getch();
}
void main()
{
node *head,*newnode;
int no,c=1,key;
clrscr();
head= create();
do
{
clrscr();
printf("\n\t***** MAIN MENU *****\n\n");
printf("[1] INSERT LAST\n[2] INSERT FIRST\n[3] INSERT AT SPECIFIC LOCATION\n");
printf("[4] DELETE AFTER KEY\n[5] DELETE FIRST\n[6] DELETE LAST\n[7] DELETE FROM SPECIFIC LOACATION\n");
printf("[8] TRAVERSE\n[9] COUNT NODES\n[10] EXIT\n");
printf("\nENTER YOUR CHOICE: ");
scanf("%d",&c);
switch(c)
{
case 1: newnode=create();
insert_last(head,newnode);
break;
case 2: newnode=create();
head=insert_first(head,newnode);
break;
case 3: newnode=create();
insert_at(head,newnode);
break;
case 4: printf("Enter the key after which the node is to be deleted:");
scanf("%d",&key);
delete_next(head,key);
break;
case 5: head=delete_first(head);
break;
case 6: head=delete_last(head);
break;
case 7: delete_at(head);
break;
case 8: traverse(head);
break;
case 9: count(head);
break;
case 10: exit();
}
}while(c>0);
getch();
}
#include <conio.h>
struct llnode
{
int info;
struct node *next;
};
typedef struct llnode node;
node* create()
{
node *tp;
int no;
printf("Enter the info for new node: ");
scanf("%d",&no);
tp =(node *)malloc(sizeof(node));
tp->info=no;
tp->next=NULL;
return tp;
}
void insert_last(node *head,node *newnode)
{
node *tp=head;
if(tp!=NULL)
{
while(tp->next!=NULL)
tp=tp->next;
tp->next=newnode;
}
else
{
printf("LIST IS EMPTY, CANNOT INSERT");
getch();
}
}
void traverse(node *head)
{
node *tp=head;
printf("Traverse\n-->");
if(tp!=NULL)
{
printf("\t%d \n",tp->info);
while(tp->next!=NULL)
{
tp=tp->next;
printf("\t%d \n",tp->info);
}
}
getch();
}
void count(node *head)
{
int count=1;
node *tp=head;
if(tp!=NULL)
{
while(tp->next!=NULL)
{
count++;
tp=tp->next;
}
printf("No. of nodes are: %d \n",count);
}
getch();
}
node* insert_first(node *head,node *newnode)
{
if(head==NULL)
{
head=newnode;
}
else
{
newnode->next=head;
head=newnode;
}
return head;
}
void insert_at(node *head,node *newnode)
{
int pos,i;
node *tp=head;
printf("Enter the position(greater than 0) where you want to insert new node: ");
scanf("%d",&pos);
for(i=1;i<pos;i++)
{
tp=tp->next;
}
newnode->next=tp->next;
tp->next=newnode;
}
void delete_next(node *head,int key)
{
node *tmp;
node *tp=head;
while(tp->next!=NULL && tp->info!=key)
tp=tp->next;
if(tp->next==NULL && tp->info==key)
printf("Cannot delete next node ");
else if(tp->next==NULL && tp->info!=key)
printf("Key doesn't exist ");
else if(tp->next!=NULL && tp->info==key)
{
tmp=tp->next;
tp->next=tmp->next;
free(tmp);
}
getch();
}
node* delete_first(node *head)
{
node *tp=head;
head=head->next;
free(tp);
return head;
}
node* delete_last(node *head)
{
node *tp=head,*tmp;
if(head==NULL)
return head;
else if(head->next==NULL)
{
tmp=head;
head=NULL;
free(tmp);
return head;
}
else
{
while(tp->next!=NULL)
{
tmp=tp;
tp=tp->next;
}
tmp->next=NULL;
free(tp);
return head;
}
}
void delete_at(node *head)
{
int pos,i;
node *tp=head,*tmp;
if(head==NULL||head->next==NULL)
printf("CANNOT DELETE NODE");
else
{
printf("Enter the position(greater than 0) from which you want to delete node: ");
scanf("%d",&pos);
for(i=1;i<pos;i++)
{
tp=tp->next;
}
tmp=tp->next;
tp->next=tmp->next;
free(tmp);
}
getch();
}
void main()
{
node *head,*newnode;
int no,c=1,key;
clrscr();
head= create();
do
{
clrscr();
printf("\n\t***** MAIN MENU *****\n\n");
printf("[1] INSERT LAST\n[2] INSERT FIRST\n[3] INSERT AT SPECIFIC LOCATION\n");
printf("[4] DELETE AFTER KEY\n[5] DELETE FIRST\n[6] DELETE LAST\n[7] DELETE FROM SPECIFIC LOACATION\n");
printf("[8] TRAVERSE\n[9] COUNT NODES\n[10] EXIT\n");
printf("\nENTER YOUR CHOICE: ");
scanf("%d",&c);
switch(c)
{
case 1: newnode=create();
insert_last(head,newnode);
break;
case 2: newnode=create();
head=insert_first(head,newnode);
break;
case 3: newnode=create();
insert_at(head,newnode);
break;
case 4: printf("Enter the key after which the node is to be deleted:");
scanf("%d",&key);
delete_next(head,key);
break;
case 5: head=delete_first(head);
break;
case 6: head=delete_last(head);
break;
case 7: delete_at(head);
break;
case 8: traverse(head);
break;
case 9: count(head);
break;
case 10: exit();
}
}while(c>0);
getch();
}