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

SLL

This C program implements a singly linked list with various operations like create, display, insert at beginning/specific position/end, delete from beginning/specific position/end. The main function uses a switch case to call the corresponding functions based on user's choice. Key functions include create to initialize the list, display to print the list, insert and delete functions for the various positions. Pointers are used to traverse and modify the linked list.

Uploaded by

vajjateja2657
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)
18 views5 pages

SLL

This C program implements a singly linked list with various operations like create, display, insert at beginning/specific position/end, delete from beginning/specific position/end. The main function uses a switch case to call the corresponding functions based on user's choice. Key functions include create to initialize the list, display to print the list, insert and delete functions for the various positions. Pointers are used to traverse and modify the linked list.

Uploaded by

vajjateja2657
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/ 5

#include<stdio.

h>
#include<stdlib.h>

void create();
void display();
void insert_begin();
void insert_spec();
void insert_end();
void delete_begin();
void delete_spec();
void delete_end();

typedef struct node_t


{
int data;
struct node_t *next;
}node;

node *head=NULL,*newnode,*temp;

int main()
{
int choice;
while(1)
{
printf("\n---------------------------------------------------");
printf("\nChoose operations to perform on singly linked list:\n");
printf("1.Create\n2.Display\n");
printf("3.Insert at the beginning\n");
printf("4.Insert at a specific position\n");
printf("5.Insert at the end\n");
printf("6.Delete from beginning\n");
printf("7.Delete from specific position\n");
printf("8.Delete from the end\n9.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:create();break;
case 2:display();break;
case 3:insert_begin();break;
case 4:insert_spec();break;
case 5:insert_end();break;
case 6:delete_begin();break;
case 7:delete_spec();break;
case 8:delete_end();break;
case 9:exit(0);
default:printf("\nWrong Choice!!!\n");
}
}
}

void create()
{
int i,n;
printf("Enter number of nodes: ");
scanf("%d",&n);
printf("Enter data: ");
//temp=head;
for(i=0;i<n;i++)
{
newnode=(node *)malloc(sizeof(node));
scanf("%d",&newnode->data);
newnode->next=NULL;
if(i==0)
{ head=newnode;temp=head;}
else
{
temp->next=newnode;temp=temp->next;
}
}
printf("\n!!Singly linked list is created!!\n");
}

void display()
{
if(head==NULL)
printf("\n!! Empty list !!\n");
else
{
printf("\nList elements are:\n");
temp=head;
while(temp)
{
printf("%d-->",temp->data);
temp=temp->next;
}
printf("NULL\n");
}
}

void insert_begin()
{
newnode=(node *)malloc(sizeof(node));
printf("Enter element:\n");
scanf("%d",&newnode->data);
newnode->next=head;
head=newnode;
printf("\n!! %d is inserted at the beginning!! \n",newnode->data);
}

void insert_spec()
{
int pos;
newnode=(node*)malloc(sizeof(node));
printf("Enter element:\n");
scanf("%d",&newnode->data);
printf("Enter position:\n");
scanf("%d",&pos);
if(pos<=0)
{printf("\n!! Invalid position !!\n");return;}
else if (pos==1)
{
newnode->next=head;
head=newnode;
}
else
{
temp=head;
for(int i=1;i<pos-1;i++)
{
temp=temp->next;
if(temp==NULL){printf("\n!! position %d not found!!\n",pos);return;}
}
newnode->next=temp->next;
temp->next=newnode;
}
printf("\n!! %d is inserted at positon %d !!\n",newnode->data,pos);
}

void insert_end()
{
newnode=(node *)malloc(sizeof(node));
printf("Enter element:\n");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL)
head=newnode;
else
{
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
}
printf("\n!! %d is inserted at the end !!\n",newnode->data);
}

void delete_begin()
{
if(head==NULL)
printf("\n!! Empty list. Deletion not possible !!\n");
else
{
temp=head;
head=head->next;
printf("\n!! %d is deleted from beginning !!\n",temp->data);
free(temp);
}
}

void delete_spec()
{
int pos;
node *temp2;
temp=head;
if(head==NULL)
{printf("\n!!! Empty List. Deletion not possible !!!\n");return;}
printf("Enter position:\n");
scanf("%d",&pos);
if (pos<=0)
{printf("\n!! Invalid position !!\n");return;}
else if(pos==1)
head=head->next;
else
{
for(int i=1;i<pos;i++)
{
temp2=temp;
temp=temp->next;
if(temp==NULL){printf("\n!! Position not found !!\n");return;}
}
temp2->next=temp->next;
}
printf("\n!! %d is deleted at position %d !!\n",temp->data,pos);
free(temp);
}

void delete_end()
{
node *temp2;
temp=head;
if(head==NULL)
{printf("\n!! Empty list. Deletion not possible !!\n");return;}
else if(head->next==NULL)
head=NULL;
else
{
while(temp->next!=NULL)
{
temp2=temp;
temp=temp->next;
}
temp2->next=NULL;
}
printf("\n!! %d is deleted from end !!\n",temp->data);
free(temp);
}

You might also like