Friday 2 September 2016

Linear doubly linked list : C implementation

Write menu driven program for the followings:

a) Linear linked list : Insertion, Deletion, Search, Display.
b) Linear ordered linked list : Insertion, Deletion, Search, Display.
c) Linear circular linked list : Insertion, Deletion, Search, Display.
d) Linear doubly linked list : Insertion, Deletion, Search, Display.
e) Linear doubly circular linked list : Insertion, Deletion, Search, Display.

Note :
1) Insertion and Deletion ( At first node, At last node, At inbetween nodes)
2) Each node in linked list contains Student's Roll_No, Name, CGPA.


//assignment 4
//double linked list

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node{
char name[20];
int roll_no;
float cgpa;
struct node *next;
struct node *prev;
};

struct node *head=NULL;

void insert();
void delete();
void search();
void display();

int main()
{
int choice;
struct node *head=NULL;
while(1){
    printf("Enter your choice:\n");
    printf("1.Insert\n2.Delete\n3.Search\n4.Display\n5.Exit\n");
    scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                insert();break;
            case 2:
                delete();break;
            case 3:
                search();break;
            case 4:
                display();break;
            case 5:
                return 0;
            default :
                printf("Try valid input\n");
        }
    }
}

void insert()
{
int choice;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node)); // allocating memory in heap
struct node *trav,*nth;
trav=head;
int n,count=0;

    printf("\nEnetr your choice :\n");
    printf("1.Insert at beginning \n2.Insert at end \n3.Insert at nth position\n");
    scanf("%d",&choice);
    //getting the data
    printf("Enter name :\n");
    scanf("%s",temp->name);
    printf("Enter roll number :\n");
    scanf("%d",&(temp->roll_no));
    printf("Enter CGPA :\n");
    scanf("%f",&(temp->cgpa));
   
    if(head==NULL)
    {
        temp->next=NULL;
        temp->prev=NULL;
        head=temp;
    }
    else{
   
        switch(choice)
        {
        case 1:
            temp->next=head;
            head->prev=temp;
            head=temp;break;  
        case 2:
            while(trav->next!=NULL)
            {
                trav=trav->next;
            }  
            trav->next=temp;
            temp->prev=trav;
            temp->next=NULL;break;  
        case 3:
          
            printf("Enter position :\n");
            scanf("%d",&n);
            while(count<n-2)
            {
                trav=trav->next;
                count++;
            }
            nth=trav->next;
            temp->next=nth;
            temp->prev=trav;
            trav->next=temp;
            nth->prev=temp;
            break;  
        }
   
    }

}

void delete()
{
    int choice;
    printf("Enter your choice :\n");
    printf("1.Delete 1st node\n2.Delete last node\n3.Delete nth node\n");
    scanf("%d",&choice);
    int n,count=0;
    struct node *temp,*temp1;
    struct node *last;
    temp=head;
    switch(choice)
    {
    case 1:
        temp=temp->next;
        head=temp;
        head->prev=NULL;
        free(temp);break;
    case 2:
        while(temp->next->next!=NULL)
        {
            temp=temp->next;
        }
        last=temp->next;
        temp->next=NULL;
        free(last);break;      
    case 3:
        printf("Enter n :\n");
        scanf("%d",&n);
        while(count<n-2)
        {
            temp=temp->next;
            count++;
        }
        temp1=temp->next;
        temp->next=temp1->next;
        temp1->next->prev=temp;
        free(temp1);break;
    }
}

void search()
{
    struct node *temp;
    temp=head;
    char name[20];
    printf("Search : Name ?\n");
    scanf("%s",name);
    while(temp!=NULL)
    {
        if(strcmp(name,temp->name)==0)
        {
            printf("Here is the required data :\n");
            printf("Name : %s\n",temp->name);
            printf("Roll Number : %d\n",temp->roll_no);
            printf("CGPA : %.2f\n\n",temp->cgpa);
            return;          
        }
        temp=temp->next;
    }
    printf("(X) Sorry , NO MATCHES FOUND !\n");
}

void display()
{
struct node *temp;
temp=head;
int c;
printf("Press 0 : Print in usual order\n");
printf("Press 1 : Print in reverse order\n");
scanf("%d",&c);

if(c==0){
        int count=1;
        printf("\n...................................\n");
        while(temp!=NULL)
        {
            printf("Student %d : \n",count);
            printf("Name : %s\n",temp->name);
            printf("Roll Number : %d\n",temp->roll_no);
            printf("CGPA : %.2f\n\n",temp->cgpa);
            temp=temp->next;
            count++;
        }
        printf("...................................\n");
    }
else if(c==1){
        int count=1;
        while(temp->next!=NULL)
        {
            temp=temp->next;
        }
        printf("\n...................................\n");
        while(temp!=NULL)
        {
            printf("Student %d : \n",count);
            printf("Name : %s\n",temp->name);
            printf("Roll Number : %d\n",temp->roll_no);
            printf("CGPA : %.2f\n",temp->cgpa);
            temp=temp->prev;
            count++;
        }
        printf("...................................\n");
    }
}


Thanking you ! 

No comments:

Post a Comment