Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct node
- {
- int data;
- struct node *next;
- };
- struct node *head;
- void Insert(int data,int pos)
- {
- struct node* temp=(struct node*)malloc(sizeof(struct node));
- temp->data=data;
- temp->next=NULL;
- if(pos==1)
- {
- temp-> next=head;
- head=temp;
- return;
- }
- struct node *temp1=head;
- int i;
- for(i=0; i<pos-2; i++)
- {
- temp1=temp1->next;
- }
- temp->next = temp1->next;
- temp1->next = temp;
- }
- void print()
- {
- struct node *temp=head;
- while(temp!=NULL)
- {
- printf("%d ",temp->data);
- temp = temp->next;
- }
- }
- int search(int value)
- {
- struct node* temp1=head;
- int f=0;
- while (temp1!=NULL)
- {
- if(temp1->data == value)
- {
- f=1;
- }
- temp1=temp1->next;
- }
- return f;
- }
- int main()
- {
- head = NULL;
- Insert(4,1);
- Insert(5,1);
- Insert(6,1);
- print();
- printf("\n");
- Insert(7,2);
- print();
- printf("\n");
- int f;
- f=search(4);
- if(f==1)
- printf("Found");
- else
- printf("\n Not Found");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment