#include<stdio.h>
#include<conio.h>
struct node
{
int info;
struct node *next;
};
struct node* creation();
void display(struct node*);
struct node* insfirst(struct node*);
struct node* inslast(struct node*);
struct node* insbyele(struct node*);
struct node* insbypos(struct node*);
struct node* delatlast(struct node*);
struct node* delbyele(struct node*);
struct node* delbypos(struct node*);
void main()
{
int choice,opt;
struct node *head=NULL;
clrscr();
do
{
printf("\nenter your choice\n1.create linked list\n2.view linked list\n3.inset\n4.deletion of node\n5.quit program\n");
scanf("%d",&opt);
switch (opt)
{
case 1:printf("creation of linked list\n");
head=creation();
printf("\nnode created successfully\ndisplaying the list\n\n");
display(head);
break;
case 2:printf("\ndisplaying list");
display(head);
break;
case 3:if(head!=NULL)
{
printf("\nenter your choice\n1.insertion at first\n2.insertion at last\n3.insertion by element\n4.insertion by positon\n");
scanf("%d",&choice);
switch (choice)
{
case 1:printf("\ninsertion at first\n");
head=insfirst(head);
break;
case 2:printf("\ninsertion at last\n");
head=inslast(head);
break;
case 3:printf("\ninsertion by element\n");
head=insbyele(head);
break;
case 4:printf("\ninsertion by position\n");
head=insbypos(head);
}
}
else
printf("\nwithout creation of a list, insertion is not possible\n");
break;
case 4:if(head!=NULL)
{printf("\n1.deletion at first\n2.delation at last\n3.deletion by element\n4.deletion by position");
scanf("%d",&opt);
switch(opt)
{
case 1:printf("\ndeletion at first\ndisplayinglist\n");
display(head);
head=head->next;
printf("\ndeleted the head element\ndisplaying list\n");
display(head);
break;
case 2:printf("\ndeletion at last\ndislaying list\n");
display(head);
printf("\n\n");
head=delatlast(head);
break;
case 3:printf("\ndeletion by element\n");
delbyele(head);
break;
case 4:printf("\ndeletion of element by position\n");
display(head);
head=delbypos(head);
break;
}
}
else
printf("\nlist is empty\ndeletion not possible");
break;
case 5:printf("\nthankyou");
getch();
exit(0);
}
}while(opt!=5);
getch();
}
struct node* creation()
{
struct node *first,*temp,*new;
int i,n;
printf("enter the number of nodes to be created\n");
scanf("%d",&n);
first=(struct node*)malloc(sizeof(struct node));
printf("\nenter data for node 1\n");
scanf("%d",&first->info);
temp=first;
for(i=1;i<n;i++)
{
new=(struct node*)malloc(sizeof(struct node));
printf("\nenter data for node %d\n",i+1);
scanf("\n%d",&new->info);
if(i==1)
first->next=temp;
new->next=NULL;
temp->next=new;
temp=new;
}
return first;
}
void display(struct node* first)
{
struct node* temp;
temp=first;
printf("\n");
while(temp->next!=NULL)
{
printf("::%d->%u::",temp->info,temp->next);
temp=temp->next;
}
printf("::%d->NULL::",temp->info);
}
struct node* insfirst(struct node* head)
{
struct node *new;
new=(struct node*)malloc(sizeof(struct node));
printf("\nenter the new data to be inserted\n");
scanf("%d",&(new->info));
new->next=head;
head=new;
printf("\nnode created sucessfully\ndisplaying node\n");
display(head);
return(head);
}
struct node* inslast(struct node *head)
{
struct node *temp1,*newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->next=NULL;
temp1=head;
printf("enter the data to be inserted at last");
scanf("%d",&(newnode->info));
do
{
temp1=temp1->next;
}while(temp1->next!=NULL);
temp1->next=newnode;
printf("\nnode created sucessfully");
display(head);
return(head);
}
struct node* insbyele(struct node* head)
{
struct node *travel,*newnode;
int data,f;
newnode=(struct node*)malloc(sizeof(struct node));
travel=head;
printf("\nenter the data after which the newnode is to be created\n");
scanf("%d",&data);
while(travel->next!=NULL)
{
if(travel->info==data)
{
printf("\nenter data for newnode\n");
scanf("%d",&(newnode->info));
newnode->next=travel->next;
travel->next=newnode;
f=1;
break;
}
travel=travel->next;
}
if(f==1)
{
printf("\nnode created sucessfully\n");
display(head);
return(head);
}
else
printf("\ndefined data not found in the list\ninsertion not possible");
return(head);
}
struct node* insbypos(struct node *head)
{
struct node *travel,*newnode;
int count,pos;
count=0;
newnode=(struct node*)malloc(sizeof(struct node));
while(travel!=NULL)
{
count++;
travel=travel->next;
}
printf("\nenter the position after which insertion is to be made(b/w 1 and %d)\n",count);
scanf("%d",&pos);
if(pos<count)
{
travel=head;
printf("\nenter the data to be inserted\n");
scanf("%d",&(newnode->info));
count=1;
while(count<pos)
{ travel=travel->next;
count++;
}
newnode->next=travel->next;
travel->next=newnode;
printf("\nnewnode sucessfully added at positon %d\n",pos);
}
else
printf("invalid positon\n");
display(head);
return(head);
}
struct node* delatlast(struct node *head)
{
struct node *last,*prev;
while(last->next!=NULL)
{prev=last;
last=last->next;
}
prev->next=NULL;
printf("\n\n");
display(head);
return(head);
}
struct node* delbyele(struct node *head)
{
int data;
struct node *travel,*prev;
printf("enter the data to be deleted from the linked list\n");
scanf("%d",&data);
travel=head;
while(travel->next!=NULL)
{
prev=travel;
travel=travel->next;
if(travel->info==data)
{
prev->next=travel->next;
printf("\nsucessfully deleted data-%d from the list\n",data);
display(head);
return(head);
}
travel=travel->next;
}
if(travel==NULL)
printf("element not found\n");
return(head);
}
struct node* delbypos(struct node *head)
{
struct node *travel,*prev;
int count,pos;
count=0;
prev=head;
while(travel!=NULL)
{
count++;
travel=travel->next;
}
travel=head;
printf("\nenter the positon of the node to be deleted\n");
scanf("%d",&pos);
if(pos<count)
{
count=1;
while(count<=(pos-1))
{ prev=travel;
travel=travel->next;
count++;
}
prev->next=travel->next;
printf("\nelement deleted sucessfully\n");
display(head);
return(head);
}
else
printf("\ninvalid entry");
return(head);
}
No comments:
Post a Comment