DS Lesson 2
DS Lesson 2
LISTS
LINKED LIST:
A linked list is a linear data structure, in which the elements are not stored at contiguous memory
locations. The elements in a linked list are linked using pointers as shown in the below image
In simple words, a linked list consists of nodes where each node contains a data field and a
reference(link) to the next node in the list
In a singly linked list, each node contains a reference to the next node in the sequence. Traversing a
singly linked list is done in a forward direction.
✓ Insertion at the start Insertion of a new node at the start of a singly linked list is carried out in the
following manner.
• Make the new node point to HEAD.
• Make the HEAD point to the new node.
✓Insertion after some Node Insertion of a new node after some node in a singly linked list is carried out
in the following manner,
• Reach the desired node after which the new node is to be inserted.
• Make the new node point to the next element of the current node.
• Make the current node point to the new node. Inserting a new node after some node is an O(N)
operation
Insertion at the end Insertion of a new node at the end of a singly linked list isperformed in te following
way,
• Taverse the list from start and reach the last node.
• Make the last node point to the new node.
• Make the new node point to null, marking the end of the list. Inserting a new node at the end is an O(N)
operation.
2.Deletion:
Deletion at last
The deletion of the last node is performed in the following manner,
3.Display
To display the entire singly linked list, we need to traverse it from first to last.
In contrast to arrays, linked list nodes cannot be accessed randomly. Hence to reach the n-th element,
we are bound to traverse through all (n-1) elements.
Since the entire linked list is traversed, the operation costs us O(N) time complexity. The following JAVA
snippet shows how the entire singly linked list can be displayed
4. Search
To search an element in the singly linked list, we need to traverse the linked list right from the start.
At each node, we perform a lookup to determine if the target has been found, if yes, then we return the
target node else we move to the next element.
In the worst case, we could end up visiting all the nodes in the list and hence searching an element in the
singly linked list cost us O(N) operational time.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node // CREATING A NODE
{
int info;
struct node *link;
}*start;
void create(int);
void display();
void insert_end(int);
void insert_beg(int);
void insert_pos(int,int);
void delete_beg();
void delete_pos(int);
void delete_end();
main()
{
int choice,data,choice1,choice2,pos,no_of_node;
start=NULL;
while(1)
{
clrscr();
printf("1.Create\n");
printf("2.Display\n");
printf("3.Insert node\n");
printf("4.Delete node\n");
printf("5.Exit\n");
printf("Enter ur choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter no. of node to be created:");
scanf("%d",&no_of_node);
create(no_of_node);
break;
case 2: display();
break;
case 3: clrscr();
printf("1.Insert node at beginning\n");
printf("2.Insert node at specific position\n");
printf("3.Insert node at end of list\n");
printf("4.Previous menu\n");
printf("Enter choice:");
scanf("%d",&choice1);
switch(choice1)
{
case 1: printf("Enter data for node");
scanf("%d",&data);
insert_beg(data);
break;
case 2:printf("Enter data for node:");
scanf("%d",&data);
printf("Enter the position to insert\n");
scanf("%d",&pos);
insert_pos(data,pos);
break;
case 3: printf("Enter data for node:");
scanf("%d",&data);
insert_end(data);
break;
case 4: break;
}break;
case 4: clrscr();
printf("1.Delete node at beginning\n");
printf("2.Delete node at specific position\n");
printf("3.Delete node at end of list\n");
printf("4.Previous menu\n");
printf("Enter choice:");
scanf("%d",&choice2);
switch(choice2)
{
case 1:delete_beg();
break;
case 2:printf("Enter the position to insert\n");
scanf("%d",&pos);
delete_pos(pos);
break;
case 3:delete_end(data);
break;
case 4: break;
}break;
case 5: exit(1);
default: printf("Invalid entry");
}
}
}
void create(int no) // CREATING A NODE
{
int i,data;
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
for(i=0;i<no;i++)
{
if(start==NULL)
{
printf("Enter data for node %d:",i);
scanf("%d",&data);
temp->info=data;
temp->link=NULL;
start=temp;
printf("Enter Data for node %d:",i);
scanf("%d",&data);
insert_end(data);
if(no>0)
printf("List created");
else
printf("List not created");
getch();
clrscr();
}
void display() // DISPLAYING A NODE
{
struct node *ptr;
ptr=start;
clrscr();
if(start==NULL)
{
printf("List is empty");
getch();
return;
}
printf("Linked List\n");
while(ptr!=NULL)
{
printf("%d->",ptr->info);
ptr=ptr->link;
}
printf("End_of_list");
getch();
clrscr();
}
void insert_end(int data) // INSERTING AT THE END
{
struct node *ptr,*tempnode;
ptr=start;
while(1)
{
if(ptr->link!=NULL)
ptr=ptr->link;
else
break;
}
tempnode = (struct node *)malloc(sizeof(struct node));
tempnode->info=data;
tempnode->link=NULL;
ptr->link=tempnode;
}
void insert_beg(int data) // INSERTING AT THE BEGINNING
{
struct node *tempnode;
tempnode = (struct node *)malloc(sizeof(struct node));
tempnode->info=data;
tempnode->link=start;
start = tempnode;
}
void insert_pos(int data,int pos)
{
int i;
struct node *tempnode,*ptr;
ptr=start;
for(i=1;i<pos;i++)
{
if(ptr==NULL)
{
printf("Invalid Position Entered");
getch();
return;
}
ptr=ptr->link;
}
if(ptr->link==NULL)
{
insert_end(data);
}
else
{
tempnode = (struct node *)malloc(sizeof(struct node));
tempnode->info=data;
tempnode->link=ptr->link;
ptr->link = tempnode;
}
}
void delete_beg() // DELETING AT THE BEGINNING
{
struct node *ptr;
ptr = start;
if(start==NULL)
{
printf("List is empty");
getch();
return;
}
start = ptr->link;
free(ptr);
}
void delete_pos(int pos) // DELETING AT THE POSITION
{
int i;
struct node *tempnode,*ptr;
ptr=start;
for(i=1;i<pos;i++)
{
if(ptr==NULL)
{
printf("Invalid Position Entered");
getch();
return;
}
ptr=ptr->link;
}
if(ptr->link==NULL)
{
delete_end();
}
else
{
tempnode = ptr->link;
ptr->link = ptr->link->link;
free(tempnode);
}
}
void delete_end() // DELETING AT END OF THE LIST
{
struct node *ptr,*prvptr;
ptr=start;
while(1)
{
if(ptr->link!=NULL)
{
prvptr=ptr;
ptr=ptr->link;
}
else
break;
}
prvptr->link=NULL;
free(ptr);
}
OUTPUT:
1. Create
2. Display
3. Insert node
4. Delete node
Enter your choice : 1
1->2->4
A doubly linked list (DLL) is a special type of linked list in which each node contains a pointer to the
previous node as well as the next node of the linked list.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
int info;
struct node *nxt,*prv;
}*start;
void create(int);
void display();
void insert_end(int);
void insert_beg(int);
void insert_pos(int,int);
void delete_beg();
void delete_end();
void delete_pos(int);
main()
{
int ch,data,ch1,ch2,nof,pos;
start=NULL;
while(1)
{
clrscr();
printf("\n1.Create\n2.Display\n3.Insert\n4.Delete\n5.Exit\n");
printf("\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nNumber of nodes to be created\n");
scanf("%d",&nof);
create(nof);
break;
case 2:
display();
break;
case 3:
clrscr();
printf("\n1.Insert node at beginning\n2.Insert node at specific position\n3.Insert node at
end\n4.Previous menu\n");
printf("\nEnter your choice\t");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("\nEnter the data for node\n");
scanf("%d",&data);
insert_beg(data);
break;
case 2:
printf("\nEnter the data for node\n");
scanf("%d",&data);
printf("\nEnter the position to insert\n");
scanf("%d",&pos);
insert_pos(data,pos);
break;
case 3:
printf("\nEnter the data for node\n");
scanf("%d",&data);
insert_end(data);
break;
case 4:
break;
}break;
case 4:
clrscr();
printf("\n1.Delete node at beginning\n2.Delete node at specific position\n3.Delete node at
end\n4.Previous menu\n");
printf("\nEnter your choice\t");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
delete_beg();
break;
case 2:
printf("\nEnter the position to delete\n");
scanf("%d",&pos);
delete_pos(pos);
break;
case 3:
delete_end();
break;
break;
case 4:
}break;
case 5:
exit(1);
default:
printf("Invalid entry");
}
}
}
void create(int no)
{
int i,data;
struct node *temp;
clrscr();
temp=(struct node *)malloc(sizeof(struct node));
for(i=0;i<no;i++)
{
if(start==NULL)
{
}
else
{
}
}
printf("\nEnter the data for node %d:",i);
scanf("%d",&data);
temp->info=data;
temp->nxt=NULL;
temp->prv=NULL;
start=temp;
printf("\nEnter the data for node %d:",i);
scanf("%d",&data);
insert_end(data);
if(no>0)
printf("List created");
else
printf("List is not created");
getch();
clrscr();
}
void display()
{
struct node *ptr;
ptr=start;
clrscr();
if(start==NULL)
{
return;
printf("\nList is empty");
getch();
}
printf("\nLinked list");
while(ptr!=NULL)
{
printf("\t%d->",ptr->info);
ptr=ptr->nxt;
}
printf("\nEnd of list");
getch();
clrscr();
}
void insert_end(int data)
{
struct node *ptr,*tempnode;
ptr=start;
while(1)
{
else
}
if(ptr->nxt!=NULL)
ptr=ptr->nxt;
break;
tempnode=(struct node *)malloc(sizeof(struct node));
tempnode->info=data;
tempnode->nxt=NULL;
tempnode->prv=ptr;
ptr->nxt=tempnode;
}
void insert_beg(int data)
{
struct node *tempnode;
tempnode=(struct node *)malloc(sizeof(struct node));
tempnode->info=data;
tempnode->nxt=start
start=tempnode;
}
void insert_pos(int data,int pos)
{
int i;
struct node *tempnode,*ptr;
ptr=start;
for(i=1;i<pos;i++)
{
if(ptr==NULL)
{
printf("Invalid position");
getch();
return;
}
ptr=ptr->nxt;
}
if(ptr->nxt==NULL)
insert_end(data);
else
{
tempnode=(struct node *)malloc(sizeof(struct node));
tempnode->info=data;
tempnode->nxt=ptr->nxt;
tempnode->prv=ptr;
ptr->nxt=tempnode;
(tempnode->nxt)->prv=tempnode;
}
}
void delete_beg()
{
struct node*ptr;
ptr=start;
if(start==NULL)
{
printf("List is empty");
getch();
return;
}
start=ptr->nxt;
free(ptr);
}
void delete_pos(int pos)
{
int i;
struct node *tempnode,*ptr;
ptr=start;
for(i=1;i<pos;i++)
{
if(ptr==NULL)
{
printf("Invalid position");
getch();
return;
}
ptr=ptr->nxt;
}
if(ptr->nxt==NULL)
delete_end();
else
{
tempnode=ptr->nxt;
ptr->nxt=tempnode->nxt;
free(tempnode);
}
}
void delete_end()
{
struct node *ptr;
ptr=start;
while(1)
{
if(ptr->nxt!=NULL)
{
ptr=ptr->nxt;
}
else
break;
}
(ptr->prv)->nxt=NULL;
free(ptr);
}
OUTPUT:
1. Create
2. Display
3. Insert node
4. Delete node
Enter your choice : 1
The circular linked list is a linked list where all nodes are connected to form a circle. In a circular linked
list, the first node and the last node are connected to each other which forms a circle. There is no NULL
at the end.
1 . Circular singly linked list: In a circular Singly linked list, the last node of the list contains a pointer to
the first node of the list. We traverse the circular singly linked list until we reach the same node where we
started. The circular singly linked list has no beginning or end. No null value is present in the next part of
any of the nodes.
2 . Circular Doubly linked list: Circular Doubly Linked List has properties of both doubly linked list and
circular linked list in which two consecutive elements are linked or connected by the previous and next
pointer and the last node points to the first node by the next pointer and also the first node points to the
last node by the previous pointer.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
struct node *nextptr;
}*stnode;
// Function prototypes
void ClListcreation(int n);
void displayClList();
int main() {
int n;
stnode = NULL;
if (n >= 1) {
stnode = (struct node *)malloc(sizeof(struct node));
// Loop to create subsequent nodes and link them to form a circular list
for (i = 2; i <= n; i++) {
newnode = (struct node *)malloc(sizeof(struct node));
printf(" Input data for node %d : ", i);
scanf("%d", &num);
newnode->num = num;
newnode->nextptr = NULL; // Setting the next address of the new node as NULL
preptr->nextptr = newnode; // Linking the previous node with the new node
preptr = newnode; // Advancing the previous node to the new node
}
preptr->nextptr = stnode; // Linking the last node with the first node to form a circular list
}
}
if (stnode == NULL) {
printf(" No data found in the List yet.");
} else {
tmp = stnode;
printf("\n\n Data entered in the list are :\n");
OUTPUT:
MULTI LISTS:
A multi-linked list is a special type of list that contains two or more logical key sequences. Before
checking details about multi-linked list, see what is a linked list. A linked list is a data structure that is
free from any size restriction until the heap memory is not full. We have seen different types of linked
lists, such as Singly Linked List, Circular Linked List, and Doubly Linked List. Here we will see about
multi-linked list.
In a multi-linked list, each node can have N number of pointers to other nodes. A multi-linked list is
generally used to organize multiple orders of one set of elements.
The structure of a multi-linked list depends on the structure of a node. A single node generally contains
two things:
1. A list of pointers
2. All the relevant data.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct scorel
{
int no;
struct scorel *rp;
};
struct namel
{
char a[10];
struct scorel *rp;
struct namel *dp;
};
void main()
{
int i,j,n,m;
struct namel *temp,*head,*end,**s;
struct scorel *tmp,*had,*nd;
clrscr();
printf("enter no of students");
scanf("%d",&n);
end=NULL;
for(i=0;i<n;i++)
{
temp=(struct namel*)malloc(sizeof(struct namel));
printf("enter name\n");
scanf("%s",temp->a);
temp->rp=NULL;
temp->dp=NULL;
*(s+i)=temp;
if(end==NULL)
{
end=head=temp;
}
else
{
end->dp=temp;
end=temp;
}
printf("enter no of scores");
scanf("%d",&m);
nd=NULL;
for(j=0;j<m;j++)
{
tmp=(struct scorel*)malloc(sizeof(struct scorel));
printf("enter score\n");
scanf("%d",&tmp->no);
tmp->rp=NULL;
if(nd==NULL)
{
nd=had=tmp;
temp->rp=tmp;
}
else
{
nd->rp=tmp;
nd=tmp;
}
}
}
for(i=0;i<n;i++)
{
temp=*(s+i);
printf("%s-->",temp->a);
tmp=temp->rp;
while(tmp!=NULL)
{
printf("%d-->",tmp->no);
tmp=tmp->rp;
}
printf("\n");
}
getch();
}
OUTPUT:
enter no of students:
Name:
No of scores:
Enter scores: