2. Dynamic memory location:
• The process of allocating memory at run time is known as dynamic memory
allocation.
• C does not inherently have this facility. There are four library routines known as
memory management functions that can be used for allocating and freeing
memory during execution.
• malloc(), calloc(), free(), realloc()
• All are defined in alloc.h
Prepared by: Moniruzzaman_CSE_KU_190231
3. malloc: Allocating a block of memory
• General form
• ptr = (cast-type *) malloc(byte-size);
• ptr is a pointer of type cast-type
• The malloc() returns a pointer (of cast-type) to an area of memory with size byte-size. If
there is not enough space a NULL pointer is returned.
• Example
• x=(int *) malloc(100*sizeof(int));
• cptr=(char *) malloc(10);
• st= (struct *) malloc(sizeof(struct store));
• Storage space allocated dynamically has no name and therefore its contents can be
accessed only through a pointer.
Prepared by: Moniruzzaman_CSE_KU_190231
4. calloc: Allocating multiple block of memory
• While malloc() allocates a single block of storage space, calloc() allocates
multiple blocks of storage, each of the same size, and then set all bytes to zero.
If there is not enough space a NULL pointer is returned.
• General form
• ptr=(cast-type *) calloc(n, element-size);
• we
Prepared by: Moniruzzaman_CSE_KU_190231
5. free( ); Releasing the used space
• With dynamic run-time allocation, we need to release the space when it is not
required
• free(ptr);
• Frees previously allocated space created by malloc() or calloc()
Prepared by: Moniruzzaman_CSE_KU_190231
6. realloc( ); Altering the size of a block
• It is likely that the previous allocated memory is not sufficient and we need
additional space for more elements.
• It is also possible that the memory allocated is much larger than necessary and
we want to reduce it.
• ptr=realloc(ptr, newsize);
• Modifies the size of previously allocated space by malloc() or calloc().
Prepared by: Moniruzzaman_CSE_KU_190231
7. Linked List
• A list refers to a set of items organized sequentially
• Linked list is a completely different way to represent a list is to make each item
in the list part of a structure that also contains a “link” to the structure containing
the next item.
• A linked list is a dynamic data structures.
Prepared by: Moniruzzaman_CSE_KU_190231
8. Difference between array and linked list
Basic It is a consistent set of a fixed number of data
items.
It is an ordered set comprising a variable
number of data items.
Size Specified during declaration. No need to specify; grow and shrink during
execution
Storage
Allocation
Element location is allocated during compile
time.
Element position is assigned during run time.
Order of the
elements
Stored consecutively Stored randomly
Accessing the
element
Direct or randomly accessed, i.e., Specify the
array index or subscript.
Sequentially accessed, i.e., Traverse starting
from the first node in the list by the pointer.
Array Linked List
Prepared by: Moniruzzaman_CSE_KU_190231
9. Difference between array and linked list
Insertion and
deletion of
element
Slow relatively as shifting is required. Easier, fast and efficient.
Searching Binary search and linear search linear search
Memory
required
less More
Memory
Utilization
Ineffective Efficient
Array Linked List
Prepared by: Moniruzzaman_CSE_KU_190231
10. Advantages of linked list
• Can grow or shrink in size during the execution of a program
• Does not waste memory space
• Here it is not necessary to specify the number of nodes to be used in the list
• It provides flexibility is allowing the items to be rearranged efficiently
• It is easier to insert or delete items by rearranging the links
Prepared by: Moniruzzaman_CSE_KU_190231
11. Limitation of Linked List
• The access to any arbitrary item is little cumbersome and time consuming.
• Use more storage than an array with the same number of items. This is because
each item has an additional link field.
• Linked lists are traversed in unidirection
• Complex to implement
• Sequential access to elements
• Leads to memory problems if not taken care about the pointer manipulations
properly
That is, Whenever we deal with a fixed length list, it would be better to use an array
rather than a linked list.
Prepared by: Moniruzzaman_CSE_KU_190231
12. Types of linked list:
Prepared by: Moniruzzaman_CSE_KU_190231
13. We need basic of pointer
Initiation :
pointer ptr contains the address of a variable
reference variable *ptr contains denotes the value of variables
int i;
int *j;
i=5;
j=&i;
*j= ? ans: 5
j= ? ans: 65524
p=&x;
p points to x
q=&y;
q points to y
Prepared by: Moniruzzaman_CSE_KU_190231
14. We need basic of pointer
Initialization: Assignment p=q
p=&x; 100 p=q; 100
p x p x
q=&y; 200 200
q y q y
*p=100 and *q=200 and p< >q *p=*q=200 but x< >y
Assignment *p=*q NULL pointers
*p=*q; 200 p=0; (or p= NULL;) p 0
p x p x
200 q=0; (or q=NULL;) q 0
q y
x=y=200 but p< >q p and q points nothing
Prepared by: Moniruzzaman_CSE_KU_190231
15. What we can do with Linked List:
• Creating a list
• Traversing the list
• Counting the items in the list
• Printing the list (or sub list)
• Looking up an item for editing or printing
• Inserting an item
• Deleting an item
• Concatenating two lists
Prepared by: Moniruzzaman_CSE_KU_190231
16. What we can do with Linked List:
• Replace() : replaces a value stored at a position with some other value.
• Swap() : swap the values of the nodes specified by two positions.
• Merging two linked lists into a larger list
• Searching for an element in a linked list
• Reversing a linked list
Prepared by: Moniruzzaman_CSE_KU_190231
17. Program: (creating a linked list) Singly_1
#include<stdio.h>
#include<stdlib.h>
void createList(int);
struct node
{
int data;
struct node *right;
} *start=NULL, *end;
//typedef node x; // node type defined
//x *start=0, *end; //start r head 1
int main( )
{
int x;
printf("Enter the no. of data: ");
createList(x);
return 0;
}
Prepared by: Moniruzzaman_CSE_KU_190231
18. Program: (creating a linked list) Singly_2
void createList(int x) {
int i, num;
struct node *newNode ;
if(x>=1){
start = (struct node*)malloc(sizeof(struct node));
if(start!=NULL){
printf(“Enter data for node 1: ”);
scanf(“%d”,&num);
start->data=num;
start->right=NULL;
end = start;
for(i=2 ; i<=x ; i++){
newNode = (struct node*)malloc (sizeof(struct node));
if(newNode!=NULL){
Prepared by: Moniruzzaman_CSE_KU_190231
19. Program: (creating a linked list) Singly_3
printf(“Enter data for node %d: ”,i);
scanf(“%d”, &num);
newNode->data = num;
newNode->right = NULL;
end->right = newNode;
end = newNode;
}
else{
printf(“Memory not allocated”);
break;
}
}
}
else{
printf(“Memory not allocated”);
}
}
}
Prepared by: Moniruzzaman_CSE_KU_190231
20. Program: (creating a linked list) Doubly_1
#include<stdio.h> //same as singly changes shown in red text
#include<stdlib.h>
void createList(int);
struct node
{
int data;
struct node *right;
struct node *left;
} *start=NULL, *end=NULL;
//typedef node x; // node type defined
//x *start=0, *end; //start r head 1
int main( )
{
int x;
printf("Enter the no. of data: ");
while (scanf("%d",&x))
createList(x);
return 0;
}
Prepared by: Moniruzzaman_CSE_KU_190231
21. Program: (creating a linked list) Doubly_2
void createList(int x) { //same as singly changes shown in red text
int i, num;
struct node *newNode ;
if(x>=1){
start = (struct node*)malloc(sizeof(struct node));
if(start!=NULL){
printf(“Enter data for node 1: ”);
scanf(“%d”,&num);
start->data=num;
start->right=NULL;
start->left=NULL;
end = start;
for(i=2 ; i<=x ; i++){
newNode = (struct node*)malloc (sizeof(struct node));
if(newNode!=NULL){
Prepared by: Moniruzzaman_CSE_KU_190231
22. Program: (creating a linked list) Doubly_3
printf(“Enter data for node %d: ”,i);
scanf(“%d”, &num);
newNode->data = num;
newNode-> left = end;
newNode->right = NULL;
end->right = newNode;
end = newNode;
}
else{
printf(“Memory not allocated”);
break;
}
}
}
else{
printf(“Memory not allocated”);
}
}
}
Prepared by: Moniruzzaman_CSE_KU_190231
23. Program: (printing/traversing a linked list)
//Let start denotes the first node of the list
#include<stdio.h> // Singly or doubly are same
#include<stdlib.h>
Void display( );
struct node{
int data;
struct node *right;
} *temp;
Int main( ){
display( );
return 0;
}
void displayList(){
int i=1;
temp=start;
while(temp!=NULL){
printf("Node_%d: %dn",i,temp->data);
i++;
temp=temp->right;
}
} Prepared by: Moniruzzaman_CSE_KU_190231
24. Program: Checking an item in Linked List
void check(){ // Singly or doubly are same
int lf, count=0, pos=0; //1st er oi main() r struct j kono ans e likhte hobe
printf("nWhat you want to search? ");
scanf("%d",&lf);
p = start;
while (p!= NULL){
pos++;
if(p->data==lf){
printf("n%d is in position %dn", lf , pos);
p=p->right;
count++ ;
}
else{
p=p->right;
}
}
if(count==0){
printf("Given data doesn't exist");
}
}
Prepared by: Moniruzzaman_CSE_KU_190231
25. Program: Replace of any data
void editData(){ // Singly or doubly are same
int old,new, count=0; //1st er oi main() r struct j kono ans e likte hobe
printf("Enter the old data: ");
scanf("%d",&old);
printf("Enter the new data: ");
scanf("%d",&new);
p=start;
while(p!=NULL){
if (p->data!=old){
p=p->right;
}
else{
p->data = new;
p=p->right;
count++;
}
}
if(count==0){
printf("The given data doesn't exist in the list");
}
}
Prepared by: Moniruzzaman_CSE_KU_190231
26. Program: Counting data in a linked list
//Let start denotes the first node of the list
#include<stdio.h> // Singly or doubly are same
#include<stdlib.h>
Void countData( );
struct node{
int data;
struct node *right;
} *temp;
Int main( ){
countData( );
return 0;
}
void countData(){
int count=0;
temp=start;
while(temp!=NULL){
count++;
temp=temp->right;
}
printf(“No. of Data: %d”, count);
} Prepared by: Moniruzzaman_CSE_KU_190231
27. Program: Inserting node in a linked list_1
//Let start denotes the first node of the list
#include<stdio.h> // Singly or doubly are same
#include<stdlib.h> //red code for making doubly
void insertFirst( int);
Void insertLast(int );
Void insertMiddle(int );
struct node{
int data;
struct node *right, *left;
} *temp;
Int main( ){
int newData, position;
printf("Enter the new data");
scanf("%d",&newData);
printf("Enter the position");
scanf("%d",&position);
switch(position){
case 1: {
Prepared by: Moniruzzaman_CSE_KU_190231
28. Program: Inserting node in a linked list_2
insertFirst(newData);
break; //red code for making doubly
}
case 2: {
insertLast(newData);
break ;
}
case 3: {
insertMiddle(newData);
break;
}
default: exit(0);
}
}
void insertFirst(int x){
struct node *newInsert = (struct node *)malloc(sizeof(struct node));
newInsert->data=x;
newInsert->right=start;
newInsert->left = NULL;
start=newInsert;
} Prepared by: Moniruzzaman_CSE_KU_190231
30. Program: Inserting node in a linked list_4
newInsert->data= x ; //red code for making doubly
newInsert->right= temp->right ;
newInsert->left = temp ;
temp-> right = newInsert ;
newInsert->right->left = newInsert; //newInsert->right ei tuku jeta add korbo
} //tar porer node indicate kore r tar->left
//Diagram:
Prepared by: Moniruzzaman_CSE_KU_190231
31. Program: Deleting a node from linked list_1
//Let ‘start’ denotes the first node of the list
//Let ‘end’ denotes the last node of the list
#include<stdio.h> // Singly or doubly are same
#include<stdlib.h> //red code for making doubly
void deleteFirst( );
Void deleteLast( );
Void deleteSpecific( );
struct node{
int data;
struct node *right, *left;
} *temp;
Int main( ){
int position;
printf(“press 1 to delete from first, 2 for last and 3 for specific positoin");
scanf("%d",&position);
switch(position){
case 1: {
Prepared by: Moniruzzaman_CSE_KU_190231
32. Program: Deleting a node from linked list_2
deleteFirst( );
break; //red code for making doubly
}
case 2: {
deleteLast( );
break ;
}
case 3: {
deleteSpecific(newData);
break;
}
default: exit(0);
}
}
void deleteFirst( ){
temp=start;
start= start->right;
start->left = NULL;
free(temp);
}
Prepared by: Moniruzzaman_CSE_KU_190231
33. Program: Deleting a node from linked list_3
void deleteLast( ){ //same code for making doubly
struct node *prev; //and also have a very simple way
temp = start;
while (temp->right!=NULL){ void deleteLast( ){
prev = temp; temp = start;
temp = temp -> right; while(temp->right !=NULL){
} temp=temp->right;
if(temp==head){ }
head=NULL; temp->left->right= NULL;
} free(temp);
else{ }
prev->right = NULL;
}
free(temp);
}
Prepared by: Moniruzzaman_CSE_KU_190231
34. Program: Deleting a node from linked list_4
void deleteSpecific( ) { //red code for making doubly
int pos, count=1;
struct node *prev;
printf(“Enter the deleting position”);
scanf(“%d”,&pos);
temp=start;
while(count!=pos){
prev=temp;
temp= temp->right;
count++;
}
prev->right= temp->right;
temp->right->left = prev;
free(temp);
}
Prepared by: Moniruzzaman_CSE_KU_190231