Friday 2 September 2016

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



//ordered 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 orderedInsertion();
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:
                orderedInsertion();break;
            case 2:
                delete();break;
            case 3:
                search();break;
            case 4:
                display();break;
            case 5:
                return 0;
            default :
                printf("Try valid input\n");
        }
    }
}

void orderedInsertion()
{
    struct node* current;
    struct node* new_node;
    new_node=(struct node *)malloc(sizeof(struct node));
  
        printf("Enter name :\n");
    scanf("%s",new_node->name);
    printf("Enter roll number :\n");
    scanf("%d",&(new_node->roll_no));
    printf("Enter CGPA :\n");
    scanf("%f",&(new_node->cgpa));
    /* Special case for the head end */
    if (head == NULL || head->roll_no >= new_node->roll_no)
    {
        new_node->link = head;;
        head = new_node;
    }
    else
    {
        /* Locate the node before the point of insertion */
        current = head;
        while (current->link!=NULL &&
               current->link->roll_no < new_node->roll_no)
        {
            current = current->link;
        }
        new_node->link = current->link;
        current->link = new_node;
    }
}

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