DS Module 3
DS Module 3
• Memory Management:
- malloc()
- calloc()
- realloc()
- free()
• Linked List:
o Definition,
o Representation
• Header nodes
1
Data Structures with Algorithms 20MCA11
Module -3: Linked List
2. Memory Management
• What is DMA?
- The process of allocating memory during program execution is called dynamic memory
allocation.
- Dynamic memory allocation allows you to manually handle memory space for your
program.
2
Data Structures with Algorithms 20MCA11
Module -3: Linked List
• What is heap?
- This is a separate memory area maintained by the compiler which is logical separation in
RAM.
- Initially, entire heap area is available for dynamic allocation, so, the amount of memory
- calloc()
- realloc()
- free()
• Advantages
- The main advantage of dynamic memory allocation is to save memory from unnecessary
3
Data Structures with Algorithms 20MCA11
Module -3: Linked List
• malloc()
- The name malloc stands for "memory allocation".
- It allocates requested size Bytes in Heap memory.
- Return a starting address of allocated location of type void*.
- Return address must be type casted into pointer of required type.
- Example:
5.5
ptr 260
260
Stack
- Pointer 'ptr' is allocated in stack. DMA function malloc() allocates 4 bytes from
Heap and returns its address to pointer ptr.
#include<stdio.h>
Ql. Example on malloc() function.
#include<conio.h>
void main()
{
int *p;
clrscr();
p = (int*)
malloc(sizeof(int));
if(p==NULL)
{
printf("ERROR\n");
exit(l);
}
4
Data Structures with Algorithms 20MCA11
Module -3: Linked List
*p=10;
getch();
}
Output:
oos
tOH
' DOSBox 0.74, Cpu speed: max 100% cycles, Frames
P hnl<ls thr, ct<l<lrr,ss = 1 1.4
P pninting(hnl<l ct<l<lrr,ss) VctillP-
= 10
A<l<lrr,ss of P = h /.4
• calloc()
- The name calloc stands for "contiguous allocation".
- It allocates multiple blocks of memory each of same size and sets all bytes to zero.
- Example
int *ptr = (int*) calloc (3, sizeof(int));
Heap
5
Data Structures with Algorithms 20MCA11
Module -3: Linked List
Write a Program to dynamically allocate an array of 3 locations of int type. Read values
into the locations and display the same.
#include<stdio.h>
#include<conio.h>
void main()
{
int *ptr;
int i;
clrscr();
printf("\nEnter
Elements: \n"),
for(i=0; i<3; i++)
scanf("%d", &ptr[i]);
6
Data Structures with Algorithms 20MCA11
Module -3: Linked List
Output:
• realloc()
o Helps to increase or shrink the previously allocated memory.
- Assume that you have allocated few blocks of memorv using malloc() or calloc( ).
- Now you may want tu mcrease 01 Jecrease me previously allocated memory, obviously,
you can't go for the earlier two tunct1ons, but we have anomer function in C that can
solve this problem.
- Example
• free()
o This function releases the memory allocated previously by malloc( ) or calloc( ) or
realloc( ).
o free( ) does not return anything.
- Example
Free (ptr)
7
Data Structures with Algorithms 20MCA11
Module -3: Linked List
#include<stdio.h> #include<conio.h>
void main()
{
char *s; clrscr();
s = (char*) malloc(4);
s = "RNS";
printf("Before realloc() s = %s \n", s); s = (char*) realloc(s, 10);
s = "RNSIT-MCA";
Output:
8
Data Structures with Algorithms 20MCA11
Module -3: Linked List
3. Linked List
• Definition
o Linked list is a linear collection of data elements, called nodes, each pointing to the next
node by means of a pointer.
o Each node is composed of data and a reference (link) to the next node.
o Allows for efficient insertion or removal of elements from any position.
• Representation
HEADER
• Creating a node
o struct
A variable Listto above List structure is called Node.
created
{ pl, p2;
NODE /* pl and p2 are two nodes */
int data;
struct node *link;
};
typedef struct List NODE;
9
Data Structures with Algorithms 20MCA11
Module -3: Linked List
Operations: getnode()
A node can be created dynamically.
The method getnode() creates a node dynamically.
NODE *getnode()
{
NODE *p;
p = (NODE*) malloc(sizeof(NODE)); if(p == NULL)
{
printf("\n Memory Not Allocated !");
exit(0);
}
p->link = NULL; return p;
}
• Operations: Freenode()
o Dynamically de-allocate a given node passed as parameter.
void freenode(NODE *temp)
{
free(temp);
}
- Inserting a node
- Deleting a node
- Searching a node
- Traversing a node
10
Data Structures with Algorithms 20MCA11
Module -3: 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:
Head
A
Data Next
IJ-( IJ-{ IJ- NULL
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.
11
Data Structures with Algorithms 20MCA11
Module -3: Linked List
• Memory Diagram
,,,.
/ ---- --..., .
/ head
I
J
- -
' . I 2 1 NUL
7..---. .
L -
4
_j_ -1
-- -
.
-
return start;
}
12
Data Structures with Algorithms 20MCA11
Module -3: Linked List
• Memory Diagram
START
3 2 1
newnode
if(start==NULL)
{
return newnode;
}
cur= start;
whiLe(cur->Link != NULL) cur= cur->Link;
cur->Link = newnode;
printf("\n Node at End Inserted Successfully ..\n");
return start;
}
13
Data Structures with Algorithms 20MCA11
Module -3: Linked List
start prev
@I 2001
"-I 10!200 H l
201300 I I401
200' •Joo
,
100 \
\ I
301300
400
14
Data Structures with Algorithms 20MCA11
Module -3: Linked List
Step3: Copy link part of start to start pointer. (start= start link)
Step4: Display data part of 'cur' Node and remove the node.
• Memory Diagram
01 02 03 ....,NULL
- Step2: move start to next node and remove temp using free()
Start
02 03 NULL
f.
free(temp)
15
Data Structures with Algorithms 20MCA11
Module -3: Linked List
Step2: if link part of 'start' is NULL then list has only one node
-Mark link part of 'prev' with NULL and remove Node at 'last'.
16
Data Structures with Algorithms 20MCA11
Module -3: Linked List
• Memory Diagram
- Step1: Use previous and current pointer to point to last and previous nodes
- Step2: Set link part of previous to NULL and remove last node pointed by current.
START PftEV
3 NULL
C-function:
NODE* removeEnd Remove node from end of list
(NODE *start)
{
node *prevJ *Last;
if ( start == NULL)
{ printf (" .L 1.s'C' is
Empt:y..JJ); return st:art;
}
Last= prev = start;
if ( start Link ==
NULL) start=
NULL;
whiLe ( Last Link ! = NULL) /* search last node */
{
prev = Last;
Last = Last Link J
}
prev Link =
NULL; free
(Last);
17
Data Structures with Algorithms 20MCA11
Module -3: Linked
List
4.6 Remove node at a given position.
$1,.... 1 00
1
start
$ I....----
-- _10_l_2_0H.20..l... ,. 3 1.o 40I
100200"',
'
◄
., 400
.,
18
Data Structures with Algorithms 20MCA11
Module -3: Linked List
19
Data Structures with Algorithms 20MCA11
Module -3: Linked List
20
Data Structures with Algorithms 20MCA11
Module -3: Linked List
top
o Display(top) :
This function remains same as Display of Linked list. Top will be pointing to the
node which is inserted in last.
21
Data Structures with Algorithms 20MCA11
6. Header Nodes
Header Nodes
Sometimes it is desirable to keep an extra 1.1ode al the front of a list. Sucha node does n
Cal
Cbl
(cl
ldl
Ir)
22
Data Structures with Algorithms 20MCA11
Module -3: Linked List
- Header node is an extra node that contains the address of the first node of the linked list. If
this node contains NULL, then this shows that linked list is empty.
- It is an extra node kept at the front of a list. Such a node does not represent an item in the list.
The information portion is generally used to store extra information like the number of nodes
present in the linked list.
- The space for header node is not allocated until the first node is created. Header node itself
is pointed by a pointer called head (or start) pointer.
4 8 -+-- 10
Start Header Data Link
Node
23
Data Structures with Algorithms 20MCA11
Module -3: Linked List
24
Data Structures with Algorithms 20MCA11
Module -3: Linked List
25
Data Structures with Algorithms 20MCA11
Module -3: Linked List
Lab Program: 6
Write a C program to simulate the working of a singly linked list providing the following operations:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define max 20
struct Student
{
int data;
struct NODE *link;
};
typedef struct Student NODE;
NODE *getNODE()
{
NODE *p;
p = (NODE*) malloc(sizeof(NODE));
if(p == NULL)
{
printf("\n Memory Not Allocated !");
exit(0);
}
return p;
}
26
Data Structures with Algorithms 20MCA11
// Insert Functions
newNODE=getNODE();
newNODE->data = data;
newNODE->link = start;
start= newNODE;
return start;
}
// removeFront Functions
• )
return start;
}
temp= start;
start= start->link;
); free (temp);
return start;
}
27
Data Structures with Algorithms 20MCA11
// removeEnd Functions
if ( start->link == NULL)
{
printf("Element %d deleted successfully",start->data);
return NULL;
}
last= last->link
}
prev->link = NULL;
free (last);
return start;
}
28
Data Structures with Algorithms 20MCA11
// removeElement Functions
if (start== NULL)
{
printf("---Empty List. Cant delete- - - -");
return start;
}
cur= start;
if (start->data == element)
{
printf("\n***Item Deleted is %d **\n", start->data);
start= start->link,
free (cur);
return start;
}
while(cur != NULL)
{
if(cur->data == element)
{
prev->link = cur->link;
printf("\n NODE with Element: %d deleted successfully!..\n",cur->data);
free(cur);
return start;
}
prev = cur;
cur= cur-
>link;
}
return start;
}
29
Data Structures with Algorithms 20MCA11
// Display Functions
if(start == NULL)
{
printf("\n List is Empty----\n\n");
return;
}
temp=start;
while(temp!=NULL)
{
printf("%d \t",temp->data);
temp=temp->link;
}
}
void main()
{
int ch, data, pos, element;
clrscr();
while(l)
{
printf("\n *********LINKED LIST************\n");
printf("\n 1. Insert");
printf("\n 2. Delete at front");
printf("\n 3. Delete at end");
printf("\n 4. Delete a given element");
printf("\n 5. Display");
printf("\n 6. Exit");
printf("\n\n Enter your choice: ");
scanf("%d",&ch);
30
Data Structures with Algorithms 20MCA11
Module -3: Linked List
clrscr();
switch(ch)
{
case 1:
printf("\n Enter a data: ");
scanf("%d",&data);
flushall();
case 2:
printf("\n ** Deletion at Front ***\n");
start= removeFront(start);
break;
case 3:
printf("\n ** Deletion at End ***\n");
start= removeEnd(start);
break;
case 4:
printf("\n ** Delete a given element ***\n");
case 5:
Display(start);
break;
default: exit(0);
}
}
}
31