Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Linked List Data Structure, Study notes of Computer Science

Contiguous memory allocation vs Linked memory allocation

Typology: Study notes

2019/2020

Available from 03/23/2023

raj-paliwal-1
raj-paliwal-1 🇮🇳

5 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Unit 3: Link List
Contiguous memory allocation vs Linked memory allocation
Difference between Sequential and Linked Organization:
S. No
Sequential Allocation
Linked Allocation
1.
Insertions and deletions are
difficult at runtime.
Insertions and deletions can be
done easily.
2.
It needs movements of elements
for insertion and deletion.
It does not need movement of
elements for insertion and
deletion.
3.
In it space is wasted.
In it space is not wasted.
4.
It is more expensive.
It is less expensive.
5.
It requires less space as only
information is stored.
It requires more space as pointers
are also stored along with
information.
6.
Its size is fixed.
Its size is not fixed.
7.
It can not be extended or
reduced according to
requirements.
It can be extended or reduced
according to requirements.
8.
Same amount of time is
required to access each element.
Different amount of time is
required to access each element.
9.
Elements are stored in
consecutive memory locations.
Elements may or may not be
stored in consecutive memory
locations.
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Linked List Data Structure and more Study notes Computer Science in PDF only on Docsity!

Unit 3: Link List

Contiguous memory allocation vs Linked memory allocation

Difference between Sequential and Linked Organization:

S. No Sequential Allocation Linked Allocation

  1. Insertions and deletions are difficult at runtime.

Insertions and deletions can be done easily.

  1. It needs movements of elements for insertion and deletion.

It does not need movement of elements for insertion and deletion.

  1. In it space is wasted. In it space is not wasted.
  2. It is more expensive. It is less expensive.
  3. It requires less space as only information is stored.

It requires more space as pointers are also stored along with information.

  1. Its size is fixed. Its size is not fixed.

It can not be extended or reduced according to requirements.

It can be extended or reduced according to requirements.

  1. Same amount of time is required to access each element.

Different amount of time is required to access each element.

  1. Elements are stored in consecutive memory locations.

Elements may or may not be stored in consecutive memory locations.

If have to go to a particular element then we can reach there directly.

If we have to go to a particular element then we have to go through all those elements that come before that element.

Dynamic Memory Allocation Process

The process of allocating memory at runtime is known as dynamic memory allocation. Library routines known as memory management functions are used for allocating and freeing memory during execution of a program. These functions are defined in stdlib.h header file.

Function Description

malloc() allocates requested size of bytes and returns a void pointer pointing to the first byte of the allocated space

calloc() allocates space for an array of elements, initialize them to zero and then returns a void pointer to the memory

free releases previously allocated memory

realloc modify the size of previously allocated space

Allocating block of Memory

malloc() function is used for allocating block of memory at runtime. This function reserves a block of memory of the given size and returns a pointer of type void. This means that we can assign it to any type of pointer using typecasting. If it fails to allocate enough space as specified, it returns a NULL pointer.

Syntax:

void* malloc(byte-size)

Time for an Example: malloc() int *x;

Diffrence between malloc() and calloc() calloc() malloc()

calloc() initializes the allocated memory with 0 value.

malloc() initializes the allocated memory with garbage values.

Number of arguments is 2 Number of argument is 1

Syntax :

(cast_type *)calloc(blocks , size_of_block);

Syntax :

(cast_type *)malloc(Size_in_bytes);

Linked List:

A linked list is a dynamic data structure consist of sequence of nodes, which are connected together by links (addresses).

Linked List is a sequence of linked nodes which contains items (Data).

Each node holds its own data and the address of the next node.

Linked list is the second most-used data structure after array.

Following are the important terms to understand the concept of Linked List.

Node − Each node of a linked list can store a data called an element.

Next − Each node of a linked list contains a link (address) to the next node called Next.  Previous - Each node of a Doubly linked list contains a link (address) to the previous node called Previous.

Linked List Representation

Linked list can be visualized as a chain of nodes, where every node points to the next node.

Types of Linked List

Following are the various types of linked list.

Singly Linked List − Item navigation is forward only.  Doubly Linked List − Items can be navigated forward and backward.  Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous.

SCLL

DCLL

What is Single Linked List?

Simply a list is a sequence of data, and the linked list is a sequence of data linked with each other. The formal definition of a single linked list is as follows...

Single linked list is a sequence of elements in which every element has link to its next element in the sequence.

In any single linked list, the individual element is called as "Node". Every "Node" contains two fields, data field, and the next field. The data field is used to store actual value of the node and next field is used to store the address of next node in the sequence. The graphical representation of a node in a single linked list is as follows...

 Insert at between

 Delete a Head Node

  Delete a Last node

 Delete a node in between:

Before we implement actual operations, first we need to set up an empty list. First, perform the following steps before implementing actual operations.

Step 1 - Include all the header files which are used in the program.  Step 2 - Declare all the user defined functions.  Step 3 - Define a Node structure with two members data and nextStep 4 - Define a Node pointer 'head' and set it to NULL.  Step 5 - Implement the main method by displaying operations menu and make suitable function calls in the main method to perform user selected operation.

Insertion

In a single linked list, the insertion operation can be performed in three ways. They are as follows...

  1. Inserting At Beginning of the list
  2. Inserting At End of the list
  3. Inserting At Specific location in the list

Inserting At Beginning of the list

void insertatbegg()

{

newNode=new Node;

if(head==NULL)

{

newNode->next=NULL;

head=newNode;

}

if(head==NULL)

{

head=newNode;

newnode->next==NULL;

}

Else

{

while(temp!=NULL)

{

temp=temp->next;

}

Temp->next=newNode;

}

Inserting At Specific location in the list (After a Node)

We can use the following steps to insert a new node after a node in the single linked list...

Step 1 - Create a newNode with given value.  Step 2 - Check whether list is Empty ( head == NULL )  Step 3 - If it is Empty then, set newNode → next = NULL and head = newNode.  Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.

Step 5 - Keep moving the temp to its next node until it reaches to the node after which we want to insert the newNode (until temp1 → data is equal to location , here location is the node value after which we want to insert the newNode).  Step 6 - Every time check whether temp is reached to last node or not. If it is reached to last node then display 'Given node is not found in the list!!! Insertion not possible!!!' and terminate the function. Otherwise move the temp to next node.  Step 7 - Finally, Set ' newNode → next = temp → next ' and ' temp → next = newNode '

Deletion

In a single linked list, the deletion operation can be performed in three ways. They are as follows...

  1. Deleting from Beginning of the list
  2. Deleting from End of the list
  3. Deleting a Specific Node

Deleting from Beginning of the list

We can use the following steps to delete a node from beginning of the single linked list...

Step 1 - Check whether list is Empty ( head == NULL )  Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.  Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.  Step 4 - Check whether list is having only one node ( temp → next == NULL )  Step 5 - If it is TRUE then set head = NULL and delete temp (Setting Empty list conditions)  Step 6 - If it is FALSE then set head = temp → next , and delete temp.

Deleting from End of the list

We can use the following steps to delete a node from end of the single linked list...

Step 1 - Check whether list is Empty ( head == NULL )  Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.  Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and ' temp2' and initialize ' temp1 ' with head.  Step 4 - Check whether list has only one Node ( temp1 → next == NULL )  Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate the function. (Setting Empty list condition)

Step 5 - Finally display temp → data with arrow pointing to NULL ( temp → data ---> NULL ).

Double Linked List

What is Double Linked List?

In a single linked list, every node has link to its next node in the sequence. So, we can traverse from one node to other node only in one direction and we can not traverse back. We can solve this kind of problem by using double linked list. Double linked list can be defined as follows...

Double linked list is a sequence of elements in which every element has links to its previous element and next element in the sequence.

In double linked list, every node has link to its previous node and next node. So, we can traverse forward by using next field and can traverse backward by using previous field. Every node in a double linked list contains three fields and they are shown in the following figure...

Here, 'link1' field is used to store the address of the previous node in the sequence, 'link2' field is used to store the address of the next node in the sequence and 'data' field is used to store the actual value of that node.

Example

☀ In double linked list, the first node must be always pointed by head. ☀ Always the previous field of the first node must be NULL. ☀ Always the next field of the last node must be NULL.

Advantages over singly linked list 1) A DLL can be traversed in both forward and backward direction. 2) The delete operation in DLL is more efficient if pointer to the node to be deleted is given. 3) We can quickly insert a new node before a given node. In singly linked list, to delete a node, pointer to the previous node is needed. To get this previous node, sometimes the list is traversed. In DLL, we can get the previous node using previous pointer.

Disadvantages over singly linked list 1) Every node of DLL Require extra space for an previous pointer. It is possible to

Inserting At Beginning of the list

We can use the following steps to insert a new node at beginning of the double linked list...

Step 1: Create a newNode with given value and newNode → previous as NULL.  Step 2: Check whether list is Empty ( head == NULL )  Step 3: If it is Empty then, assign NULL to newNode → next and newNode to head.  Step 4: If it is not Empty then, assign head to newNode → next, assign newNode to head->prev and newNode to head.

Inserting At End of the list

We can use the following steps to insert a new node at end of the double linked list...

Step 1: Create a newNode with given value and newNode → next as NULL.  Step 2: Check whether list is Empty ( head == NULL )  Step 3: If it is Empty , then assign NULL to newNode → previous and newNode to head.  Step 4: If it is not Empty , then, define a node pointer temp and initialize with head.

Step 5: Keep moving the temp to its next node until it reaches to the last node in the list (until temp → next is equal to NULL ).  Step 6: Assign newNode to temp → next and temp to newNode → previous.

Inserting At Specific location in the list (After a Node)

We can use the following steps to insert a new node after a node in the double linked list...

Step 1: Create a newNode with given value.  Step 2: Check whether list is Empty ( head == NULL )  Step 3: If it is Empty then, assign NULL to newNode → previous & newNode → next and newNode to head.  Step 4: If it is not Empty then, define two node pointers temp1 & temp and initialize temp1 with head.  Step 5: Keep moving the temp1 to its next node until it reaches to the node after which we want to insert the newNode (until temp1 → data is equal to location , here location is the node value after which we want to insert the newNode).  Step 6: Every time check whether temp1 is reached to the last node. If it is reached to the last node then display 'Given node is not found in the list!!! Insertion not possible!!!' and terminate the function. Otherwise move the temp1 to next node.  Step 7: Assign newNode to temp1 → next , temp1 to newNode → previous , temp2 to newNode → next and newNode to temp2 → previous.

S

Deleting from End of the list

We can use the following steps to delete a node from end of the double linked list...

Step 1: Check whether list is Empty ( head == NULL )  Step 2: If it is Empty , then display 'List is Empty!!! Deletion is not possible' and terminate the function.  Step 3: If it is not Empty then, define a Node pointer 'temp' and initialize with head.  Step 4: Check whether list has only one Node ( temp → previous and temp → next both are NULL )  Step 5: If it is TRUE , then assign NULL to head and delete temp. And terminate from the function. (Setting Empty list condition)  Step 6: If it is FALSE , then keep moving temp until it reaches to the last node in the list. (until temp → next is equal to NULL )

Step 7: Assign NULL to temp → previous → next and delete temp.

Deleting a Specific Node from the list

We can use the following steps to delete a specific node from the double linked list...

Step 1: Check whether list is Empty ( head == NULL )  Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.  Step 3: If it is not Empty, then define a Node pointer 'temp' and initialize with head.  Step 4: Keep moving the temp until it reaches to the exact node to be deleted or to the last node.  Step 5: If it is reached to the last node, then display 'Given node not found in the list! Deletion not possible!!!' and terminate the fuction.  Step 6: If it is reached to the exact node which we want to delete, then check whether list is having only one node or not  Step 7: If list has only one node and that is the node which is to be deleted then set head to NULL and delete temp ( free(temp) ).  Step 8: If list contains multiple nodes, then check whether temp is the first node in the list ( temp == head ).  Step 9: If temp is the first node, then move the head to the next node ( head = head → next ), set head of previous to NULL ( head → previous = NULL ) and delete temp.  Step 10: If temp is not the first node,