Ds Mod3
Ds Mod3
Ds Mod3
Memory Wastage :
• It is a run-time concept
static data
space for global variables
automatic data
run-time stack - activation records added
and removed as program runs (expands
and shrinks in an orderly LIFO manner)
info link
Singly linked list :
A singly linked list is a linked list, where
each node has designated field called link field which contains
the address of the next node.
The below figure shows the representation of a singly
linked list consisting of the items 50,20,45,10 and 80.
Syntax :
struct node
{
type 1 info;
type 2 *link;
}
Link :
It contains address of the next node. So, link field must be
pointer to a node which can be declared as shown below,
first info
first link
Now the question is where the nodes are present and
how to get these nodes.
Available list :
The list of free available nodes is called available list.
Avail
------------ \o
1000 1040 1020 1010
getnode :
The getnode() is a function which removes the first node
from the available list and makes it available for the user. The
getnode operation can be treated as a machine that
manufactures nodes. Each time the getnode is invoked, it gives
a new node from the available list.
Syntax :
Node first;
----------- ;
----------- ;
first = getnode();
The getnode () function is implemented based on whether the
available list is empty or not,
20 45 10 80 \0
• Insertion steps :
4
• temp first
1 50 20 45 10 80 \0
2 3
first After insertion
50 20 45 10 80 \0
// function to insert a node
temp --------------------------------------
// function to display contents of list
void display (NODE first )
{
NODE temp; // function 3.
if ( first == NULL )
{
Printf (“ list is empty” ); return;
}
temp = first;
while ( temp != NULL )
{
printf ( “ %d “, temp info );
temp = temp link;
}
Delete a node from front end : The following steps will explain
the deletion of a node,
temp
1 20 45 10 80 \0
temp
2 20 45 10 80 \0
4 temp
3 20 45 10 80 \0
45 10 80 \0
first
// function to delete a node
NODE delete_front ( NODE first )
{
NODE temp; // function 4
if ( first == NULL )
{
printf ( “list is empty” );
return first;
}
temp = first;
temp = temp link;
printf (“ item deleted = %d”, first info);
freenode (first);
return temp;
}
// Complete C – program
# include < stdio.h >
# include < alloc.h >
# include < process.h >
Strust node
{
int info;
struct node * link;
}; typedef struct node * NODE;
// function 1
// function 2
// function 3
// function 4
Void freenode ( NODE first )
{
free (first );
}
void main ()
{
Node first = NULL;
int choice, item;
for ( ; ; )
{
printf (“ 1.insert 2.delete 3.display 4.exit”);
printf ( “ enter your choice “);
scanf (“ %d”, & choice);
Switch ( choice )
{
case 1 : printf ( “ Enter the item to be inserted “ );
scanf ( “%d” , & item);
first = insert_front (item, first);
break;
case 2 : first = delete_front ( first );
break;
case 3 : display ( first );
break;
default : exit (0);
}
}
}
Insert a node at the rear end :
• Before insertion :
first temp
20 45 10 80 \0 55 \0
first temp
20 45 10 80 \0 55 \0
---------------------------------------
cur
Insert a node at the rear end :
first temp
20 45 10 80 \0 55 \0
• After insertion :
first
20 45 10 80 55 \0
// Algorithm to insert an item at the rear end of the list
Step 2 : Copy item = 50 to the info field of temp and make link
field of temp to null.
temp info = item ; temp link = null;
20 45 10 80 \0
First cur
-------------------------------------
20 45 10 80 \0
-------------------------
prev
first cur
20 45 10 80 \0
prev
freenode ( cur );
prev link = NULL;
After deletion :
first
20 45 10 \0
// function to delete an item at the rear end of the list
NODE delete_rear ( NODE first )
{
NODE prev;
NODE cur;
if ( first == NULL )
{
printf ( “ list is empty “ );
return first;
}
if ( first link == NULL )
{
printf (“ The item deleted is %d”, first info);
freenode ( first ); return NULL;
}
// Continued….
prev = NULL;
cur = first;
while ( cur link != NULL )
{
prev = cur;
cur = cur link;
}
printf ( “ The item deleted is %d”, cur info);
freenode ( cur );
prev link = NULL;
return first;
}
Creating an ordered linked list:
A linked list in which the items are stored in
some specific order is an Ordered linked list.
The elements in an ordered list can be in
ascending or descending order based on key information.
first
10 20 30 40 \0
Step 1:
Create a node to be inserted & insert the item information
Using the following statements,
temp = getnode();
temp -> info = item; 10 \0
temp -> link = NULL;
Step 2:
If the node temp is inserted into the list for the first time
(i.e, into the memory list) then return temp itself as the first
node using the following code.
first
10 20 30 40 \0
Step 4:
Once the node temp is inserted at the front end, let us return
temp indicating temp itself is the first node of the list using,
return temp;
temp first
10 10 20 30 40 \0
10 20 30 40 \0
So,
Prev =Null; Cur= first; 35
While (cur! = Null && item > cur -> info)
{
Prev =cur;
Cur= cur-> link;
}
Step 6: Insert at middle/end;
prev-> link = temp;
temp-> link = cur;
10 20 30 40 50 \0
//C function to create an Ordered linked list.
NODE insert_ord (int item, NODE first)
{
NODE temp, prev, cur;
temp = getnode();
temp-> info=item; step 1
temp-> link=Null;
if (first==NULL) return temp; step 2
if(item < first-> info)
{
temp-> link = first; step 3 & 4
return temp;
}
Prev=NULL;
Cur=first;
While (cur!=NULL && item > cur-> info) step 5
{
prev=cur;
Cur=cur-> link;
}
prev-> link=temp; step 6
Temp->link=cur;
return first; step 7
}
*Search for an item in a list:
// Function to search for key in the list.
if(first==NULL)
{
Printf(“list is empty”);
Return;
}
Cur=first;
While (cur!=NULL)
{
If(key == cur-> info)
break;
cur = cur-> link;
}
if(cur== NULL)
{
printf(“search is unsuccessful’);
return;
}
printf(“search is successful’);
}
Delete a node at the specified position:
NODE delete_pos(int pos, NODE first)
{
NODE cur;
NODE Prev;
Int count;
If (first == NULL || pos <=0)
{ Printf (“Invalid position “);
Return NULL:
}
if (pos==1)
{ Cur=first;
first=first-> link;
freenode(cur);
return first;
}
prev =NULL;
Cur =first; Count=1;
While (cur !=NULL)
{
If (count== pos)
break;
prev =cur;
cur=cur-> link;
}
if (count! =pos)
{
printf(“Invalid Position”);
return first;
}
prev -> link = cur -> link;
freenode(cur);
return first;
}
Insert a node at the specified position.
Count =1;
prev=NULL:
cur=first;
While (cur!=NULL && count!=pos)
{
prev =cur;
cur=cur-> link;
Count ++;
}
If(count == pos)
{
Prev->link=temp;
Temp->link=cur;
Return first;
}
Printf(“Invalid position”);
Return first;
}
Concatenate two lists:
first * Before Concatenation
10 20 40 \0
second
10 20 40 \0
* After Concatenation
first cur
10 20 40 10 20 40 \0
// C function to concatenate two lists
NODE concat (NODE first, NODE second)
{
NODE cur;
if(first == NULL) return second;
if(second == NULL) return first;
cur=first;
While (cur-> link ! =null)
{
cur=cur-> link;
}
cur-> link = second ;
return first;
}
Header Node:
\0
header node
20 30 40 \0
Circular list with a header node :
In a circular list with a header node, the link field
of the last node contains address of the header node & the link
field of the header node contains the address of the first node.
header node
20 30 40
Insert a node at the front :
Step1:
Obtain a new node
temp = getnode();
temp -> info=item;
Step2: Make a link to first node.
temp-> link = head-> link;
Step 3: Reassign the head pointer
head -> link = temp;
Step 4: Return the address of header,
return head;
Before insertion :
head * Before insertion
20 30 40
10
head 3 2
4
20 30 40
// function to insert at the front end.
temp= getnode();
temp->info=item;
}
Insert a node at the rear end:
head * Before insertion
20 30 40
* After insertion
head cur temp
20 30 40 50
// function to insert at the rear end of the list
prev = head;
cur = head->link;
// Delete a node whose info field is specified:
While(cur!=head)
{
if(item== cur->info) break;
prev=cur;
cur=cur->link;
}
if(cur==head)
{
printf(“item not found \n”); return head;
}
prev-> link=cur-> link;
freenode(cur);
return head;
}
Insert a node at the specified position :
head * Before insertion
20 30 40
* After insertion
head prev cur
20 30 40
50
temp
// Insert a node at the specified position :
NODE insert_position (int item, int pos, NODE head)
{
NODE Prev, cur, temp;
int i;
prev=head;
cur=head->link;
count=1;
Whil;e(cur!=head)
{
if(count==pos) Break;
prev=cur;
cur=cur->link;
count++;
}
// Insert a node at the specified position :
if(count!=pos)
{
printf(“invalid position”);
return head;
}
temp=getnode();
temp->info=item;
prev-> link=temp;
temp->link=cur;
return head;
}
Delete a node at the specified position :
head * Before delition
20 30 40
* After insertion
head prev cur
20 30 40
prev =head;
cur = head-> link;
count = 1;
While(cur!=head)
{
if(count ==pos)
break;
prev = cur;
cur = cur->link;
count++;
}
// Delete a node at the specified position :
if(count!=pos)
{
printf(“ Invalid positon”);
return head;
}
prev->link=cur-> link;
freenode(cur);
return head;
}
// Display the contents of circular list:
Void display (NODE head)
{ NODE temp;
if(head-> link == head)
{
printf(“List is empty”); return;
}
printf(“contents of the list is”);
temp = head->link;
While(temp!=head)
{
printf(“%d”,temp->info);
temp = temp->link;
}
}
//complete “c” program for circular linkedlist.
#include<stdio.h>
#include<alloc.h>
#include<process.h>
Struct node
{
int info;
Struct node * link;
};
head=getnode();
head->info=0;
head->link=head;
for(;;)
{
printf(“1.Insert_front 2. Insert_at_postion”);
printf(“3.Delete_item 4.Delete_at_postion”);
printf(“5. Display 6.Exit”);
printf(“enter the choice”);
Scanf(“%d”, & choice);
Switch(choice)
{
Case1:
Printf(“enter the item to be inserted”);
Scanf(“%d”,&item);
head = insert_front(item,head);
break;
Case2 :
printf(“enter the item to be inserted”);
Scanf(“%d”,&item);
printf(“at position=”);
Scanf(“%d”, &pos);
head = insert_position(item,pos,head);
break;
Case3:
printf(“enter the item to be deleted”);
Scanf(“%d”,&item);
head = delete_item(item,head);
break;
Case4:
printf(“position of node to delete”);
Scanf(“%d”,&pos);
head = delete_position(pos,head);
break;
Case5:
display(head); break;
Default:
exit(0);
} }
Static allocation vs. Linked allocation
Static allocation technique Linked allocation technique
Memory is allocated during compilation time. Memory is allocated during execution time
The size of the memory to be allocated is fixed As & when memory is required, memory can
during compilation time & cab not be altered be allocated. If not required, memory can be
during execution time. deallocated. The size of the memory required
may vary during execution time.
used only when the data size is fixed & known used only for unpredictable memory
in advance before processing. requirement
Execution is faster, since memory is already Execution is slower since memory has to be
allocated & data manipulation is done on allocated during runtime. Data manipulation
these allocated memory locations. is done only after allocating the memory.
Memory is allocated either in stack area (for Memory is allocated only in heap area
local variables) or data area (for global &
static variable).
Data accessing is very fast & is done by Data accessing is very slow. Data stored in the
specifying the array name along with index. beginning of the list can be accessed very fast.
Time take to access a[0] & a[1000], in general But, to access any other data, the list has to be
a[i] is same. traversed & definitely requires more time.
Static allocation vs Linked allocation
Static allocation technique Linked allocation technique
It is difficult to insert or delete the data in The data can be inserted anywhere in the
the beginning or middle of the array. list. Addition or deleting of a node is done
First, we have to make a room by just by manipulating the links.
copying the items into successive locations
& then insert. While deleting an item, the
subsequent elements should be copied
into previous locations.
If we can predict the size of the data or if If the size of the data can not be
the size of the data is known in advance, predicted or number of items to be
then it is better to go for static allocation. inserted is not known in advance, then it
is better to go for linked allocation.
For n items only n memory locations are For n items, apart from n memory
allocated. locations extra memory required for each
node to store the address of the next
node. So, more memory space is used for
n items when compared with arrays.
DOUBLY LINKED LISTS:
NODE
\o 20 10 20 30 \0
* Circular DLL
first
20 10 20 30
Header node
10 20 30
\0 10 \0 10 \0
//To insert a node to Doubly Linked List based on position.
Step1:
Create a newnode & assign NULL to left & right pointer;
ie; NODE newnode;
newnode -> info = x;
newnode -> left = newnode -> right = NULL;
Step2:
Check for valid position.
ie; if ((Pos < 1) || (Pos > (size + 1)))
invalid position
free(newnode);
//To insert a node to Doubly Linked List based on position.
Step3:
Suppose position is equal to 1,
header
header 10 \0
10 \0
\0 12 \0 12
newnode newnode
Step 4: Suppose position greater than 1.
Find the position & insert.
header
10 12 \0
newnode
\0 11 \0
* Insert at position 2
header
prev next
10 12 \0
newnode 11
Step4: Suppose position greater than 1.
Step1: Create some temporary nodes like Prev, Pres, & next
NODE Prev, Pres, next;
Prev = Pres = header;
Step2: Check for valid position
if(Pos < 1) || (Pos > size) || (header == NULL))
Invalid position;
Step3: If position is equal to 1
if(Pos == 1)
Header = header -> right;
header header
10 \0 \0 \0
Step4: If position is greater than 1. find position & delete
NODE temp;
temp = header;
if(header == NULL)
printf("List is empty“);
while(temp!=NULL)
{
printf(“temp->info”);
temp = temp->right;
}
10 20 30
//C Function to insert a node at the front end of the list.
NODE insert_front(int item,NODE head)
{
NODE temp ,cur;
temp=getnode();
temp-> info=item;
cur=head->rlink;
head->rlink=temp;
temp->llink=head;
temp->rlink=cur;
cur-> llink=temp;
return head;
}
//C Function to insert a node at the end of the list.
programs will be length & need more using circular linked list with header,
time to design efficient & small programs can be
written & hence design is easier.
care is taken to modify only one link care is taken to modify both links of a
of a node node.