Вы находитесь на странице: 1из 6

#include<stdio.

h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
typedef struct node ND;
ND *insert_begin(ND *start,int item)
{
ND *temp;
temp=(ND*)malloc(sizeof(ND));
if(temp==NULL)
{
printf("no allocation");
return start;
}
temp->data=item;
temp->next=start;
start=temp;
return start;
}
void display(ND *start)
{
ND *current;
current=start;
printf("\n the given data\n");
while(current!=NULL)
{
printf("%d ",current->data);
current=current->next;
}
}
ND *insert_end(ND* start,int item)
{
ND *temp;
ND *current;
temp=(ND*)malloc(sizeof(ND));
if(temp==NULL)
{
printf("\n no allocation");
return start;
}
temp->data=item;
temp->next=NULL;
current=start;
while(current->next!=NULL)
{
current=current->next;
}
current->next=temp;
return start;

}
void search(ND* start,int key)
{
ND *current=start;
while(current!=NULL)
{
if(current->data==key)
{
break;
}
current=current->next;
}
if(current==NULL)
{
printf("\n the data not found");

}
else
printf("\n the data=%d found",key);

}
void before_search(ND *start,int key)
{
ND *current=start;
ND *prev=start;
while(current!=NULL)
{
if(current->data==key)
{
break;
}
else
{
prev=current;
current=current->next;
}
}
if(current==NULL)
{
printf("\n data not found");
}
else
{
printf("\n the data found & prev value %d",prev->data);
}
}
/* delete a particular node*/
ND *delete_particular(ND *start,int item)
{
ND *current,*prev;
current=prev=start;
while(current!=NULL)
{
if(current->data==item)
{
break;
}
else
{
prev=current;
current=current->next;
}
}
if(start==NULL)
{
printf("list Empty");
return start;
}
else
{
if(prev==current)
{
start=current->next;
}
else
{
prev->next=current->next;
printf("\n the data %d is deleted",current->data);
}
free(current);
}
return start;
}
/* physical reverse of node*/
ND *rev(ND *start)
{
ND *p,*q,*r;
if(start==NULL || start->next==NULL)// for 1 node & no node
{
printf("\n the list will be same");
}
else
{
p=start;
q=start->next;
p->next=NULL;
while(p!=NULL)
{
r=q->next;
q->next=p;
p=q;q=r;
}
start=p;
}
return start;
}
int main()
{
int d,flag,item;
int choice;
ND* temp,*current;
ND* start=NULL;
system("clear");
do
{
printf("\n enter the data");
scanf("%d",&d);
temp=(ND*)(malloc(sizeof(ND*)));
if(temp==NULL)
{
printf("\n no allocation");
return 0;
}
temp->data=d;
temp->next=NULL;
if(start==NULL)
{
start=temp;

}
else
{
current->next=temp;
}
current=temp;
printf("\n do you want create more node");

scanf("%d",&flag);
}while(flag==1);
do
{
printf("\n1:insert_begin\n2:insert_end\n3:display\n4:Search\n5:print the value
before key \n6:Delete a particular node\n7:reverse\n\n0:exit");
printf("\n_________________________________");
printf("\n entre the choice->");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\n entre the data->");
scanf("%d",&item);
start=insert_begin(start,item);break;
case 2: printf("\n enter the data->");
scanf("%d",&item);
start=insert_end(start,item);break;
case 3:display(start);break;
case 4:printf("\n enter the searching item");
scanf("%d",&item);
search(start,item);break;
case 5:printf("\n enter the searching item");
scanf("%d",&item);
before_search(start,item);break;
case 6: printf("\n Enter the key");
scanf("%d",&item);
start=delete_particular(start,item);
break;
case 7:start=rev(start);
break;
case 11:exit(0);
}
}while(1);
}

Вам также может понравиться