Showing posts with label linked list. Show all posts
Showing posts with label linked list. Show all posts

Saturday, 3 September 2016

Reversing a linked list : C implemenation

For reversing a linked list we have to choice :-
  1.  Change all links .
  2.  Change the data part .
Second method , changing the data , is very easy !
What we have to do for that is :
  • Create a linked list
  • Create a stack as well
  • In Reverse Function ()
    • Push() all the elements in the stack first .
      • Now stack contains the elements of linked list in same order.
    • Traverse the list from head to NULL again .
      • Assign the the popped value to each node .
    • As Stack pops elements in reverse order , we'll have a reversed list now . 
check its implementation :

Reversing linked list using stack :


#include<stdio.h>
#include<stdlib.h>
#define max 100


// creating a stack of integers

int top=-1;
int stack[max];

void push(int );
int pop();

//linked list part


struct node{
    int data;
    struct node *link;
};

struct node *head=NULL;

void append();
void addatbeg();
void reverse();
void display();

int main()
{
int choice;
while(1){
    printf("Enter your choice:\n");
    printf("1.Append\n2.Add at begining\n3.Display\n4.Reverse\n5.Exit\n");
    scanf("%d",&choice);

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

void append()
{
    struct node *temp,*trav;
    trav=head;
    temp=(struct node *)malloc(sizeof(struct node));
    int n;
    printf("Enter data to add at end : ");
    scanf("%d",&n);
    temp->data = n;
   
    if(head==NULL)
    {
        temp->link=NULL;
        head=temp;  
    }
    else
    {
        while(trav->link!=NULL)
        {
            trav=trav->link;
        }
        trav->link = temp;
        temp->link = NULL;
    }
   
}


void addatbeg()
{
    struct node *temp;
    temp=head;
    temp=(struct node *)malloc(sizeof(struct node));
    int n;
    printf("Enter data to add at beginning : ");
    scanf("%d",&n);
    temp->data = n;
   
    if(head==NULL)
    {
        temp->link=NULL;
        head=temp;  
    }
   
    else{
        temp->link = head;
        head = temp;
    }
}

void reverse()
{
    //pushing elements in the stack
    struct node *temp;
    temp = head;
    while(temp!=NULL)
    {
        push(temp->data);
        temp=temp->link;
    }
    // poping elements from stack
   
    struct node *temp1;
    temp1=head;
    while(temp1!=NULL)
    {
        temp1->data = pop();
        temp1=temp1->link;
    }
   
    //setting the stack again for further use.
    top=-1;
}


void display()
{
    struct node *temp ;
    temp =head;
    printf("List : ");
    while(temp != NULL)
    {
        printf(" %d ",temp->data);
        temp=temp->link;
    }
}


// stack operations

void push(int n)
{
    //check overflow
    if(top==max-1)
    {
        printf("Overflow !");
        return;
    }
    else
    {
        top++;
        stack[top]=n;
    }
}

int pop()
{
    //check underflow
    int x=0;
    if(top==-1)
    {
        printf("Underflow !\n");
        return 0;
    }
    else
    {
        x=stack[top];
        top--;
        return x;
    }
}

 

Also , we can reverse linked list by changing the links .
 In this case too, we have two choice
  • Iterative method
  • Recursive method    
What our goal is :
  • Set head to last node
  • Set link part of former head to NULL
  • Set linked parts of all elements to its predecessor 
 Try to implement by taking this in account or check the following implementation !

Reversing a linked list by Changing links :



//reversing a singly linked list

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

struct node{
    int data;
    struct node *link;
};

struct node *head=NULL;

void append();
void addatbeg();
void revIt();
void revRe(struct node *);
void display();

int main()
{
int choice;
while(1){
    printf("Enter your choice:\n");
    printf("1.Append\n2.Add at begining\n3.Display\n4.Reverse(Iteration)\n5.Reverse(recurssion)\n6.Exit\n");
    scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                append();break;
            case 2:
                addatbeg();break;
            case 3:
                display();break;
            case 4:
                revIt();break;
            case 5:
                revRe(head);
                display();break;
            case 6:
                return 0;
            default :
                printf("Try valid input\n");
        }
    }
}

void append()
{
    struct node *temp,*trav;
    trav=head;
    temp=(struct node *)malloc(sizeof(struct node));
    int n;
    printf("Enter data to add at end : ");
    scanf("%d",&n);
    temp->data = n;
   
    if(head==NULL)
    {
        temp->link=NULL;
        head=temp;  
    }
    else
    {
        while(trav->link!=NULL)
        {
            trav=trav->link;
        }
        trav->link = temp;
        temp->link = NULL;
    }
   
}


void addatbeg()
{
    struct node *temp;
    temp=head;
    temp=(struct node *)malloc(sizeof(struct node));
    int n;
    printf("Enter data to add at beginning : ");
    scanf("%d",&n);
    temp->data = n;
   
    if(head==NULL)
    {
        temp->link=NULL;
        head=temp;  
    }
   
    else{
        temp->link = head;
        head = temp;
    }
}

void revIt()
{
    struct node *current,*next,*prev;
    current = head;
    prev = NULL;
    while(current != NULL)
    {
        next = current->link;
        current->link = prev;
        prev = current ;
        current = next ;
    }
    head = prev;
    printf("Reversed linked list :\n");
    display();
}
void revRe(struct node *temp)
{

    if(temp->link==NULL)
    {
        head = temp;
        return ;
    }
    revRe(temp->link);
    struct node *temp2;
    temp2 = temp->link;
    temp2->link = temp;
    temp->link = NULL;
   
}

void display()
{
    struct node *temp ;
    temp =head;
    printf("List : ");
    while(temp != NULL)
    {
        printf(" %d ",temp->data);
        temp=temp->link;
    }
}


Thanking you !

Friday, 2 September 2016

Linear doubly circular 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
//doubly circular 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)
    {
        head=temp;
        temp->next=head;
        temp->prev=head;
    }
    else{
   
        switch(choice)
        {
        case 1:

            temp->next=head;
            head->prev=temp;
            while(trav->next!=head)
            {
                trav=trav->next;
            }   
            trav->next=temp;
            temp->prev=trav;
            head=temp;
            break;
        case 2:
            while(trav->next!=head)
            {
                trav=trav->next;
            }   
            trav->next=temp;
            temp->prev=trav;
            temp->next=head;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,*trav;
    struct node *last;
    temp=head;
    switch(choice)
    {
    case 1:
        temp=temp->next;
        while(trav->next!=head)
        {
            trav=trav->next;
        }
        trav->next=temp;
        temp->prev=trav;
        head=temp;
        break;
    case 2:
        while(temp->next->next!=head)
        {
            temp=temp->next;
        }
        last=temp->next;
        temp->next=head;
        head->prev=temp;
        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);
    do
    {
        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;
    }while(temp->next!=head);
    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");
        do
        {
            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++;
        }while(temp!=head);
        printf("...................................\n");
    }
else if(c==1){
        int count=1;
        while(temp->next!=head)
        {
            temp=temp->next;
        }
        printf("\n...................................\n");
        do
        {
            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++;
        }while(temp!=head);
       
            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);
           
        printf("...................................\n");
    }
}

Thanking you !

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 ! 

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


//single circular 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=temp;
                head=temp;
            }
    else{
   
        switch(choice)
        {
        case 1:
                while((trav->link)!=head)
                {
                    trav=trav->link;
                }
                trav->link=temp;
                temp->link=head;
                head=temp;break;  
        case 2:
            while(trav->link!=head)
            {
                trav=trav->link;
            }  
            trav->link=temp;
            temp->link=head;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()
{
    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:
        if (head==NULL)
        {
            printf("List is empty\n");
        }
        else
        {
        temp1=head;
        while(temp1->link!=head)
        {
            temp1=temp1->link;
        }
        temp1->link=head->link;
        head=head->link;
        } break;
    case 2:
        if (head==NULL)
        {
            printf("List is empty\n");  
        }
        else
        {
            while((temp->link->link)!=head)
            {
                temp=temp->link;
            }
            temp->link=head;
        }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);
    do
    {
        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;
    }while(temp!=head);
    printf("(X) Sorry , NO MATCHES FOUND !\n");
}

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


Thanking you !

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 !

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 !

Wednesday, 17 August 2016

Linked list : Implementation using C


Hello guys !
Here is the explanation regarding to implementation of Linked list , in a very easy way .
Its very basics and aimed to clear the the fundamental and logical structure of linked list . I hope you will appreciate this !





  



#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* link;
};
struct node *head=NULL;
int main()
{
//creating first node
struct node *temp;
temp= (struct node*)malloc(sizeof(struct node));
temp->data = 2;
temp->link = NULL;
//connecting with head node
head = temp;


//creating another node
struct node *tempo;
tempo= (struct node*)malloc(sizeof(struct node));
tempo->data = 4;
tempo->link = NULL;

//traversing till NULL for connecting the created node to end .
struct node *temp1;
temp1=head;
while(temp1->link!=NULL)
{
temp1=temp1->link;
}
temp1->link=tempo;


//creating another node
struct node *tempoo;
tempoo= (struct node*)malloc(sizeof(struct node));
tempoo->data = 6;
tempoo->link = NULL;

//traversing till NULL for connecting the created node to end .
struct node *temp2;
temp2=head;
while(temp2->link!=NULL)
{
temp2=temp2->link;
}
temp2->link=tempoo;


//printing elements

struct node *a;
a=head;
while(a!=NULL)
{
printf("%d\n",a->data);
a=a->link;
}
}




Thanking you !