Friday 2 September 2016

Linear 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.


//singly linked list
//assignment 4

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

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

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;
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->link==NULL;
        head=temp;
    }
    else{
   
        switch(choice)
        {
        case 1:
            temp->link=head;
            head=temp;break;   
        case 2:
            while(trav->link!=NULL)
            {
                trav=trav->link;
            }   
            trav->link=temp;
            temp->link=NULL;break;   
        case 3:
           
            printf("Enter position :\n");
            scanf("%d",&n);
            while(count<n-2)
            {
                trav=trav->link;
                count++;
            }
            temp->link=trav->link;
            trav->link=temp;break;   
        }
   
    }

}

void delete()
{
    if(head==NULL)
    {
    printf("List is empty\n");
    return;
    }
    else{
    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->link;
        head=temp;
        free(temp);break;
    case 2:
        while(temp->link->link!=NULL)
        {
            temp=temp->link;
        }
        last=temp->link;
        temp->link=NULL;
        free(last);break;       
    case 3:
        printf("Enter n :\n");
        scanf("%d",&n);
        while(count<n-2)
        {
            temp=temp->link;
            count++;
        }
        temp1=temp->link;
        temp->link=temp1->link;
        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->link;
    }
    printf("(X) Sorry , NO MATCHES FOUND !\n");
}

void display()
{
struct node *temp;
temp=head;
    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->link;
        count++;
    }
    printf("...................................\n");
}




Thanking you !

No comments :

Post a Comment