Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Linked List

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

LINKED LIST DATA STRUCTURE

Implementation of Queue using Array


Algorithm for insertion
Step 1: IF REAR = MAX - 1
Write OVERFLOW
Go to step
[END OF IF]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: Set QUEUE[REAR] = NUM
Step 4: EXIT
Implementation of Queue using Array
void insert (int queue[], int max, int front, int rear, int item)
{
if (rear == max-1)
{
printf("overflow");
}
else
{
if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
Implementation of Queue using Array
else
{
rear = rear + 1;
}
queue[rear]=item;
}
}
Implementation of Queue using Array
Algorithm for Deletion
Step 1: IF FRONT = -1 or FRONT > REAR
Write UNDERFLOW
ELSE
SET VAL = QUEUE[FRONT]
SET FRONT = FRONT + 1
[END OF IF]
Step 2: EXIT
Implementation of Queue using Array
int delete (int queue[], int max, int front, int rear)
{
int y;
if (front == -1 || front > rear)
{
printf("underflow");
}
else
{
y = queue[front];
if(front == rear)
{
front = rear = -1;
else
front = front + 1;
}
return y;
}
}
Implementation of Queue using Array
void display()
{
int i;
if(rear == -1)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
for(i=front;i<=rear;i++)
{
printf("\n%d\n",queue[i]);
}
}
}
Linked List
• A linked list is a sequence of data structures, which are connected together via links.
• Linked List is a sequence of links which contains items.
• Each link contains a connection to another link.
• Linked list is the second most-used data structure after array.

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

• Link − Each link of a linked list can store a data called an element.
• Next − Each link of a linked list contains a link to the next link called Next.
• LinkedList − A Linked List contains the connection link to the first link called First.
Linked List Representation

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

As per the above illustration, following are the important points to be considered
• Linked List contains a link element called first.
• Each link carries a data field(s) and a link field called next.
• Each link is linked with its next link using its next link.
• Last link carries a link as null to mark the end of the list.
Basic Operations
Following are the basic operations supported by a list –

• Insertion − Adds an element at the beginning of the list.


• Deletion − Deletes an element at the beginning of the list.
• Display − Displays the complete list.
• Search − Searches an element using the given key.
• Delete − Deletes an element using the given key.
Singly Linked List
• A singly linked list is a type of linked list that is unidirectional, that is, it can be traversed in only
one direction from head to the last node (tail).
• Each element in a linked list is called a node.
• A single node contains data and a pointer to the next node which helps in maintaining the structure
of the list.
• The first node is called the head; it points to the first node of the list and helps us access
every other element in the list. The last node, also sometimes called the tail, points
to NULL which helps us in determining when the list ends.
Common Singly Linked List Operations:

Search - You can determine and retrieve a specific node either from
the front, the end, or anywhere in the list.
The worst case Time Complexity for retrieving a node from
anywhere in the list is O(n).

Add - You can add a node at the front, the end or anywhere in the
linked list.
The worst case Time Complexity for performing these operations is as
follows:
•Add item to the front of the list: O(1)
•Add item to the end of the list: O(n)
•Add item to anywhere in the list: O(n)x
Common Singly Linked List Operations:
You can remove a node either from the front, the end or from anywhere in the list.

The worst case Time Complexity for performing this operation is as follows:
• Remove item from the front of the list: O(1)
• Remove item from the end of the list: O(n)
• Remove item from anywhere in the list: O(n)
Circular Linked List
• A circular linked list is a variation of a normal linked list.
• In a circular linked list, as the name suggests, the list does not end; instead, it loops around. The last
element of a circular linked list points to the head instead of pointing to null.
• A circular linked list can be implemented as a singly linked list or a doubly linked list.
• There are three fundamental operations that every linked list does:
1. Add element
2. Delete element
3. Display list
Add element –

There are two things to be careful of when writing


the addElement() function for a circular linked list:

1.When adding an element to an empty list,


make head of the linked list equal
to newNode. next should point to itself.

2.When adding an element to a linked list with one or


more nodes, you need to find the last element before
it loops to head again. Then, make the next pointer of
the last node point to newNode and the next pointer
of newNode point to head.

Delete element –

When deleting an element, you need to be mindful of


two things:

1.When you are deleting an element from a circular


linked list of size one, you need to point
the head pointer to Null.

2.When you are deleting an element from a list with


two or more elements, you need to keep track of two
pointers: current and previous. As soon as the current
reaches the target node, make the previous
node’s next point to the current nodes next. Then
delete the current node.
Display element –

Displaying the data of a circular linked list is similar


to a normal linked list – you visit each node and print
the data.

The only difference between the two lists is in the


method of termination. When printing data of a
normal linked list, the code should start displaying
data from the head pointer and terminate as soon
as null is hit. Whereas, in a circular linked list, the
code should start displaying data from
the head pointer and terminate as soon as head is
reached again.
Doubly Linked List
• Doubly linked list is a type of linked list in which each node apart from storing its data has two links.

• The first link points to the previous node in the list and the second link points to the next node in the list.

• The first node of the list has its previous link pointing to NULL similarly the last node of the list has its

next node pointing to NULL.

The two links help us to traverse the list in both backward and forward direction. But storing an extra link
requires some extra space.
Quiz

A linear collection of data elements where the linear node is given by means of
pointer is called?

A. Linked List
B. Node List
C. Primitive List
D. None of these
Quiz

Ans : A
Explanation: A linear collection of data elements where the
linear node is given by means of pointer is called linked list.
Quiz

What is the time complexity to count the number of elements in the linked list?

A. O(1)
B. O(n)
C. O(logn)
D. None of the mentioned
Quiz

Ans : B
Explanation: To count the number of elements, you have to traverse through
the entire list, hence complexity is O(n).
Quiz

Linked lists are not suitable to for the implementation of?

A. Insertion sort
B. Radix sort
C. Polynomial manipulation
D. Binary search
Quiz

Ans : D
Explanation: Linked lists are not suitable to for the
implementation of Binary search.
Quiz

Which of these is an application of linked lists?

A. To implement file systems


B. For separate chaining in hash-tables
C. To implement non-binary trees
D. All of the mentioned
Quiz

Ans : D
Explanation: Linked lists can be used to implement all of the above mentioned
applications.
THANK YOU!

You might also like