0% found this document useful (0 votes)
14 views

Singly Linked List

Uploaded by

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

Singly Linked List

Uploaded by

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

Singly Linked list

#include<stdio.h>
#include<stdlib.h>
int c=0,pos=0;
struct node
{
int info;
struct node*next;
};
struct node*start;struct node*ptr;struct node*temp;struct node*prev;
int value;

void insertbeg() //insertion at the beginning


{
ptr=malloc(sizeof(struct node *));
printf("Enter the value: ");
scanf("%d",&ptr->info);
ptr->next=NULL;
if(start==NULL)
{
start=ptr;
c++;
}
else
{
ptr->next=start;
start=ptr;
c++;
}
}

void insertend() //insertion at the end


{
ptr=malloc(sizeof(struct node *));
printf("Enter the value: ");
scanf("%d",&ptr->info);
ptr->next=NULL;
if(start==NULL)
{
start=ptr;
c++;
}
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
c++;
}
temp->next=ptr;
}
}

void insertpos() //insertion at a specific position


{
int pos;
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter the value: ");
scanf("%d",&ptr->info);
printf("Enter the position: ");
scanf("%d",&pos);
if(c<=pos)
{
printf("not possible \n");
}
else
{
if(pos==1)
{
ptr->next=start;
start=ptr;
}
else
{
int count=1;
temp=start;
while(count<pos-1)
{
temp=temp->next;
count++;
}
ptr->next=temp->next;
temp->next=ptr;
}
}
}

void delbeg() //deletion at the beginning


{
int value;
if(start==NULL)
{
printf("Empty list \n");
}
else
{
temp=start;
value=temp->info;
start=temp->next;
c--;
}
free(temp);
printf("Deleted element is %d \n",value);
}

void delend()// deletion at the end


{
int value;
if(start==NULL)
{
printf("Empty list \n");
}
else
{
temp=start;
}
if(temp->next==NULL)
{
value=temp->info;
start=NULL;
free(temp);
c--;
}
else
{
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
}
value=temp->info;
printf("Deleted element is %d \n",value);
prev->next=NULL;
free(temp);
c--;
}
}

void delpos() // deletion at a specific position


{
if(start==NULL)
{
printf("Empty list \n");
}
else
{
temp=start;
printf("Enter the position: ");
scanf("%d",&pos);
if(pos==1)
{
if(temp->next==NULL)
{
value=temp->info;
printf("Deleted element is %d \n",value);
start=NULL;
free(temp);
c--;
}
else
{
value=temp->info;
printf("Deleted element is %d \n",value);
start=temp->next;
free(temp);
c--;
}
}
else
{
int count=1;
while(count<pos)
{
prev=temp;
temp=temp->next;
count++;
}
value=temp->info;
printf("Deleted element is %d \n",value);
prev->next=temp->next;
free(temp);
c--;
}
}

void delelm()
{
int e;
if(start==NULL)
{
printf("Empty list \n");
}
else
{
printf("Enter the element to deleted: ");
scanf("%d",&e);
if(start->info==e)
{
if(start->next==NULL)
{
start=NULL;
}
else
{
temp=start;
start=temp->next;
free(temp);
c--;
}
}
else
{
temp=start;
while(temp!=NULL)
{

if(temp->info!=e && temp-> next==NULL)


{
prev=temp;
temp=temp->next;
}
else
{
prev->next=temp->next;
free(temp);
c--;
}
}

}
}
}

void display() //display the linklist


{
int count=0;
temp=start;
if(start==NULL)
{
printf("List is empty \n");
}
else
{
while(temp!=NULL)
{
printf("%d ",temp->info);
count++;
temp=temp->next;
}
printf("\n Total elements: %d\n",count);
}
}

void main()
{
int i;
do
{
printf("1. Insertion at the beginning \n");
printf("2. Insertion at end \n");
printf("3. Insertion at a specified position \n");
printf("4. Deletion at the beginning \n");
printf("5. Deletion at end \n");
printf("6. Deletion at a specified position \n");
printf("7. Deletion of a specified element \n");
printf("8. Display \n");
printf("9. Exit\n");
printf("Enter: ");
scanf("%d",&i);
switch(i)
{
case 1: insertbeg();
break;
case 2: insertend();
break;
case 3: insertpos();
break;
case 4: delbeg();
break;
case 5: delend();
break;
case 6: delpos();
break;
case 7: delelm();
break;
case 8: display();
break;
case 9: printf("Exiting.. \n");
exit(0);
break;
default:printf("invalid");
}
}
while(i<9);
}
Output:

You might also like