Program of linked list
2:32 AM
#include<conio.h>
#include<stdio.h>
struct node
{
int data;
struct node *next;
};
void addatbeg(int x,struct node**plist);
void addatend(int x,struct node **plist);
int deletefrombeg(struct node **plist);
int deletefromend(struct node **plist);
void display(struct node *plist);
int main()
{
struct node *list=NULL;
addatbeg(5,&list);
addatbeg(7,&list);
addatbeg(10,&list);
display(list);
addatend(8,&list);
display(list);
deletefrombeg(&list);
display(list);
deletefromend(&list);
display(list);
getch();
}
void display(struct node *p)
{
printf("\nThe list is:");
while(p!=NULL)
{
printf("%4d-->",p->data);
p=p->next;
}
printf("END");
}
void addatbeg(int x,struct node**plist)
{
struct node *new;
new=(struct node*)malloc(sizeof(struct node));
new->data=x;
new->next=*plist;
*plist=new;
}
void addatend(int x,struct node **plist)
{
struct node *temp,*new;
new=(struct node*)malloc(sizeof(struct node));
new->data = x;
new->next=NULL;
if((*plist)==NULL)
{
(*plist)=new;
}
else
{
temp=(*plist);
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new;
}
}
int deletefrombeg(struct node **plist)
{
struct node *temp;
temp=*plist;
if(temp==NULL)
{
printf("list is empty");
return;
}
*plist=temp->next;
free(temp);
}
int deletefromend(struct node**plist)
{
struct node *temp,*prev;
temp=*plist;
if(temp==NULL)
{
printf("The list is empty:");
return;
}
else if(temp->next==NULL)
{
(*plist)=NULL;
free(temp);
return;
}
while(temp->next != NULL)
{
prev = temp;
temp = temp->next;
}
prev->next = NULL;
free(temp);
return;
}
#include<stdio.h>
struct node
{
int data;
struct node *next;
};
void addatbeg(int x,struct node**plist);
void addatend(int x,struct node **plist);
int deletefrombeg(struct node **plist);
int deletefromend(struct node **plist);
void display(struct node *plist);
int main()
{
struct node *list=NULL;
addatbeg(5,&list);
addatbeg(7,&list);
addatbeg(10,&list);
display(list);
addatend(8,&list);
display(list);
deletefrombeg(&list);
display(list);
deletefromend(&list);
display(list);
getch();
}
void display(struct node *p)
{
printf("\nThe list is:");
while(p!=NULL)
{
printf("%4d-->",p->data);
p=p->next;
}
printf("END");
}
void addatbeg(int x,struct node**plist)
{
struct node *new;
new=(struct node*)malloc(sizeof(struct node));
new->data=x;
new->next=*plist;
*plist=new;
}
void addatend(int x,struct node **plist)
{
struct node *temp,*new;
new=(struct node*)malloc(sizeof(struct node));
new->data = x;
new->next=NULL;
if((*plist)==NULL)
{
(*plist)=new;
}
else
{
temp=(*plist);
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new;
}
}
int deletefrombeg(struct node **plist)
{
struct node *temp;
temp=*plist;
if(temp==NULL)
{
printf("list is empty");
return;
}
*plist=temp->next;
free(temp);
}
int deletefromend(struct node**plist)
{
struct node *temp,*prev;
temp=*plist;
if(temp==NULL)
{
printf("The list is empty:");
return;
}
else if(temp->next==NULL)
{
(*plist)=NULL;
free(temp);
return;
}
while(temp->next != NULL)
{
prev = temp;
temp = temp->next;
}
prev->next = NULL;
free(temp);
return;
}
0 comments: