/*LINER LINK LIST*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
struct node
{
int item;
struct node*next;
};
struct node *create(struct node*head)
{
struct node*ptr;
int n,i=0;
ptr=(struct node*)malloc(sizeof(struct node));
head=NULL;
printf("\nHow many number elements are U enter=");
scanf("%d",&n);
printf("\nElement 1=");
scanf("%d",&ptr->item);
ptr->next=NULL;
head=ptr;
while(i<n-1)
{
ptr->next=(struct node*)malloc(sizeof(struct node));
printf("\nElement %d=",i+2);
scanf("%d",&ptr->next->item);
ptr->next->next=NULL;
ptr=ptr->next;
i++;
}
return head;
}
struct node *insertatend(struct node*head)
{
struct node*ptr,*temp;
temp=head;
if(head==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter element=");
scanf("%d",&temp->item);
temp->next=head;
head=temp;
}
else
{
temp=head;
while(temp->next!=NULL)
temp=temp->next;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nEnter element=");
scanf("%d",&ptr->item);
ptr->next=NULL;
temp->next=ptr;
}
return head;
}
struct node *insertatbeg(struct node*head)
{
struct node*ptr;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nEnter element=");
scanf("%d",&ptr->item);
ptr->next=head;
head=ptr;
return head;
}
void display(struct node*head)
{
struct node*ptr;
ptr=head;
printf("\n");
while(ptr!=NULL)
{
printf("\t%d",ptr->item);
ptr=ptr->next;
}
}
void remove1(struct node*head,int x)
{
struct node*old,*temp;
temp=head;
while(temp!=NULL)
{
if(temp->item==x)
{
if(temp==head)
{
head=temp->next;
free(temp);
return;
}
else
{
old->next=temp->next;
free(temp);
return;
}
}
else
{
old=temp;
temp=temp->next;
}
}
printf("\n%d not found",x);
}
void main()
{
struct node*head,*p;
int i,x;
head=NULL;
clrscr();
while(1)
{
printf("\nPress 1 for create the link list=>");
printf("\nPress 2 for display the link list=>");
printf("\nPress 3 for insert at end of the link list=>");
printf("\nPress 4 for insert at first of the link list=>");
printf("\nPress 5 for remove a element/node from the link list=>");
printf("\nPress 6 for exit=>");
printf("\nEnter your choice=>");
scanf("%d",&i);
switch(i)
{
case 1: p=create(head);
break;
case 2: display(p);
break;
case 3: insertatend(p);
break;
case 4: p=insertatbeg(p);
break;
case 5: printf("\nWhich element want to remove=");
scanf("%d",&x);
remove1(p,x);
break;
case 6: exit(1);
default:printf("\nWrong Choice");
}
}
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
struct node
{
int item;
struct node*next;
};
struct node *create(struct node*head)
{
struct node*ptr;
int n,i=0;
ptr=(struct node*)malloc(sizeof(struct node));
head=NULL;
printf("\nHow many number elements are U enter=");
scanf("%d",&n);
printf("\nElement 1=");
scanf("%d",&ptr->item);
ptr->next=NULL;
head=ptr;
while(i<n-1)
{
ptr->next=(struct node*)malloc(sizeof(struct node));
printf("\nElement %d=",i+2);
scanf("%d",&ptr->next->item);
ptr->next->next=NULL;
ptr=ptr->next;
i++;
}
return head;
}
struct node *insertatend(struct node*head)
{
struct node*ptr,*temp;
temp=head;
if(head==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter element=");
scanf("%d",&temp->item);
temp->next=head;
head=temp;
}
else
{
temp=head;
while(temp->next!=NULL)
temp=temp->next;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nEnter element=");
scanf("%d",&ptr->item);
ptr->next=NULL;
temp->next=ptr;
}
return head;
}
struct node *insertatbeg(struct node*head)
{
struct node*ptr;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nEnter element=");
scanf("%d",&ptr->item);
ptr->next=head;
head=ptr;
return head;
}
void display(struct node*head)
{
struct node*ptr;
ptr=head;
printf("\n");
while(ptr!=NULL)
{
printf("\t%d",ptr->item);
ptr=ptr->next;
}
}
void remove1(struct node*head,int x)
{
struct node*old,*temp;
temp=head;
while(temp!=NULL)
{
if(temp->item==x)
{
if(temp==head)
{
head=temp->next;
free(temp);
return;
}
else
{
old->next=temp->next;
free(temp);
return;
}
}
else
{
old=temp;
temp=temp->next;
}
}
printf("\n%d not found",x);
}
void main()
{
struct node*head,*p;
int i,x;
head=NULL;
clrscr();
while(1)
{
printf("\nPress 1 for create the link list=>");
printf("\nPress 2 for display the link list=>");
printf("\nPress 3 for insert at end of the link list=>");
printf("\nPress 4 for insert at first of the link list=>");
printf("\nPress 5 for remove a element/node from the link list=>");
printf("\nPress 6 for exit=>");
printf("\nEnter your choice=>");
scanf("%d",&i);
switch(i)
{
case 1: p=create(head);
break;
case 2: display(p);
break;
case 3: insertatend(p);
break;
case 4: p=insertatbeg(p);
break;
case 5: printf("\nWhich element want to remove=");
scanf("%d",&x);
remove1(p,x);
break;
case 6: exit(1);
default:printf("\nWrong Choice");
}
}
}
No comments:
Post a Comment