Saturday, 1 August 2015

DS - LINKED LISTS 4 (Doubly linked list)

Write a C program to insert an element to the front end, rear end or specified position in the doubly linked list, also write the display function-to display the content of the list and delete function- to delete the node.

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
int i;

struct student
{
    int data;
    struct student *next;
    struct student *back;
};
typedef struct student *NODE;

NODE insert_front(NODE first,int d)
{
    NODE temp;
    temp=(NODE)malloc(sizeof(struct student));
    temp->data=d;
        if(first==NULL){
    return temp;
}
    temp->back=NULL;
    temp->next=first;
    first->back=temp;
    return temp;
}
NODE insert_rear(NODE first,int d)
{
    NODE temp,cur;
    temp=(NODE)malloc(sizeof(struct student));
    temp->data=d;
    temp->next=NULL;
    temp->back=NULL;
    cur=first;
    while(cur->next!=NULL){
    cur=cur->next;}
    cur->next=temp;
    temp->back=cur;
    return first;
}
NODE insert_pos(NODE first, int d,int pos)
{
    NODE temp,cur,prev;
    temp=(NODE)malloc(sizeof(struct student));
    temp->data=d;
    temp->next=NULL;
    temp->back=NULL;
    if(first==NULL)
        return temp;
    cur=first;
    for(i=1;i<pos;i++)
    {
        prev=cur;
        cur=cur->next;
    }
    prev->next=temp;
    temp->back=prev;
    temp->next=cur;
    cur->back=temp;
    return first;
}
NODE delete(NODE first, int a)
{
    NODE temp,cur,prev;
    if(a==0)
    {
        cur=first;
        while(cur->next!=NULL){
        prev=cur;
        cur=cur->next;}
        prev->next=NULL;
        free(cur); return first;
    } else{
    cur=first;
    for(i=1;i<a;i++){
    prev=cur;
    cur=cur->next;}

    temp=cur->next;
    printf("\n ::: deleted is %d :: \n",cur->data);
    prev->next=temp;
    temp->back=prev;
    free(cur);
    return first;
}
}
void display(NODE first)
{
    NODE temp;
  if(first==NULL)
    printf("\nList is empty");
else{
    temp=first;
    printf("\n content is :: \n");
 while(temp!=NULL)
      {
       printf("%d\n",temp->data);
       temp=temp->next;
      }
}
}
void main()
{
    NODE first=NULL;
    int ch,d,pos;
    for(;;)
    {
        printf("\n 1.front insert \n 2.rear insert \n 3.pos insert \n 4. front delete ");
        printf("\n 5.rear delete \n 6.pos delete \n 7. display \n 8.exit \n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1: printf(" \n enter the item to be inserted :: \n"); scanf("%d",&d);
                first=insert_front(first,d); break;
            case 2: printf(" \n enter the item to be inserted :: \n"); scanf("%d",&d);
                first=insert_rear(first,d); break;
            case 3: printf(" \n enter the item to be inserted :: \n"); scanf("%d",&d);
                printf(" \n enter the position :: \n"); scanf("%d",&pos);
                first=insert_pos(first,d,pos); break;
            case 4: printf(" \n deleting item from front end \n");
                first=delete(first,1); break;
            case 5: printf(" \n deleting item from rear end \n");
                first=delete(first,0); break;
            case 6: printf(" \n enter the pos :: \n"); scanf("%d",&pos);
                printf(" \n deleting item from specified pos \n");
                first=delete(first,pos); break;
            case 7: printf(" \n content of linked list is ::: \n");
                display(first); break;
            case 8:exit(0); break;
        }   
    }
}

No comments:

Post a Comment

Total Pageviews