Advertisement
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 n)
- {
- struct node *temp1=(struct node *)malloc(sizeof(struct node));
- temp1->data=data;
- temp1->next=NULL;
- if(n==1)
- {
- temp1->next=head;
- head=temp1;
- return;
- }
- struct node *temp2=head;
- int i;
- for(i=0;i<n-2;i++)
- {
- temp2=temp2->next;
- }
- temp1->next=temp2->next;
- temp2->next=temp1;
- }
- void print()
- {
- struct node *s=head;
- while(s!=NULL)
- {
- printf("%d\n",s->data);
- s=s->next;
- }
- }
- void search(int n)
- {
- struct node *s=head;
- int c=0;
- while(s!=NULL)
- {
- if(s->data==n) c++;
- s=s->next;
- }
- printf("%d found %d times\n",n,c);
- }
- int count()
- {
- struct node *s=head;
- int c=0;
- while(s!=NULL)
- {
- c++;
- s=s->next;
- }
- return c;
- }
- void dlnode(int n)
- {
- struct node *temp=head;
- if(count()<n)
- {
- printf("No more node than %d",count());
- }
- if(n==1)
- {
- head=temp->next;
- free(temp);
- printf("Node %d deleted\n",n);
- return;
- }
- int i;
- for(i=0;i<n-2;i++)
- {
- temp=temp->next;
- }
- struct node *temp2;
- temp2=temp->next;
- temp->next=temp2->next;
- free(temp2);
- printf("Node %d deleted\n",n);
- return;
- }
- int main()
- {
- head = NULL;
- insert(5,1);
- insert(10,2);
- insert(15,3);
- insert(20,4);
- insert(25,5);
- print();
- int n=count();
- printf("%d items\n",n);
- int x;
- printf("Which node you want to search?\nReplay:");
- scanf("%d",&x);
- search(x);
- int z;
- printf("Which node you want to delete?\nReplay:");
- scanf("%d",&z);
- dlnode(z);
- print();
- int m=count();
- printf("%d items \n",m);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement