C program to implement a list library (singlylist.h) for a singly linked list with the below six operations. Write a menu driven driver program to call the operations.

Singly Linked List – Dynamic Implementation

 An abstract data type List is an ordered set of elements where insertions and deletions are possible from any position. Implementing List statically using an array to store elements is costly as insertions and deletions require moving of array elements. List is efficiently implemented dynamically using Linked list. The linked list is a series of nodes where each node contains the data element and a link to the node containing the next element. The data element can be integer, character or user defined type. A list is a single entity which is a pointer to the first node of the linked list. A dummy node is used as header of the list so that it is not affected by insertions or deletions.

Operations on Linked List:

append(L, x):

 Insert the data element x by creating the node containing the data element x and inserting it in last position.

insert (L, x, pos)  :

inserts the data element x by creating the node containing the data element x and inserting it in position pos. The links are appropriately changed. If pos equals 1, the node is inserted in first position immediately after the header. If pos is greater than the nodes present in the list, the node is added at the end of the list.

 search (L, x) :

Searches for the data element x and returns the pointer to the node containing x if x is present or returns NULL.

 delete (L, x):

Deletes the node containing the data element x by appropriately modifying the links.

delete (L, pos) :

Delete the node at a position given by pos. If the position pos is invalid then display appropriate message. 

display (L): 

Displays all the data elements in the list

Program :

#include<stdio.h>
#include "singlylist.h"

int main()
{
    node *head;
    head=(node*)malloc(sizeof(node));
    head->next=NULL;

    createlist(head);
    int num,choice,pos;
    do
    {
        printf("\n1.Append\n2.Insert\n3.search\n4.deleteValue\n5.deletePos\n6.display\n7.Exit\n");
        printf("\nEnter choice:");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:printf("Enter number to append:");
            scanf("%d",&num);
            append(head,num);
            break;
            case 2:printf("Enter number to insert:");
            scanf("%d",&num);
            printf("Enter position:");
            scanf("%d",&pos);
            insert(head,num,pos);
            break;
            case 3:printf("Enter number to search:");
            scanf("%d",&num);
            search(head,num);
            break;
            case 4:printf("Enter number to delete:");
            scanf("%d",&num);
            deletevalue(head,num);
            break;
            case 5:printf("Enter postion to delete:");
            scanf("%d",&pos);
            deletePos(head,pos);
            break;
            case 6:
            display(head);

        }
    }while(choice!=7);
}

output :


How many nodes:4
Enter the node value:1 2 3 4

1.Append
2.Insert
3.search
4.deleteValue
5.deletePos
6.display
7.Exit

Enter choice:1
Enter number to append:7

1.Append
2.Insert
3.search
4.deleteValue
5.deletePos
6.display
7.Exit

Enter choice:6
The list is :
1       2       3       4       7
1.Append
2.Insert
3.search
4.deleteValue
5.deletePos
6.display
7.Exit

Enter choice:2
Enter number to insert:8
Enter position:5

1.Append
2.Insert
3.search
4.deleteValue
5.deletePos
6.display
7.Exit

Enter choice:6
The list is :
1       2       3       4       8       7
1.Append
2.Insert
3.search
4.deleteValue
5.deletePos
6.display
7.Exit

Enter choice:3
Enter number to search:8
Search Found at position 5
1.Append
2.Insert
3.search
4.deleteValue
5.deletePos
6.display
7.Exit

Enter choice:4
Enter number to delete:2

1.Append
2.Insert
3.search
4.deleteValue
5.deletePos
6.display
7.Exit

Enter choice:6
The list is :
1       3       4       8       7
1.Append
2.Insert
3.search
4.deleteValue
5.deletePos
6.display
7.Exit

Enter choice:5
Enter postion to delete:3

1.Append
2.Insert
3.search
4.deleteValue
5.deletePos
6.display
7.Exit

Enter choice:6
The list is :
1       3       8       7
1.Append
2.Insert
3.search
4.deleteValue
5.deletePos
6.display
7.Exit

Enter choice:7

Process returned 0 (0x0)   execution time : 87.622 s
Press any key to continue.

Stack Library(singlylist.h):

You have to copy this code in your IDE and save it as 'singlylist.h'.

#include<stdio.h>
#include<malloc.h>
typedef struct node
{
    int info;
    struct node *next;
} node;//create node structure

void createlist(node *head)
{
    int i,n;
    node *last,*newnode;
    printf("How many nodes:");
    scanf("%d",&n);
    printf("Enter the node value:");

    last=head;
    for(i=1; i<=n; i++)
    {
        newnode=(node*)malloc(sizeof(node));
        scanf("%d",&newnode->info);
        newnode->next=NULL;
        //attach newnode to last
        last->next=newnode;
        last=newnode;
    }
}
void display(node *head)
{
    node *temp;
    printf("The list is :\n");
    for(temp=head->next; temp!=NULL; temp=temp->next)
        printf("%d\t",temp->info);
}
void append(node *head,int num)
{
    node *newnode,*temp;
    newnode=(node*)malloc(sizeof(node));
    newnode->info=num;
    newnode->next=NULL;
  for(temp=head;temp->next!=NULL;temp=temp->next);
    temp->next=newnode;
}
void insert(node* head,int num,int pos)
{
node *newnode,*temp;
int  i;
newnode=(node*)malloc(sizeof(node));
newnode->info=num;
newnode->next=NULL;
for(i=1,temp=head;i<pos;temp=temp->next,i++);
if(temp==NULL)
    printf("Position out of range");
else
{
    newnode->next=temp->next;
    temp->next=newnode;
}

}
void search(node *head,int num)
{
   node *temp;
   int pos;
    for(pos=1,temp=head->next;temp!=NULL;temp=temp->next,pos++)
    if(temp->info==num)
    {
    printf("Search Found at position %d",pos);
    break;
    }
    if(temp==NULL)
        printf("Not Found");
}
void deletevalue(node *head,int num)
{
node *temp,*temp1;
  for(temp=head;temp->next!=NULL;temp=temp->next)
        if(temp->next->info==num)
  {
      temp1=temp->next;
      temp->next=temp1->next;
      free(temp1);
      break;
  }
}
void deletePos(node *head,int pos)
{
 node *temp,*temp1;
 int i;
  for(i=1,temp=head;i<pos;temp=temp->next,i++);
   temp1=temp->next;
      temp->next=temp1->next;
      free(temp1);

}

//Please Give your feedback in comments

Post a Comment

0 Comments