Applications of linked list programs
Applications of linked list programs
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *link;
};
NODE first=NULL;
NODE getnode()
{
NODE x;
x=(NODE) malloc(sizeof(NODE));
if(x==NULL)
{
printf("out of memory\n");
exit(0);
}
return x;
}
void freenode(NODE x)
{
free(x);
}
if(first==NULL)
{
printf("List is underflow\n");
return first;
}
cur=first;
first=first->link;
printf("%d is deleted from list\n",cur->info);
freenode(cur);
return first;
}
NODE display(NODE first)
{
NODE cur;
if(first==NULL)
{
printf("List is empty\n");
return 0;
}
printf("Contents of the list are\n");
cur=first;
while(cur!=NULL)
{
printf("%d\t",cur->info);
cur=cur->link;
}
printf("\n");
}
void main()
{
int option,item;
do
{
printf("\n****Linked Satck operations****");
printf("\n1.push\n2.pop \n 3.Display \n4:Exit\n");
printf("Enter your choice");
scanf("%d",&option);
switch(option)
{
case 1:first=insert_front(first);
break;
case 2:first=delete_front(first);
break;
case 3:display(first);
break;
case 4:break;
default:printf("Invalid option\n");
}
}while(option!=4);
}
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *link;
};
NODE first=NULL;
NODE getnode()
{
NODE x;
x=(NODE) malloc(sizeof(NODE));
if(x==NULL)
{
printf("out of memory\n");
exit(0);
}
return x;
}
void freenode(NODE x)
{
free(x);
}
NODE insert_rear(NODE first)
{
NODE temp,cur;
int item;
printf("\nThe value to insert\n");
scanf("%d",&item);
temp=getnode();
temp->info=item;
cur=first;
while(cur->link!=NULL)
{
cur=cur->link;
}
cur->link=temp;
temp->link=NULL;
printf("%d is inserted into list\n",temp->info);
return first;
}
while(cur!=NULL)
{
printf("%d\t",cur->info);
cur=cur->link;
}
printf("\n");
}
void main()
{
int option,item;
do
{
printf("\n****Linked Queue operations****");
printf("\n1.Insert\n2.Delete\n3.Display \n4:Exit\n");
printf("Enter your choice");
scanf("%d",&option);
switch(option)
{
case 1:first=insert_rear(first);
break;
case 2:first=delete_front(first);
break;
case 3:display(first);
break;
case 4:break;
default:printf("Invalid option\n");
}
}while(option!=4);
}
Write a program to store a polynomial using linked list. Also, perform addition
and subtraction on two polynomials.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
int coeff;
struct node *link;
};
while(num>0)
{
printf("Enter the coefficient:\n");
scanf("%d",&n);
printf("Enter its power:\n");
scanf("%d",&c);
if(first==NULL)
{
temp = (NODE )malloc(sizeof(NODE));
temp -> num = n;
temp -> coeff = c;
temp -> link = NULL;
first = temp;
}
else
{
ptr = first;
while(ptr -> link != NULL)
ptr = ptr -> link;
temp = (NODE)malloc(sizeof(NODE));
temp-> num = n;
temp -> coeff = c;
temp -> link = NULL;
ptr -> link = temp;
}
num--;
}
return first;
}
else
{
ptr = first;
while(ptr -> link != NULL)
ptr = ptr -> link;
temp = (NODE)malloc(sizeof(NODE));
temp -> num = n;
temp -> coeff = c;
temp -> link = NULL;
ptr -> link = temp;
return first;
}
}