0% found this document useful (0 votes)
95 views10 pages

Linked Lists Are Dynamic in Nature"

Linked lists are dynamic data structures where memory can vary during use. They consist of nodes that contain data and a pointer to the next node. Common operations on linked lists include insertion and deletion of nodes. Insertion adds a new node after the current head node, making it the new head. Deletion removes a target node by adjusting the pointer of the previous node. Linked lists can implement stacks, queues, and priority queues through manipulating pointers to the head and tail of the list.

Uploaded by

sruthi1119
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views10 pages

Linked Lists Are Dynamic in Nature"

Linked lists are dynamic data structures where memory can vary during use. They consist of nodes that contain data and a pointer to the next node. Common operations on linked lists include insertion and deletion of nodes. Insertion adds a new node after the current head node, making it the new head. Deletion removes a target node by adjusting the pointer of the previous node. Linked lists can implement stacks, queues, and priority queues through manipulating pointers to the head and tail of the list.

Uploaded by

sruthi1119
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Linked lists

Introduction: Definition: It is a linear data structure where the amount of memory required can be varied during their use. Linked lists are dynamic in nature. In linked lists, adjacency between the elements is mainted by means of links or pointers. A link is actually the address of the subsequent element; an element in linked list is specially termed as node. Data field Address field

<----------------NODE------------------> As shown above the address field of each node is connected to the next successive nodes(if exits) otherwise the address field is left idle(i.e. null) here is an example to show how a linked list is created:

Here the first node A(is placed in the data field) and the address field is connected to the next node B, the address field is connected to node C and here there are no further notes to be connected so it is kept null. Examples:
Binary Trees, Stacks and Queues can be implemented with a doubly linked list, Relational Databases (e.g. Microsoft Access) etc

Operations on linked lists: The common operations in linked lists are: 1) Insertion 2) Deletion

Insertion of a node onto a list:

In the above fig(a) shows the insertion of a node onto a list, say we are inserting a NODE B to the list then here are some following steps we can follow: Algorithm:
Step1: allocate space for a new node, Step2: Copy the item into it, Step3: Make the new node's next pointer point to the current head of the list and Step4: Make the head of the list point to the newly allocated node. This strategy is fast and efficient, but each item is added to the head of the list.

Deletion of the node from the list: Fig(b) shows the deletion of a particular node from the list, say we are deleting a NODE N from the list then we need to follow few steps to proceed: Algorithm:
Step1: Take the address where the node we want to delete is present (i.e. Here TARGET is on node N). Step2: Make the previous node of TARGET to point to where TARGET is currently pointing Step3: The next pointer field of Node N now points to Node B, Where Node N previously pointed.

Step4: Return the value in that intermediate variable There are also two special cases. If the deleted node N is the first node in the list, then the Start will point to node B; and if the deleted node N is the last node in the list, then Node A will contain the NULL pointer. These are the common operations in the linked lists

Linked list implementation of stacks: In several applications the size of the stack may vary during the program execution, hence linked lists are used in its representation. The operation of adding an element to the front of a linked list is quite similar to that of pushing an element on to a stack. A stack can be accessed only through its top element, and a list can be accessed only from the pointer to its first element. Similarly, removing the first element from a linked list is analogous to popping from a stack.
A linked-list is somewhat of a dynamic array that grows and shrinks as values are added to it and removed from it respectively. Rather than being stored in a continuous block of memory, the values in the dynamic array are linked together with pointers. Each element of a linked list is a structure that contains a value and a link to its neighbor. The link is basically a pointer to another structure that contains a value and another pointer to another structure, and so on. Insertion: To insert an element into the stack we need to check whether stack is empty or not.

If the stack is not empty we are not supposed to insert the node.

Here there exists only one data field, there are no further links attached to node So, Head=tail=p

Here we are linking up each and every node with the next by storing the address of the next node in the address field of previous one We can say that Tail ->link=p Tail=p

Algorithm: Step1: Check whether the stack is empty or not If (head==NULL) Head=tail=p Step2: Get the address of the node we need to insert into the stack and then place it in the address field Of the previous node tail ->link.p tail=p Step3: Make the last node (i.e. the address field) to null, this indicates that there are no further nodes Linked.

Pop: To pop an element into the stack we need to check whether there exits some data in the stack or not. Algorithm: Step1: Check whether there are elements in the stack or not If (p -> link! =NULL) Step2: Consider some variable (say k) and copy the address of the variable that we need to pop out of Stack K=p

P= p -> link Step3: Once the elements are popped out we need to free that pointer, so that it moves further K -> link=NULL Tail=k Free (p) Step4: These steps (from 1 to 3) will be processed only If (head! =null) Else Head=tail=NULL Free (p) Step5: Thus, the data is popped into the stack Linked list Implementation of queues: One major drawback of representing a queue by using array is that there a fixed amount of storage is allocated, hence lists are used. Linked list implementations of queues often require two pointers or references to links at the beginning and end of the list. Operations: There are two major operations Enqueue: Inserting an element into the queue Enqueue(inserting an element into the queue) Dequeue(deleting an element from the queue) Front: which holds the address of first node Rear: which holds the address of last node

Algorithm: Step1: Check whether the queue is empty or not If(head===NULL) Head=tail=p Step2: Insert the elements into the queue by linking up the address field of the next node (the node we need to insert) to the previous node tail -> link=p tail=p Step3: Make the last node (i.e. the address field) to null; this indicates that there are no further nodes Linked.

Dequeue: Deleting an element from the queue

Algorithm: Step1: Check whether there are elements in the queue or not If(head!==NULL) Step2: Step3: Step4: Thus, the elements are deleted from the queue

Linked list implementation of priority queues: These are a type of queues where the execution of the jobs take place based on there priorities. It means an importance given to the set of jobs/activities depending on which an order will be fixed for the purpose of execution of the same. There are two different types of priority queues, there are: Ascending priority queue Descending priority queue

Ascending priority queue: Here the lower prioritized will be deleted or executed first. Example:

Values: Priority:

45 4

46 3

95 2

105 1

Based on the priorities given the values are executed (the least prioritized one is executed first).
For an ascending Priority queue, insertion is implemented by the place operation, which keeps the list ordered, and deletion of the minimum element is implemented by the delete operation, which removes the first element from the list.

Descending priority queue: Here the higher prioritized value is deleted or executed first. Example: Values: Priority: 104 1 96 2 952 3 100 4

Based on the priorities given the values are executed (the highest prioritized one is executed first)

A Descending priority queue can be implemented by keeping the list in descending order rather than ascending, or by using remove to delete the minimum element. Note: 1) In an ordered list, if you want to insert an element to the priority queue, it will require examining an average of approximately n/2 nodes but only one search for deletion. 2) An unordered list may also be used as a priority queue. If you want to insert an element to the list always requires examining only one node but always requires examining n elements for removal of an element.

3) The advantage of a list over an array for implementing a priority queue is that an element can be inserted into a list without moving any other elements, where as this is impossible for an array unless extra space is left empty.

Circular lists
Circular lists are like singly linked lists, except that the last node contains a pointer back to the first node rather than the null pointer.

From any point in such a list, it is possible to reach any other point in the list. If we begin at a given node and travel the entire list, we ultimately end up at the starting point.

Operations: Insertion Deletion

Inserting Items into a Circular List:

Here NODE C is being inserted Algorithm: Step1: InsertItem Step2: Set new Node to address of newly allocated node Step3: Set Info (newNode) to item Step4: Find the place where the new element belongs Step4: Put the new element into the list

Deleting Items from a Circular List: Algorithm: Step1: DeleteItem Step2: Find the element in the list Step3: Remove the element from the list Step3: Deallocate the node

Here the NODE B is deleted

Stacks and queues as circular lists Stacks as circular lists:

Queues as circular lists:


Since insertion, deletion takes place at different location. Implementing queue as circular list makes its easier. Normally queue functions as circular list. When the rear, front reaches the end of the list, they can be pointed to the beginning, so that the queue operation continues.

Initially, when such a queue is empty, the "front" and the "rear" values are 0 & -1 respectively; and the queue has a NULL value for all its element. Every time we add an element to the queue, the "rear" value increments by 1 till the time it reaches the upper limit of queue; after which it starts all over again from 0. Similarly, every time we delete an element from queue, the "front" value increments by 1 till the time it reaches the upper limit of queue; after which it starts all over again from 0.

You might also like