Lec 4 Iinked List
Lec 4 Iinked List
Lecture 4
Linked list
Instructor:
DR. Nada El-Meligy
Dynamic Memory Allocation
Agenda
Introduction to linked list
• Traversing a list
• Insertion item.
Dynamic Memory
Allocation
Problems with normal array variable declaration
int main()
{
int A[10]; // Fixed size
int B[n]; // Error ..Size must be known at compile time.
}
Main Memory
• Our program when loaded into main memory is divided into 4
segments: CODE, DATA, STACK, Heap.
• A data segment contains the global variables and static variables.
• A code segment contains the executable instructions.
• A stack segment store all the auto variables. Also, Used for function
call’s return, addresses, arguments and local variables.
• Hence a stack is an area of memory for storing data temporarily.
Heap and Stack
• Allocation and deallocation of memory in this area is done automatically.
• Heap segment is for dynamic memory management.
• It is for the programmers to manage.
• We can allocate memory when you feel the need for it and delete when you
feel that the memory is no longer needed.
• And it is responsibility of the programmer to delete memory when no longer
needed.
• Hence for array we can dynamically allocate memory of size which can be
determined at run time.
• In C++ we use new and delete.
Heap and Stack (Cont.)
int main()
{ int I;
float *ptr;
cout<<"Enter array size ";
cin>>I;
ptr=new float[I];
cout<<"Enter your elements:"<<endl;
for (int k=0;k<I;k++)
cin>> *(ptr+k);
cout<<"the elements in array:"<<endl;
for (int k=0;k<I;k++)
cout<< *(ptr+k)<<endl;
delete [] ptr;
return 0;}
Linked List
Linked List
• A linked list is a dynamic data structure which allows to store data and manage data
efficiently. Typically, a linked list, in its simplest form looks like the following:
• There is a pointer (called head) points the first element (also called node)
• Successive nodes are connected by pointers.
• Last element points to NULL.
• It can grow or shrink in size during execution of a program.. It can be made just as
long as required. It does not waste memory space, consume exactly what it needs
head A B C NULL
Array versus Linked Lists
But what if you int main()
In arrays don’t know how {
many items you’ll int array[100];
• Arrays are static data structure have ahead of time? …
}
Then you have to
reserve enough slots
• Arrays are suitable for for the largest possible
case.
▪ Inserting/deleting an element at the end. And what if you arrayName
10
need to insert
0
15
▪ Randomly accessing any element(easy access to any a new item in
1
2 20
node
class node
To allocate new nodes:
{ Data
int data; /* Data */ node *new_node = new node();
node *next; /* pointer*/ next
} ;
To change/access a node p’s value:
new_node->data = 10;
cout << new_node->data;
To make node p link to another node
To make node q a “terminal” node: that’s at address q:
q->next = NULL; new_node->next = q;
Example 1: Illustration
class LL
class node
{public:
{public:
node *head;
string name;
LL()
int age;
{
node *next;
head=NULL;
};
}};
It creates a single node. For example, if the data entered is 100 then the list look like
head
100 NULL
Creating a single linked list
If we need n number of nodes in the linked list:
• Allocate n newNodes, one by one.
• Read in the data for the newNodes.
• Modify the links of the newNodes so that the chain is formed.
node *n1= new node();
node *n2= new node();
node *n3= new node();
head = n1;
n1-> data=100;
n1-> next=n2;
n2-> data=200;
n2-> next=n3;
n3-> data=50;
n3-> next=NUll;
It creates n number of nodes . For e.g. if the data entered is 100, 200, 50 then the list look like
head
100 200 50 NULL
Operations on single linked list
• Traversing a list
• Printing, etc.
• Searching an item in the list.
• Count number of items in the list.
• Insertion of a node into a list
• At front, end and anywhere, etc.
The function show()is given. This function to be called from main() function as:
void show()
{
node* temp = head;
while(temp!= NULL)
{
cout<< temp->data<<endl; //Prints the data of current
node
temp= temp->next; //Advances the position of current node
}
}
Single Linked List: searching an item in the list (search function)
bool search(int item)
{
node* temp = head;
while(temp!= NULL)
{ if (temp->data==item)
return true;
temp= temp->next; //Advances the position of current node
}
return false;
}
Insertion steps:
Steps to insert node at the beginning of singly linked list Step 2: Link the newly created node with
the head node, i.e. the newNode will now
Step 1: Create a new node. point to head node.
Step 3: Make the new node as the head node, i.e. now head node will point to newNode.
Insertion at front Function
Step 2: Traverse to the last node of the linked list and connect the last node of the list with
the new node, i.e. last node will now point to new node. (lastNode->next =
newNode).
Insertion at End Function
/* Create a new node and insert at the end of the linked
list. */
}
Single Linked List: Insertion at any Position
Steps to insert node at any position of Singly Linked List
Step 1: Create a new node. Step 2: Traverse to the n-1th position of
the linked list and connect the new node
with the n+1th node.
(newNode->next = temp->next)
where temp is the n-1th node.