Screenshot:
Linklist:
Link- list is a datastructure in which we a node consist of a data and the pointer which points to the address of next node.
In linklist a new node is dynamically created using different c functions and memory for that is allocated as we want to insert new element in the linklist.
Description:
In this project you can do all operation with linklist to store data. At first you have to insert 5 numbers and then you will get option to insert new number or node to the different positions.
Here you have option to display all the nodes or numbers in the linklist, and a option to exit from the program.
You can format this project as you want, in the Turbo c++ software.
Source code:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
struct node
{
int info;
struct node*ptr;
};
void main()
{
int i,ch,n,m,k;
struct node*temp,*temp1,*start=NULL,*prev;
clrscr();
i=1;
while(i<=5)
{
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter number : ");
scanf("%d",&n);
temp->info=n;
temp->ptr=NULL;
if(start==NULL)
{
start=temp;
}
else
{
prev->ptr=temp;
}
prev=temp;
i++;
}
temp=start;
while(temp!=NULL)
{
printf("\n%d->",temp->info);
temp=temp->ptr;
}
while(1)
{
printf("\n 1==Node insert at beginning.");
printf("\n 2==Node insert at middle.");
printf("\n 3==Node insert at end.");
printf("\n 4==Display the the list.");
printf("\n 5==Exit");
printf("\n Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
temp1=(struct node *)malloc(sizeof(struct node));
printf("\nEnter number : ");
scanf("%d",&m);
temp1->info=m;
temp1->ptr=start;
start=temp1;
break;
case 2:
printf("Enter the number after you want to insert node : ");
scanf("%d",&k);
temp=start;
while(temp!=NULL)
{
if(temp->info==k)
{
temp1=(struct node *)malloc(sizeof(struct node));
printf("\nEnter number : ");
scanf("%d",&m);
temp1->info=m;
temp1->ptr=temp->ptr;
temp->ptr=temp1;
}
temp=temp->ptr;
}
break;
case 3:
temp=start;
while(temp->ptr!=NULL)
{
temp=temp->ptr;
}
temp1=(struct node *)malloc(sizeof(struct node));
temp->ptr=NULL;
printf("Enter number : ");
scanf("%d",&m);
temp1->info=m;
temp->ptr=temp1;
break;
case 4:
temp=start;
while(temp!=NULL)
{
printf("\n%d->",temp->info);
temp=temp->ptr;
}
break;
case 5:
exit(0);
break;
default:
printf("\nUnvalid case.....plz reenter.....");
}
}
getch();
}
0 Comments