Dsa Mod3 - Part1
Dsa Mod3 - Part1
Module 3
Linked Lists: Definition, Representation of linked lists in Memory, Linked list operations: Traversing,
Searching, Insertion, and Deletion, Doubly Linked lists Circular linked lists and header linked
operations: Traversing, Searching, Insertion, and Deletion, polynomial using header linked list.
1. The size of array must be known in advance before using it in the program.
2. All the elements in the array need to be contiguously stored in the memory.
3. Inserting any element in the array needs shifting of all its predecessors. Also, delete operations is
tedious.
4. Expanding or shrinking the size of the array is difficult.
Linked list is the data structure which can overcome all the limitations of an array. Using linked list is
useful because,
Note: No particular data structure is the best. The choice of the data structure depends on the kind of
application that needs to be implemented. While for some applications linked lists may be useful, for
others, arrays may be useful.
Use Arrays when we need quick access to elements, better memory efficiency, and when the data
size is static.
Use Linked Lists when the data size is dynamic, frequent insertions and deletions are needed, or
when implementing certain complex data structures.
Linked list is a linear data structure that consists of a sequence of elements where each element (usually
called a node) comprises of two items - the data and a reference (link) to the next node. The last node
has a reference to null.
The linked list contains a list variable called START or FIRST or HEAD, which contains the address of the first
node in the list. We need only this address in START to trace through the list.
The entry point into a linked list is called the head (start or first) of the list. It should be noted that
head is not a separate node, but the reference to the first node. If the list is empty then the start is a
null reference. The list with no nodes –empty list or null list.
Types of Linked List
1. Singly Linked List
2. Doubly Linked List
3. Circular Linked List
4. Header Linked List
2) A way to create new nodes when we need them. We use malloc() function for this.
3) A way to remove nodes that we no longer need. We use free() function for this.
head
One way chain or singly linked list can be traversed only in one direction. In other words, we can say
that each node contains only next pointer, therefore we cannot traverse the list in the reverse direction.
Consider an example where the marks obtained by the student in three subjects are stored in a linked list
as shown in the figure.
In the above figure, the arrow represents the links. The data part of every node contains the marks
obtained by the student in the different subject. The last node in the list is identified by the null pointer
which is present in the address part of the last node.
Node Creation
struct node
{
int data;
struct node *next;
};
struct node *head, *ptr;
ptr = (struct node *)malloc(sizeof(struct node));
SN Operation Description
Deletion at beginning
It involves deletion of a node
from the beginning of the list. It
4
just need a few adjustments in
the node pointers.
5 Deletion at the end of the list It involves deleting the last node
of the list.
Traversing
In traversing, we simply visit
each node of the list at least
once in order to perform some
7
specific operation on it, for
example, printing data part of
each node present in the list.
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
void begin_insert ();
void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at random location\n4.Delete from Beginning");
printf("\n5.Delete from last\n6.Delete node after specified location\n7.Search for an element\n8.Show\
n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
begin_insert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void begin_insert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted");
}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");
}
}
}
void randominsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter element value");
scanf("%d",&item);
ptr->data = item;
printf("\nEnter the location after which you want to insert ");
scanf("\n%d",&loc);
temp=head;
for(i=1;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\ncan't insert\n");
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
}
}
void last_delete()
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
ptr=head;
head = NULL;
free(ptr);
printf("\nOnly node of the list deleted ...\n");
}
else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}
}
void random_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=1;i<=loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL)
{
printf("\nCan't delete");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}
void search()
{
struct node *ptr;
int item,i=1,flag=0;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i);
flag=1;
break;
}
i++;
ptr = ptr -> next;
}
if(flag==0)
{
printf("Item not found\n");
}
}
void display()
{
int count=0;
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Nothing to print");
}
else
{
printf("\nprinting values . . . . .\n");
while (ptr!=NULL)
{
count++;
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
printf("\n The number of nodes %d",count);
}
}
Ex : 5
Title: Design, Develop and Implement a menu driven Program in C for handling change of branch.
Program:
#include<stdio.h>
#include<stdlib.h>
struct SLL
{
int usn;
char name[20];
char mode[20];
struct SLL *next;
};
typedef struct SLL node;
node *start=NULL;
node* createNode()
{
node *newnode;
newnode=(node*)malloc(sizeof(node));
printf("Enter the usn: \n");
scanf("%d",&newnode->usn);
printf("Enter the Name: \n");
scanf("%s",newnode->name);
printf("Enter the mode (Regular/Lateral/COB/COC): \n");
scanf("%s",newnode->mode);
newnode->next=NULL;
return newnode;
}
void insertend()
{
node *newnode,*temp;
newnode=createNode();
if(start==NULL)
{
start=newnode;
return;
}
temp=start;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
}
void display()
{
node* temp=start;
if(start==NULL)
{
printf("list is empty\n");
return;
}
printf("Students in the class are:\n");
printf("USN\tNAME\tMode\n");
while(temp!=NULL)
{
printf("%d\t%s\t%s\n",temp->usn,temp->name,temp->mode);
temp=temp->next;
}
printf("\n");
}
void deletekey()
{
node *temp=start,*prev;
int key;
if(start==NULL)
{
printf("List is empty. Deletion is not possible.\n");
return;
}
printf("Enter the USN to be deleted: \n");
scanf("%d",&key);
if (start->usn == key)
{
start = start->next;
printf("Deleted USN: %d\n",temp->usn);
free(temp);
}
else
{
while(temp!=NULL && temp->usn!=key)
{
prev=temp;
temp=temp->next;
}
if(temp==NULL)
{
printf("USN is not found, so can't delete.\n");
return;
}
prev->next=temp->next;
printf("Deleted USN: %d\n",temp->usn);
free(temp);
}
}
int main()
{
int choice,n,i;
while(1)
{
printf("\nEnter your choice:\n");
printf("1 for insert\n");
printf("2 for display\n");
printf("3 for delete students based on COC/COB\n");
printf("4 for adding students from lateral entry/COC/COB\n");
printf("5 for exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
case 4: printf("Enter the number of students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
insertend();
break;
case 2: display();
break;
case 3: deletekey();
break;
case 5: exit(0);
default:printf("Invalid chioce\n");
}
}
}