0% found this document useful (0 votes)
78 views6 pages

Singly Link List Program

This C program implements a linked list data structure using nodes to store integer data. The main function contains a menu that allows the user to insert, display, and delete nodes from the linked list by calling functions to perform those operations. Functions are defined to insert nodes at the beginning, end, or a specified position of the list. Additional functions allow deleting nodes from the beginning, end, or a specified position. The program uses pointers to traverse and manipulate the linked list.

Uploaded by

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

Singly Link List Program

This C program implements a linked list data structure using nodes to store integer data. The main function contains a menu that allows the user to insert, display, and delete nodes from the linked list by calling functions to perform those operations. Functions are defined to insert nodes at the beginning, end, or a specified position of the list. Additional functions allow deleting nodes from the beginning, end, or a specified position. The program uses pointers to traverse and manipulate the linked list.

Uploaded by

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

Link List Program

#include<stdio.h>
#include<conio.h>
#include<process.h>

// Creating the node structure


struct node
{
int data;
struct node *next;
}*start=NULL,*q,*t;

// Main function through which all the different functions of link-list will be called
int main()
{
int ch;
void insert_beg();
void insert_end();
int insert_pos();
void display();
void delete_beg();
void delete_end();
int delete_pos();

//While(1) makes the functional choice to get executed infinite times unless exit option is pressed.
while(1)
{
printf("\n\n---- Singly Linked List(SLL) Menu ----");
printf("\n1.Insert\n2.Display\n3.Delete\n4.Exit\n\n");
printf("Enter your choice(1-4):");
scanf("%d",&ch);

switch(ch)
{ // This switch case is responsible for various insert operation, and according to the choice the functions are
called.
case 1:
printf("\n---- Insert Menu ----");
printf("\n1.Insert at beginning\n2.Insert at end\n3.Insert at specified position\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);

switch(ch)
{
case 1: insert_beg();
break;
case 2: insert_end();
break;
case 3: insert_pos();
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
break;

case 2: display();
break;

case 3: printf("\n---- Delete Menu ----");


printf("\n1.Delete from beginning\n2.Delete from end\n3.Delete from specified position\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);

switch(ch)
{// This switch case is responsible for various delete operation, and according to the choice the
functions are called.
case 1: delete_beg();
break;
case 2: delete_end();
break;
case 3: delete_pos();
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
}
return 0;
}

// Function to insert when no node has been created. And if link-list exist, this function allows us to insert a new
node in beginning.
void insert_beg()
{
int num;
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
t->data=num;

if(start==NULL) //If list is empty


{
t->next=NULL;
start=t;
}
else
{
t->next=start;
start=t;
}
}

//Funtion to insert node at the end of the existing link-list.


void insert_end()
{
int num;
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
t->data=num;
t->next=NULL;

if(start==NULL) //If list is empty


{
start=t;
}
else
{
q=start;
while(q->next!=NULL)
q=q->next;
q->next=t;
}
}

//Funtion to insert node at any desired position of the existing link-list.

int insert_pos()
{
int pos,i,num;
if(start==NULL)
{
printf("List is empty!!");
return 0;
}

t=(struct node*)malloc(sizeof(struct node));


printf("Enter data:");
scanf("%d",&num);
printf("Enter position to insert:");
scanf("%d",&pos);
t->data=num;

q=start;
for(i=1;i<pos-1;i++)
{
if(q->next==NULL)
{
printf("There are less elements!!");
return 0;
}

q=q->next;
}

t->next=q->next;
q->next=t;
return 0;
}

//Funtion to display the link-list.

void display()
{
if(start==NULL)
{
printf("List is empty!!");
}
else
{
q=start;
printf("The linked list is:\n");
while(q!=NULL)
{
printf("%d->",q->data);
q=q->next;
}
}
}

//Funtion to delete a node from the front of the existing link-list.

void delete_beg()
{
if(start==NULL)
{
printf("The list is empty!!");
}
else
{
q=start;
start=start->next;
printf("Deleted element is %d",q->data);
free(q);
}
}

//Funtion to insert node at the end of the existing link-list.

void delete_end()
{
if(start==NULL)
{
printf("The list is empty!!");
}
else
{
q=start;
while(q->next->next!=NULL)
q=q->next;

t=q->next;
q->next=NULL;
printf("Deleted element is %d",t->data);
free(t);
}
}

//Funtion to delete a node from desired position of the existing link-list.

int delete_pos()
{
int pos,i;

if(start==NULL)
{
printf("List is empty!!");
return 0;
}

printf("Enter position to delete:");


scanf("%d",&pos);
q=start;
for(i=1;i<pos-1;i++)
{
if(q->next==NULL)
{
printf("There are less elements!!");
return 0;
}
q=q->next;
}

t=q->next;
q->next=t->next;
printf("Deleted element is %d",t->data);
free(t);

return 0;
}

You might also like