Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct x
- {
- int data;
- struct x *next;
- }x;
- x *head=NULL,*temp;
- void search()
- {
- int n,a=0,b=0;
- temp=head;
- printf("Input the number u want to search\n");
- scanf("%d",&n);
- while(1)
- {
- b++;
- if(temp->data==n)
- {
- printf("%d Found at %d\n",temp->data,b);
- a=1;
- }
- if(temp->next==NULL)
- {
- break;
- }
- temp=temp->next;
- }
- if(a==0)
- {
- printf("Not Found");
- }
- }
- void dbv()
- {
- int i,count=1;
- x *previous;
- printf("Input the value to delete:\n");
- scanf("%d",&i);
- temp=head;
- if(i==head->data)
- {
- head=head->next;
- free(temp);
- }
- else
- {
- while(temp!=NULL && temp->data!=i)
- {
- previous=temp;
- temp=temp->next;
- }
- if(temp==NULL)
- {
- printf("%d does not exist\n",i);
- }
- else
- {
- previous->next=temp->next;
- free(temp);
- }
- }
- temp=head;
- while(temp!=NULL)
- {
- printf("%d ",temp->data);
- temp=temp->next;
- }
- }
- void dbp()
- {
- int i,count=1;
- x *previous;
- printf("Input the position to delete:\n");
- scanf("%d",&i);
- if(i==1)
- {
- temp=head;
- head=head->next;
- free(temp);
- }
- else
- {
- temp=head;
- while(count<i)
- {
- previous=temp;
- temp=temp->next;
- count++;
- }
- previous->next=temp->next;
- free(temp);
- }
- temp=head;
- while(temp!=NULL)
- {
- printf("%d ",temp->data);
- temp=temp->next;
- }
- }
- int main()
- {
- int n,b,c=-1;
- x *nn;
- scanf("%d",&n);
- for(b=0;b<n;b++)
- {
- nn=(x*)malloc(sizeof(x));
- scanf("%d",&nn->data);
- nn->next=NULL;
- if(head==NULL)
- {
- head=nn;
- }
- else
- {
- temp=head;
- while(temp->next!=NULL)
- {
- temp=temp->next;
- }
- temp->next=nn;
- }
- }
- temp=head;
- while(temp!=NULL)
- {
- c++;
- printf("%d\n",temp->data);
- temp=temp->next;
- }
- search();
- dbv();
- dbp();
- while(temp!=NULL)
- {
- printf("%d\n",temp->data);
- temp=temp->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement