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

10.singly Linked List

Uploaded by

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

10.singly Linked List

Uploaded by

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

/* Program for Operations on Singly Linked List*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
int data;
struct node *link;
};
struct node *temp,*r,*old;
struct node *p=NULL;

void insert_beg();
void insert_end();
void insert_pos();
void display();
void del();
void search();

void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1.Insert_beg 2.Insert_end 3.Insert_pos 4.Display 5.Delete 6.Search 7.Exit\n");
printf("Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:insert_beg();
break;
case 2:insert_end();
break;
case 3:insert_pos();
break;
case 4:display();
break;
case 5:del();
break;
case 6:search();
break;
case 7:exit(0);
break;
default: printf("Invalid choice");
break;
}
}
}
void insert_beg()
{
int item;
temp=malloc(sizeof(struct node));
printf("\n Enter item");
scanf("%d",&item);
if(p==NULL)
{
temp->data=item;
temp->link=NULL;
p=temp;
}
else
{
temp->data=item;
temp->link=p;
p=temp;
}
}
void insert_end()
{
int item;
temp=malloc(sizeof(struct node));
printf("\n Enter item");
scanf("%d",&item);
if(p==NULL)
{
temp->data=item;
temp->link=NULL;
p=temp;
}
else
{
struct node *t;
t=p;
while(t->link!=NULL)
t=t->link;
temp->data=item;
temp->link=NULL;
t->link=temp;
}
}
void insert_pos()
{
int pos,i,item;
temp=p;
printf("\nEnter the position where u want to insert element:\t");
scanf("%d",&pos);
for(i=1;i<pos;i++)
temp=temp->link;
if(temp==NULL)
printf("There are less elements in list");
else
{
r=malloc(sizeof(struct node));
printf("\n Enter item");
scanf("%d",&item);
r->data=item;
r->link=temp->link;
temp->link=r;
}
}
void display()
{
temp=p;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->link;
}
}
void del()
{
int n;
temp=p;
printf("Enter number to be deleted: ");
scanf("%d",&n);
while(temp!=NULL)
{
if(temp->data==n)
{
if(temp==p)
p=temp->link;
else
old->link=temp->link;
free(temp);
return;
}
else
{
old=temp;
temp=temp->link;
}
}
printf("Element not found");
}
void search()
{
int n,i=0;
temp=p;
printf("\nEnter number to be search: ");
scanf("%d",&n);
while(temp!=NULL)
{
if(temp->data==n)
{
printf("\nNumber %d found at %d position",n,i);
return;
}
temp=temp->link;
i++;
}
printf("Number not present in list");
}

Output:

Insert element at the beginning


Insert element at End

Insert element at given position


Delete element from list

Search Element

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy