You are on page 1of 8

C Programming and Data Structures

(R20 Syllabus JNTUA,Anantapuramu)


UNIT-IV
Syllabus
Linked Lists – Singly linked list, dynamically linked stacks and queues, polynomials using singly linked lists, using
circularly linked lists, insertion, deletion and searching operations, doubly linked lists and its operations, circular
linked lists and its operations.

Linked List:A Linked List is a linear data structure that allocates non-sequential memory locations in main
memory of computer. These locations can’t easy to identify the particular element into the list. It is a dynamic
data structure because we can insert or delete the elements at runtime (size is not fixed). To locate the elements
from a linked list, we refer address of each memory location. Linked List are mainly divided into three types
like:
Singly Linked List, 2. Doubly Linked List, 3. Circular Linked List

Singly Linked List : A Singly linked list is a linear data structure. In this element refers “node”, it contains
two fields such as data & link.
Data : It contains actual value data link
Link : It contains next node address address
The representation of Singly Linked List in memory: Every node is basically a chunk of memory that carries an
address. A set of data elements are represented using a linked list, each data element is represented by a node.
head
10 102 20 103 30 NULL

101 102 103

In the above diagram we use a special pointer is “head”, it contains always first node address and also
last node link contains “NULL” pointer. Singly Linked List provides mainly 4 types of operations like 1.
Creation 2. Traversing 3. Insertion 4. Deletion
Dynamically Linked stacks and Queues(stacks and queues using Linked list Programs)
Linked Stack:
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf("\nInsertion is Success!!!\n");
}
void pop()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}
void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}

Linked Queue

void insert(int value)


{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode -> next = NULL;
if(front == NULL)
front = rear = newNode;
else{
rear -> next = newNode;
rear = newNode;
}
printf("\nInsertion is Success!!!\n");
}
void delete()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
struct Node *temp = front;
front = front -> next;
printf("\nDeleted element: %d\n", temp->data);
free(temp);
}
}

void display()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
struct Node *temp = front;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL\n",temp->data);
}
}
Polynomial using single linked list Representation
Let us tackle a reasonably complex problem using linked list. This problem, the
manipulation of symbolic polynomials has become example of list processing.
a = 3x14+2x8+1 and b = 8x14-3x10+10x6

a 3 14 2 8 1 0 0

b 8 14 -3 10 10 6 0

Fig: Representation of a = 3x14+2x8+1 and b = 8x14-3x10+10x6


Polynomial using circular linked list representation
We can free all the nodes of a polynomial more efficiently if we modify our list
structure so that the link field of the last node points to the first node in the list.We call this a circular list.
A single linked list in which the last node has a null link is called a chain.

3 14 2 8 1 0

Fig: circular representation of 3x14+2x8+1

Doubly Linked List


Doubly linked list is a collection of linear data items. It is dynamic data structure because elements are inserted
and deleted at runtime. In doubly linked list element refers node. Node contains three fields like left link, data
and right link.
1. Left link : It contains previous node address.
2. Data : It contains actual value.
3. Right link : It contains next node address.

The representation of doubly linked list is:

head tail
NULL NULL
10 102 101 20 103 102 30
(101)
101 102 103

In the above representation we use two special pointers like head & tail. Head is always pointing first node
address and tail is always pointing last node address.
Difference between Singly linked list & Doubly linked list:
In singly linked list node contains two fields but in doubly linked list node contains three fields. In
singly linked list traverse can possible only one direction because node contains next node address but in
doubly linked list traverse can possible both direction because node contains previous node and next node
address. The structure of node is

Ex: struct node


{
int data;
node *llink, *rlink;
};

Doubly linked list provides mainly four types of operations like:


1. Creation 2. Traverse 3. Insertion 4. Deletion

Creation: In this we first check the condition head is NULL or not. If it is NULL then create a new node
and assign that address to the head & tail. Other wise create a new node and assign that address to the tail.
Algorithm:
Algorithm create ( num )
{ temp = (struct node *) malloc (sizeof(struct node ));

if ( head == NULL )
{
temp->data =num;
temp->lnext = NULL;
temp->rnext = NULL;
head = tail = temp;
}
else
{
temp = (struct node *) malloc (sizeof(struct node ));
temp->lnext = tail;
temp->data = num;
temp->rnext = NULL;
tail->rnext = temp;
tail = temp;
}
}
Traverse: Traverse means to display the list of elements. In this traverse can possible both direction, from
first node to last node called “forward traverse” and from last node to first node called “backward traverse”.
Forward Traverse Backward Traverse

Algorithm ftraverse( ) Algorithm btraverse( )


{ {
temp = head; temp = tail;
while( temp!=NULL ) while( temp!=NULL )
{ {
printf(“%d”,temp->data; printf(“%d”,temp->data;
temp = temp->rnext; temp = temp->lnext;
} }
} }
Insertion:Suppose if we want to insert an element into the list by using three methods like
To insert an element at beginning of the list
To insert an element at end of the list
To insert an element after the specified node
To insert and element at beginning of the list: In this we first create a new node and assign that address to the
head pointer because it is a first node.
Algorithm: insert-begin( num )
{
temp = (struct node *) malloc (sizeof(struct node ));
temp->lnext = NULL;
temp->data = num;
temp->rnext = head;
head->lnext = temp;
head = temp;
}
To insert an element at end of the list: In this we first create a new node and assign that address to the tail
pointer because it is a last node.
Algorithm: insert-end( num )
{
temp = (struct node *) malloc (sizeof(struct node ));
temp->lnext = tail;
temp->data = num;
temp->rnext = NULL;
tail->lnext = temp;
tail = temp;
}
To insert an element after the specified node:
In this we first check the condition specified node is there or not. If it is found then create a new node
and assign that address to the previous and next node otherwise display element not found.
Algorithm: insert-after( num, ele )
{
a = head;
while( a!=NULL )
{
if( a->data == ele )
{
b = a->rnext;
temp = (struct node *) malloc (sizeof(struct node ));
temp->lnext = a;
temp->data = num;
temp->rnext = b;
a->rnext = b->lnext = temp;
}
}
}
Deletion:Suppose if we want to delete an element from the list, first to check the condition deleted element is
there or not. If it is found then delete that node otherwise display element not found.
Algorithm: delete( num )
{
a = head;
if( head->data == num )
{
head = head->rnext;
head->lnext = NULL;
delete a;
}
else
{
while( a!=NULL )
{
if( a->data == num )
{
c = a->lnext;
b = a->rnext;
c->rnext = b;
b->lnext = c;
delete a;
}
}
}
}

Advantages and disadvantages of a doubly linked list:


Advantages:
The availability of two links Llink and Rlink permit forward and backward movement during the processing of
the list.
The deletion of node X from the list calls only for the value of X to be known.
Disadvantages:
1.The disadvantage of doubly linked list is its memory require two links.
circular linked list and its operations
ircular Linked List is a variation of Linked list in which the first element points to the last element and the last element points
to the first element. Both Singly Linked List and Doubly Linked List can be made into a circular linked list.

Singly Linked List as Circular


In singly linked list, the next pointer of the last node points to the first node.

Doubly Linked List as Circular


In doubly linked list, the next pointer of the last node points to the first node and the previous pointer of the first node points to
the last node making the circular in both directions.

As per the above illustration, following are the important points to be considered.

 The last link's next points to the first link of the list in both cases of singly as well as doubly linked list.
 The first link's previous points to the last of the list in case of doubly linked list.

Basic Operations
Following are the important operations supported by a circular list.
 insert − Inserts an element at the start of the list.

 delete − Deletes an element from the start of the list.


 display − Displays the list.
Insertion Operation
Following code demonstrates the insertion operation in a circular linked list based on single linked list.

Example
insertFirst(data):
Begin
create a new node
node -> data := data
if the list is empty, then
head := node
next of node = head
else
temp := head
while next of temp is not head, do
temp := next of temp
done
next of node := head
next of temp := node
head := node
end if
End

Deletion Operation
Following code demonstrates the deletion operation in a circular linked list based on single linked list.
deleteFirst():
Begin
if head is null, then
it is Underflow and return
else if next of head = head, then
head := null
deallocate head
else
ptr := head
while next of ptr is not head, do
ptr := next of ptr
next of ptr = next of head
deallocate head
head := next of ptr
end if
End

You might also like