0% found this document useful (0 votes)
27 views5 pages

Dheeraj

This document defines functions for creating and manipulating nodes in a linked list data structure in C. It includes functions for creating nodes, inserting nodes at the beginning, end or a given position in the list, deleting nodes from the beginning, end or a given position, displaying the list, and counting the number of nodes. These functions are used in a main program that allows the user to test the various linked list operations.

Uploaded by

ajaydevghan70
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views5 pages

Dheeraj

This document defines functions for creating and manipulating nodes in a linked list data structure in C. It includes functions for creating nodes, inserting nodes at the beginning, end or a given position in the list, deleting nodes from the beginning, end or a given position, displaying the list, and counting the number of nodes. These functions are used in a main program that allows the user to test the various linked list operations.

Uploaded by

ajaydevghan70
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include<stdio.

h>
#include<stdlib.h>

struct node *createNode(void);


void display(void);
void insertionAtFirst(void);
void deletionAtFirst(void);
void insertionAtLast(void);
void deletionAtLast(void);
void insertionAtGiven(void);
void deletionAtGiven(void);
void countNode(void);
struct node
{
int data;
struct node *next;
};
struct node *START=NULL;
struct node *createNode()
{
struct node *node;
node=(struct node *)malloc(sizeof(struct node));
printf("Enter Nodes Data\t");
scanf("%d",&node->data);
node->next=NULL;
return node;
}
void display()
{
struct node *t;
if(START==NULL)
{
printf("Linked list is Empty");
}
else
{
t=START;
printf("Linked list is :\t");
while(t != NULL)
{
printf("%d\t",t->data);
t=t->next;
}
}
}
void insertionAtFirst()
{
struct node * t;
t=createNode();
if(START==NULL)
{
START=t;
}
else
{
t->next=START;
START=t;
}
}
void deletionAtFirst()
{
struct node *t;
if(START==NULL)
{
printf("Linked List is Empty");
}
else
{
t=START;
START=START->next;
t->next=NULL;
free(t);
}
}
void insertionAtLast()
{
struct node *t,*p;
t=createNode();
if(START==NULL)
{
START=t;
}
else
{
p=START;
while(p->next != NULL)
{
p=p->next;
}
p->next=t;
}
}
void deletionAtLast()
{
struct node *t,*p;
if(START==NULL)
{
printf("Linked list is empty");
}
else
{
t=START;
while(t->next != NULL)
{
p=t;
t=t->next;
}
p->next=NULL;
free(t);
}
}
void insertionAtGiven()
{
int pos,i=1;
struct node *t,*p;
t=createNode();
if(START==NULL)
{
START=t;
}
else
{
p=START;
printf("Enter the position to insert the node :\t");
scanf("%d",&pos);
while(i < pos-1)
{
p=p->next;
i++;
}
t->next=p->next;
p->next=t;
}
}
void deletionAtGiven()
{
int pos,i=1;
struct node *t,*p;
if(START==NULL)
{
printf("Linked list is empty");
}
else
{
t=START;
printf("Enter node position to delete :\t");
scanf("%d",&pos);
while(i < pos)
{
p=t;
t=t->next;
i++;
}
p->next=t->next;
t->next=NULL;
free(t);
}
}
void countNode()
{
int cnt=0;
struct node *t;
if(START==NULL)
{
printf("Linked list is Empty");
}
else
{
t=START;
while(t != NULL)
{
cnt++;
t=t->next;
}
printf("Total nodes are :\t%d",cnt);
}
}
void main()
{
struct node *temp;
int i,size,ch,x;
printf("Enter Linked List size\t");
scanf("%d",&size);
for(i=1;i<=size;i++)
{
temp=createNode();
if(START==NULL)
{
START=temp;
}
else
{
temp->next=START;
START=temp;
}
}
while(1)
{
printf("\n");

printf("***************************************************************************
***");
printf("\n1.Insertion\n2.Deletion\n3.Display\n4.CountNode\n5.Exit\n");
printf("Enter your choice :\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n1.Insertion At First\n2.Insertion At Last\n3.Insertion At
Given\n");
printf("Enter your choice :\t");
scanf("%d",&x);
switch(x)
{

case 1:

insertionAtFirst();
display();
break;
case 2:
insertionAtLast();
display();
break;

case 3:
insertionAtGiven();
display();
break;

default :

printf("Invalid choice");
break;

}
break;

case 2:
printf("\n1.Deletion At First\n2.Deletion At Last\n3.Deletion At
Given\n");
printf("Enter your choice :\t");
scanf("%d",&x);
switch(x)
{

case 1:

deletionAtFirst();
display();
break;
case 2:
deletionAtLast();
display();
break;

case 3:
deletionAtGiven();
display();
break;

default :

printf("Invalid choice");

}
break;

case 3:
display();
break;
case 4:
countNode();
break;
case 5:
exit(1);

default :
printf("Invalid choice");

}
}

You might also like