Weitere ähnliche Inhalte Ähnlich wie Singly Linked List (20) Mehr von Er. Ganesh Ram Suwal (9) Kürzlich hochgeladen (20) Singly Linked List1. /***** @author: Er. Ganesh Ram Suwal *****/
/***** Singly Linked List *****/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
//Global Variables
int data, position;
//Structure for Node
struct node
{
int info;
struct node *pnext;
};
struct node *pnew,*pfirst,*pthis,*ptemp;
//Function Prototype
void nifb();
void nibxp();
void niaxp();
void nife();
void ndfb();
void ndfsp();
void ndfe();
void display();
//Count the nodes of Linked List
int length()
{
int count = 0;
if(pfirst == NULL)
{
return 0;
}
else
{
pthis = pfirst;
count = 1;
while(pthis->pnext != NULL)
{
pthis = pthis->pnext;
count = count + 1;
}
return count;
}
}
void newNode()
{
pnew = (struct node*)malloc(sizeof(struct node));
printf("Data : ");
scanf("%d",&data);
pnew->info = data;
}
2. void invalidPosition()
{
printf("n********************************************************n");
printf(" Invalid Position");
printf("n********************************************************n");
}
void noNodeInLinkedList()
{
printf("n********************************************************n");
printf(" Sorry There is no Node In Linked List");
printf("n********************************************************n");
}
// Main function part
void main()
{
clrscr();
int choice;
start:
printf("n******* Singly Linked List ****************n");
printf("1: Node insert from the beginingn");
printf("2: Node insert from the endn");
printf("3: Node insert before Xth Positionn");
printf("4: Node insert after Xth Positionn");
printf("5: Node delete from the beginingn");
printf("6: Node delete from the endn");
printf("7: Node delete from the specified Positionn");
printf("8: Displayn");
printf("9: Exitn");
printf("Enter your choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:nifb();
break;
case 2:nife();
break;
case 3:nibxp();
break;
case 4:niaxp();
break;
case 5:ndfb();
break;
case 6:ndfe();
break;
case 7:ndfsp();
break;
case 8:display();
break;
case 9:exit(0);
break;
default:printf("Invalid Choice");
break;
}
goto start;
3. }
// Node Insertion from the begining
void nifb()
{
newNode();
if(pfirst == NULL)
{
pnew->pnext = NULL;
pfirst = pnew;
}
else
{
pnew->pnext = pfirst;
pfirst = pnew;
}
}
//node insertion before Xth Position
void nibxp()
{
printf("Position : ");
scanf("%d",&position);
if(position > length() || position < 1)
{
invalidPosition();
}
else if(position == 1)
{
nifb();
}
else
{
newNode();
pthis = pfirst;
for(int i = 0; i < position-2; i++)
{
pthis = pthis->pnext;
}
ptemp = pthis->pnext;
pthis->pnext = pnew;
pnew->pnext = ptemp;
}
}
//Node Insertion After Xth Position
void niaxp()
{
printf("Position : ");
scanf("%d",&position);
if(position > length() || position < 1)
{
invalidPosition();
}
else if(position == length())
{
nife();
4. }
else
{
newNode();
pthis = pfirst;
for(int i = 0; i < position-1; i++)
{
pthis = pthis->pnext;
}
ptemp = pthis->pnext;
pthis->pnext = pnew;
pnew->pnext = ptemp;
}
}
// Node Insertion from the End
void nife()
{
newNode();
pnew->pnext = NULL;
if(pfirst == NULL)
{
pfirst = pnew;
}
else
{
pthis = pfirst;
while(pthis->pnext != NULL)
{
pthis = pthis->pnext;
}
pthis->pnext = pnew;
}
}
//Node deletion from the begining
void ndfb()
{
if(pfirst == NULL)
{
noNodeInLinkedList();
}
else
{
pthis = pfirst;
pfirst = pthis->pnext;
printf("n********************************************************n");
printf("The deleted Node is : %d",pthis->info);
printf("n********************************************************n");
free(pthis);
}
}
//Node deletion from the specified position
void ndfsp()
{
if(pfirst == NULL)
5. {
noNodeInLinkedList();
}
else
{
printf("Position :");
scanf("%d",&position);
if(position > length() || position < 1)
{
invalidPosition();
}
else if(position == 1)
{
ndfb();
}
else if(position == length())
{
ndfe();
}
else
{
pthis = pfirst;
for(int i = 0; i<position-2;i++)
{
pthis = pthis->pnext;
}
ptemp = pthis->pnext->pnext;
printf("n********************************************************n");
printf("The deleted Node is : %d",pthis->pnext->info);
printf("n********************************************************n");
free(pthis->pnext);
pthis->pnext = ptemp;
}
}
}
//Node deletion from the end
void ndfe()
{
if(pfirst == NULL)
{
noNodeInLinkedList();
}
else
{
pthis = pfirst;
if(pfirst->pnext == NULL)
{
printf("n********************************************************n");
printf("The deleted Node is : %d",pthis->info);
printf("n********************************************************n");
free(pthis);
pfirst = NULL;
}
else
{
while(pthis->pnext->pnext != NULL)
6. {
pthis = pthis->pnext;
}
printf("n********************************************************n");
printf("The deleted Node is : %d",pthis->pnext->info);
printf("n********************************************************n");
free(pthis->pnext);
pthis->pnext = NULL;
}
}
}
// Didsplay the Node of a Linked List
void display()
{
int length1;
length1 = length();
if(pfirst == NULL)
{
noNodeInLinkedList();
}
else
{
printf("n********************************************************n");
printf("data of SLL (length=%d): ",length1);
pthis = pfirst;
printf("%d ",pthis->info);
while(pthis->pnext != NULL)
{
pthis = pthis->pnext;
printf("%d ",pthis->info);
}
printf("n********************************************************n");
}
}