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 *prev,*next;
- }*head,*p;
- int main()
- {
- int n,m,o,position,ndata;
- printf("Enter Total Number Of Node: ");
- scanf("%d",&n);
- createList(n);
- printf("\nDoubly Linked List is: ");
- displayList();
- printf("\nEnter data to insert at the beginning: ");
- scanf("%d",&m);
- insertatbeg(m);
- displayList();
- printf("\nEnter data to insert at the end: ");
- scanf("%d",&o);
- insertatend(o);
- displayList();
- printf("\nEnter any middle position to add node: ");
- scanf("%d",&position);
- printf("\nEnter the data for node %d: ",position);
- scanf("%d",&ndata);
- insertATn(ndata,position);
- displayList();
- return 0;
- }
- void createList(int n)
- {
- int i,data;
- struct node *temp;
- temp=(struct node *)malloc(sizeof(struct node));
- printf("Enter data for node 1: ");
- scanf("%d",&data);
- temp->data=data;
- temp->prev=NULL;
- temp->next=NULL;
- head=temp;
- p=temp;
- for(i=2;i<=n;i++)
- {
- temp=(struct node *)malloc(sizeof(struct node));
- printf("Enter data for node %d: ",i);
- scanf("%d",&data);
- temp->data=data;
- temp->next=NULL;
- temp->prev=p;
- p->next=temp;
- p=p->next;
- }
- }
- void displayList()
- {
- struct node *temp;
- temp=head;
- while(temp != NULL)
- {
- printf("\n%d",temp->data);
- temp=temp->next;
- }
- }
- void insertatbeg(int data)
- {
- struct node *temp;
- temp=(struct node *)malloc(sizeof(struct node));
- temp->data=data;
- temp->next=head;
- temp->prev=NULL;
- head->prev=temp;
- head=temp;
- }
- void insertatend(int data)
- {
- struct node *temp;
- temp=(struct node *)malloc(sizeof(struct node));
- temp->data=data;
- temp->next=NULL;
- temp->prev=p;
- p->next=temp;
- p=temp;
- }
- void insertATn(int data,int position)
- {
- struct node *temp,*q;
- int i=1;
- q=head;
- temp=(struct node *)malloc(sizeof(struct node));
- temp->data=data;
- while(i<position-1 && q != NULL)
- {
- q=q->next;
- i++;
- }
- temp->prev=q;
- temp->next=q->next;
- if(q->next != NULL)
- {
- q->next->prev=temp;
- q->next=temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement