0% found this document useful (0 votes)
2 views4 pages

SLL Code

The document is a C program that implements a singly linked list with various operations such as insertion and deletion at the beginning, end, and random locations. It includes a menu-driven interface for users to choose operations and displays the linked list contents. The program handles memory allocation and deallocation for nodes, ensuring proper management of the linked list structure.

Uploaded by

rajtiamil
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)
2 views4 pages

SLL Code

The document is a C program that implements a singly linked list with various operations such as insertion and deletion at the beginning, end, and random locations. It includes a menu-driven interface for users to choose operations and displays the linked list contents. The program handles memory allocation and deallocation for nodes, ensuring proper management of the linked list structure.

Uploaded by

rajtiamil
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

#include<stdio.

h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
};
struct node *head;

void beginsert ();


void lastinsert ();
void randominsert();
void begindelete();
void lastdelete();
void randomdelete();
void display();

void main ()
{
int ch;
clrscr();
while(ch != 8)
{
printf("\n\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\[Link] in begining \[Link] at last\[Link] at any
random location\[Link] from Beginning\[Link] from last\[Link]
node after specified location\ \[Link]\[Link]\n");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: beginsert();
break;
case 2: lastinsert();
break;
case 3: randominsert();
break;
case 4: begindelete();
break;
case 5: lastdelete();
break;
case 6: randomdelete();
break;

case 7: display();
break;
case 8: exit(0);
break;
default: printf("Please enter valid choice..");
}
}
}
void beginsert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node *));
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = NULL;
if(head==NULL)
head = ptr;
else
{
ptr->next=head;
head=ptr;
}
printf("\nNode inserted");
}

void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
printf("\nEnter value:");
scanf("%d",&item);
ptr->data = item;
ptr->next=NULL;
if(head == NULL)
head = ptr;
else
{
temp = head;
while (temp -> next != NULL)
temp = temp -> next;
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");
}
}

void randominsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
printf("\nEnter value:");
scanf("%d",&item);
ptr->data = item;
ptr->next=NULL;
printf("\nEnter the location after which you want to insert: ");
scanf("%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\ncan't insert\n");
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
}
void begindelete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
}
}
void lastdelete()
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...\n");
}

else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}
}
void randomdelete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want to
perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;

if(ptr == NULL)
{
printf("\nCan't delete");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}

void display()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Nothing to print");
}
else
{
printf("\nprinting values . . . . .\n");
while (ptr!=NULL)
{
printf("\t%d",ptr->data);
ptr = ptr -> next;
}
}
}

You might also like