You are on page 1of 3

#include<stdio.

h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*start=NULL;
void create()
{
char ch;
do
{
struct node *new_node,*current;
new_node=(struct node*)malloc(sizeof(struct node));
printf("\nenter the data");
scanf("%d",&new_node->data);
new_node->next=NULL;
if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
current->next=new_node;
current=new_node;
}
printf("\ndo you want to create another:");
ch=getche();
}while(ch=='y');
}
void display()
{
struct node *new_node;
printf("\nThe linked list:");
new_node=start;
while(new_node!=NULL)
{
printf("%d--->",new_node->data);
new_node=new_node->next;
}
printf("NULL");
}
void search(int x)
{
int count=0;

struct node* temp=start;


while(temp!=NULL)
{
if(temp->data==x)
{
count++;
printf("\nelement found %d",temp->data);
printf("\nelement found at %d position",count);
break;
}
temp=temp->next;
}
}
void reverse()
{
struct node *p,*q,*r;
if(start==NULL)
{
printf("\nlist is empty\n");
return;
}
p=start;
q=p->next;
p->next=NULL;
while(q!=NULL)
{
r=q->next;
q->next=p;
p=q;
q=r;
}
start=p;
printf("\nreverse successfully");
}
void del_first()
{
if(start==NULL)
printf("list is empty");
else
{
struct node *temp=start;
start=temp->next;
delete temp;
printf("\nfirst node deleted successfully");
}
}
void del_last()
{
if(start==NULL)
printf("\nerror list is empty");
else

{
struct node *q=start;
while(q->next->next!=NULL)
q=q->next;
node *temp=q->next;
q->next=NULL;
delete temp;
printf("\ndeleted successfully");
}
}
/*void del(int c)
{

}*/
void main()
{
int a;
clrscr();
create();
display();
reverse();
display();
search(a);
display();
del_first();
del_last();
display();
printf("\nenter the value of search");
scanf("%d",&a);
display();
getch();
}

You might also like