The greatest mistake you can make in life is to be continually fearing you will make one

Thursday, 11 February 2010

C Programs based on LInked List

Sample Program 1:
Simple linear linked list constructed in c.Adding 10 elements into the list and print them
Program

#include "stdio.h"
#include "stdlib.h"

struct Node
{
   //each node contains a data and a pointer to the next node in the list
   int data;
   struct Node *next;
};

typedef struct Node node;

int main()
{
    node *newnode,*head,*first;
    int i;
    head=NULL;
    first=NULL;

    //Inserting 10 nodes in a list
    for(i=1;i<=10;i++)
    {
       newnode=(node*)malloc(sizeof(node));
       newnode->data=i;
       newnode->next=NULL;
  
       if(head==NULL)
       {
          head=newnode;
          first=newnode; //To point the first node in the list
        }
        head->next=newnode;
        head=newnode;
    }
     //Print the nodes in the list
     newnode=first;
     while(newnode)
     {
          printf("%d\t",newnode->data);
          newnode=newnode->next;
     }
}


Output:
1 2 3 4 5 6 7 8 9 10


Sample Program 2
Write a C program to perform the basic operation like insertion,deletion,display in a singly linked list.
solution
#include<stdio.h>
#include<stdlib.h>

void Insert_list(int);
void Insert_pos(int,int);
void delete_list(int);
void display(void);
void search(int);

struct Node
{
    int data;
    struct Node *next;
};

typedef struct Node node;
node *head=NULL,*curr=NULL;

int main()
{
    int ch,n,m;
    do
    {
        printf("ENter your choice\n1.Insert end of list\n2.Insert at position\n3.Delete\n4.Search\n5.Display\n6.Exit\n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:
                    printf("Enter the data to insert\n");
                    scanf("%d",&n);
                    Insert_list(n);
                    break;
            case 2:
                    printf("Enter the data to insert\n");
                    scanf("%d",&n);
                    printf("Enter the position after the data is insert\n");
                    scanf("%d",&m);
                    Insert_pos(n,m);
                    break;
            case 3:
                    printf("Enter the data item to be delete\n");
                    scanf("%d",&n);
                    delete_list(n);
                    break;
            case 4:
                    printf("Enter the data item to be search\n");
                    scanf("%d",&n);
                    search(n);
                    break;
            case 5:
                    display();
                    break;
            case 6:
                    exit(0);
            default:
                    printf("Enter a valid digit\n");
                    break;
        }
    }while(ch>=1 && ch<=6);
}
void Insert_list(int n)
{
    node *newnode;
    //creating a new node with data n
    newnode=(node *)malloc(sizeof(node));
    newnode->data=n;
    newnode->next=NULL;
    //check the head node. If head is NULL then the list is empty
    //insert this node as the head node
    if(head==NULL)
    {
        head=newnode;
        curr=head;
    }
    else
    {
        //curr is the pointer pointing the last node of the list
        curr->next=newnode;
        curr=newnode;
    }
}

void Insert_pos(int n,int pos)
{
    node *nnode,*newnode;
    int i;
    if(head==NULL)
    {
        printf("The list is empty\n");
        return;
    }
    //assigning a temporary pointer nnode to point the head node
    nnode=head;
    //finding the position in the list
    for(i=0;i<pos-1;i++)
    {
        nnode=nnode->next;
        if(nnode==NULL) //reached end of list
        {
            printf("There are less than %d nodes in the list\n",pos);
            return;
        }
    }
    //found the position to insert
    //create a newnode with given data n
    newnode=(node *)malloc(sizeof(node));
    newnode->data=n;
    newnode->next=NULL;
    //inserting after the position
    newnode->next=nnode->next;
    nnode->next=newnode;
    printf("Node with value %d inserted after %d position\n",n,pos);
    return;
}

void delete_list(int n)
{
//Search the given item in the list if it is present delete or print a message
    node *nnode,*temp;
    if(head==NULL)
    {
        printf("The List is Empty\n");
        return;
    }
    //If the data is present in the first node
    if(head->data==n)
    {
        temp=head;
        head=head->next;
        free(temp);
        return;
    }
    nnode=head;

    while(nnode->next->next!=NULL)
    {
        if(nnode->next->data==n)
        {
            temp=nnode->next;
            nnode->next=temp->next;
            free(temp);
            printf("node with value %d is deleted\n",n);
            return;
        }
        else
        {
            nnode=nnode->next;
        }
    }
    //Deleting the last node
    if(nnode->next->data==n)
    {
        temp=nnode->next;
        nnode->next=NULL;
        free(temp);
        printf("The node with value %d is deleted\n",n);
        return;
    }
    //Reached end of list and the item is not present
    printf("The node with value %d is not found\n",n);
    return;
}

void search(int n)
{
    node *nnode;
    nnode=head;
    while(nnode)
    {
        if(nnode->data==n)
        {
            printf("Node with data %d found\n",n);
            return;
        }
        nnode=nnode->next;
    }
    printf("Node with data %d is not found\n",n);
}

void display()
{
    node *nnode;
    if(head==NULL)
    {
        printf("Empty list\n");
        return;
    }
    nnode=head;
    while(nnode)
    {
        printf("%d->",nnode->data);
        nnode=nnode->next;
    }
}

Exercise Programs in Linked list
  1. Modify sample program 1 to store items in the reverse order. ie while printing the list the output should be 10 9 8 7 6 5 4 3 2 1.

11 comments:

  1. Nice collection. Very useful for students! Thanks!!

    ReplyDelete
  2. do giv sum mre programs using linklist

    ReplyDelete
  3. very nice collection

    ReplyDelete
  4. nice but very typical make it easy for the biginners....!!ii

    ReplyDelete
  5. nice......gud job

    ReplyDelete
  6. good job!!!!!!!!

    ReplyDelete
  7. make it some what easy for the beginners

    ReplyDelete
  8. fine but not understude

    ReplyDelete
  9. make it simple .do it without switch case.do it using function

    ReplyDelete