Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<stdbool.h>
- struct node
- {
- int data;
- int key;
- struct node *next;
- struct node *prv;
- };
- struct node *head=NULL;
- void insert(int data,int key)
- {
- struct node *temp=(struct node*)malloc(sizeof(struct node));
- temp->data=data;
- temp->key=key;
- if(head==NULL)
- {
- temp->next=NULL;
- head=temp;
- return;
- }
- struct node *temp1=head;
- while(temp1->next!=NULL)
- {
- temp1=temp1->next;
- }
- temp1->next=temp;
- temp->next=NULL;
- }
- void print( )
- {
- struct node *ptr=head;
- while(ptr!=NULL)
- {
- printf("DATA:%d\t KEY:%d\n",ptr->data,ptr->key);
- ptr=ptr->next;
- }
- }
- int length()
- {
- int len=0;
- struct node *temp=head;
- while(temp!=NULL)
- {
- len++;
- temp=temp->next;
- }
- return len;
- }
- void in_pos(int data,int key,int n)
- {
- struct node *temp=(struct node*)malloc(sizeof(struct node));
- temp->data=data;
- temp->key=key;
- if(n==1)
- {
- temp->next=head;
- head=temp;
- return;
- }
- int size=length();
- if((size+1)<n)
- {
- printf("Can't insert in this position\n");
- return ;
- }
- struct node *cur=head;
- int i;
- for(i=0;i<(n-2);i++)
- {
- cur=cur->next;
- }
- temp->next=cur->next;
- cur->next=temp;
- }
- void delete_first()
- {
- if(head==NULL)
- {
- printf("Can't delete no elements assigned\n");
- return;
- }
- head=head->next;
- }
- void last_delete()
- {
- struct node* cur=head,*prv=NULL;
- while(cur->next!=NULL)
- {
- prv=cur;
- cur=cur->next;
- }
- prv->next=NULL;
- }
- void delete_nth(int n)
- {
- int size=length();
- if(size<n)
- {
- printf("No elements in this position\n");
- return;
- }
- if(n==1)
- {
- head=head->next;
- return;
- }
- int i;
- struct node *cur=head,*temp;
- for(i=0;i<n-2;i++)
- {
- cur=cur->next;
- }
- temp=cur->next;
- cur->next=temp->next;
- free(temp);
- }
- void del_key(int key)
- {
- if((head->key)==key)
- {
- head=head->next;
- return;
- }
- struct node *cur=head,*prv=NULL;
- while(cur->key!=key)
- {
- if(cur->next==NULL)
- return NULL;
- prv=cur;
- cur=cur->next;
- }
- prv->next=cur->next;
- free(cur);
- }
- int main()
- {
- insert(10,12);
- insert(13,14);
- insert(16,18);
- insert(20,18);
- int size=length();
- printf("%d\n",size);
- print();
- in_pos(11,12,1);
- in_pos(12,100,4);
- in_pos(100,111,10);
- insert(11,111111);
- size=length();
- printf("%d\n",size);
- print();
- printf("\n");
- delete_first();
- delete_first();
- size=length();
- printf("%d\n",size);
- print();
- last_delete();
- size=length();
- printf("%d\n",size);
- print();
- delete_nth(2);
- printf("\n");
- print();
- del_key(14);
- printf("\n");
- print();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement