Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct mylist
- {
- int data;
- struct mylist*next;
- };
- struct mylist*head=NULL;
- void display()
- {
- struct mylist*temp=head;
- while(temp!=NULL)
- {
- printf("%d->",temp->data);
- temp=temp->next;
- }
- printf("\n");
- }
- void first_insert(int data)
- {
- struct mylist*temp=(struct mylist*)malloc(sizeof(struct mylist));
- temp->data=data;
- temp->next=head;
- head=temp;
- }
- void insert_last(int data)
- {
- struct mylist*temp=head;
- struct mylist*new_node=(struct mylist*)malloc(sizeof(struct mylist));
- new_node->data=data;
- new_node->next=NULL;
- if(temp==NULL)
- {
- head=new_node;
- return;
- }
- while(temp->next!=NULL)
- {
- temp=temp->next;
- }
- temp->next=new_node;
- return;
- }
- void middle_insert(int n, int n1)
- {
- struct mylist*new_node=(struct mylist*) malloc(sizeof(struct mylist*));
- struct mylist*temp=head;
- new_node->data=n1;
- new_node->next=NULL;
- int i;
- for(i=0;i<(n-2);i++)
- {
- temp=temp->next;
- }
- struct mylist*temp1;
- temp1=temp->next;
- temp->next=new_node;
- new_node->next=temp1;
- return;
- }
- void delete_node(int n)
- {
- struct mylist*temp=head;
- if(n==1)
- {
- head=temp->next;
- free(temp);
- return;
- }
- int i;
- for(i=0;i<(n-2);i++)
- {
- temp=temp->next;
- }
- struct mylist*temp1;
- temp1=temp->next;
- temp->next=temp1->next;
- return;
- }
- int count_node()
- {
- struct mylist*temp;
- int count=0;
- temp=head;
- while(temp->next!=NULL)
- {
- count++;
- temp=temp->next;
- }
- printf("%d node\n",count+1);
- }
- void create(int data)
- {
- insert_last(data);
- }
- void search(int data)
- { int count=0;
- struct mylist*temp=head;
- while(temp!=NULL)
- {
- if (temp->data==data)
- count++;
- temp=temp->next;
- }
- printf("%d",count);
- }
- int main()
- {
- struct mylist *head;
- insert_last(8);
- insert_last(5);
- first_insert(2);
- first_insert(6);
- first_insert(3);
- first_insert(1);
- middle_insert(6,7);
- display();
- count_node();
- delete_node(2);
- display();
- count_node();
- delete_node(4);
- display();
- count_node();
- search(5);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement