0% found this document useful (0 votes)
48 views

Data Structure $ Algorithm Assignment: Group

This document contains information about a doubly linked list data structure. It lists the names and student IDs of 10 students in a computer science group assignment. It then provides details on doubly linked lists, including that each node contains two links to the previous and next node, allowing navigation in both directions. It describes the basic structure of a doubly linked list node and some examples of applications. It also outlines common operations like insertion, deletion and traversal in both directions.

Uploaded by

malik mac
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Data Structure $ Algorithm Assignment: Group

This document contains information about a doubly linked list data structure. It lists the names and student IDs of 10 students in a computer science group assignment. It then provides details on doubly linked lists, including that each node contains two links to the previous and next node, allowing navigation in both directions. It describes the basic structure of a doubly linked list node and some examples of applications. It also outlines common operations like insertion, deletion and traversal in both directions.

Uploaded by

malik mac
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

COMPUTER SCIENCE DEPP

DATA STRUCTURE $ ALGORITHM


ASSIGNMENT 2ND YEAR
GROUP
NO NAME ID
1. MUHIDIN KEMAL ------------------------- --------------- 057/11
2. HUSEIN GURE ---------------------- -------------039/11
3. MUHAMMED MEGARSA ------------------ ----------- 055/11
4. MALIK ABDURHAMA ------- ---------------------- 051/11
5. DAMA HUSEIN --------------------------------- 027/11
6. MUSTEFA JEMAL -----------------------------------
7. KELEMUW NIGUSE -------------------- ------------- 043/11
8. KALIL MUKTER ---------------------------------------
9. LOMI SINBIRU----------------------------------------------0 49/11
10. RUKIA AMAN----------------------------------------------065/11
02/12/2019
CHAPTER -3
DATA STRACTURE
3.3 DOUBLY LINkED LISTS
Doubly-linked list is a linked data structure that consists
of a set of sequentially linked records called nodes.
Each node contains two fields, called links, that are
references to the previous and to the next node in the
sequence of nodes.
Doubly Linked List is a variation of Linked list in which
navigation is possible in both ways either forward and
backward easily as compared to Single Linked List.
Doubly linked list is a collection of nodes linked
together in a sequential way. Each node of the list
contains two parts (as in singly linked list) data
part and the reference or address part.
Doubly linked list is almost similar to singly
linked list except it contains two address or
reference fields, where one of the address field
contains reference of the next node and other
contains reference of the previous node.
First and last node of a linked list contains a
terminator generally a NULL value, that determines
the start and end of the list. Doubly linked list is
sometimes also referred as bi-directional linked
list since it allows traversal of nodes in both
direction. The basic structure of a doubly linked list
is represented as:
The important points to be considered
indoubly linked list
• Doubly Linked List contains a link element called first and
last.
• Each link carries a data field(s) and two link fields called
next and prev.
• Each link is linked with its next link using its next link.
• Each link is linked with its previous link using its previous
link.
• The last link carries a link as null to mark the end of the
list.
Typical node in the doubly linked list consists of three fields:
Data represents the data value stored in the node.
Previous represents a pointer that points to the previous node.
Next represents a pointer that points to next node in the list.
Data Structure (Representation) Doubly
Linked List

head temp
600
400
400 600 800

0 1 600 400 5 800 600 7 0


Basic structure of a doubly linked list
The basic structure of a doubly linked list contains a data field and two
address fields. Here is how it can be represented in C programming
language.
struct node {
int data; // Data field
struct node * prev; // Address of previous node
struct node * next; // Address of next node
};
Applications/Uses of doubly linked list in real life
Doubly linked list can be used in navigation systems where both front
and back navigation is required.
It is used by browsers to implement backward and forward navigation
of visited web pages i.e. back and forward button.
It is also used by various application to
implement Undo and Redo functionality.
It can also be used to represent deck of cards in games.
It is also used to represent various states of a game.
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.
•Insert Last − Adds an element at the end of the list.
•Delete Last − Deletes an element from the end
of the list.
•Insert After − Adds an element after an item of the list.
•Delete − Deletes an element from the list using the key.
•Display forward − Displays the complete list in a
forward manner.
Display backward − Displays the complete list in a
backward manner
3.3.1. Creating Doubly Linked Lists
A double linked list contains an extra pointer to its previous node.
So, it contains pointer to next, previous and its value.
Steps to create Doubly linked list
1.Create a head node and assign some data to its data field.

2. Make sure that the previous and next address field of the head node must point


to NULL.
3.Make the head node as last node.

4.Link the last Node next address fieldwith new Node.


5. Move the lastNode to newNode i.e. last node will now point to new node.

6.Repeat Steps 4-8 if you want to add more nodes to the list.
The structure of creat nodes for a doubly linked list would be
defined as follows:

struct node{
charname[20];
node *nxt; // Pointer to next node
node *prv; // Pointer to previous node
};
node *current;
current = new node;
current->name = "Fred";
current->nxt = NULL;
current->prv = NULL;
We have also included some code to declare the first node and set its pointers to
NULL.
It gives the following situation:
3.3.2. Adding a Node to a Doubly Linked List
Adoubly linked list and insert a new node in beginning, end or at any position
in the list.
Steps to insert a new node in Doubly linked list
To insert a node at 3rd position.
1.Traverse to N-1 node in the list. Where N is the position
to insert. Say temp now points to N-1th node.

2. Create a new Node that is to be inserted and assign some


data to its data field.
3. Connect the next address field of new Node with
the node pointed by next address field
of temp node.

4.Connect the previous address field


of newNode with the temp node
5. Check if temp->next is not NULL then, connect the previous address
field of node pointed by temp.next to newNode.

6. Connect the next address field of temp node to newNode i.e. temp-


>next will now point to newNode.

7. Final list
The structure of insert node a Doubly linked list
void add_node_at_start (string new_name)
{ // Declare a temporary pointer and move it to the start
node *temp = current;
while (temp->prv != NULL)
temp = temp->prv;
// Declare a new node and link it in
node *temp2;
temp2 = new node;
void add_node_at_end ()
{ // Declare a temporary pointer and move it to the end
node *temp = current;
while (temp->nxt != NULL)
temp = temp->nxt;
// Declare a new node and link it in
node *temp2;
temp2 = new node;
temp2->name = new_name; // Store the new name in the node
temp2->nxt = NULL; // This is the new start of the list
temp2->prv = temp; // Links to current list
temp->nxt = temp2;
}
Here, the new name is passed to the appropriate function as a parameter. We'll go through
the function for adding a node to the right-most end of the list. The method is similar for
adding a node at the other end. Firstly, a temporary pointer is set up and is made to march
along the list until it points to last node in the list.
After that, a new node is declared, and the name is copied into it. The nxt pointer of this
new node is set to NULL to indicate that this node will be the new end of the list.
The prv pointer of the new node is linked into the last node of the existing list.
The nxt pointer of the current end of the list is set to the new node.
3.3.3. Deleting a Node From a Doubly Linked List
Delete a node from beginning, end or at any position of the linked list.
Steps to delete node from any position of a doubly linked list
 To delete node from 2nd position.
1. Traverse to Nth node of the linked list,lets say a
pointer current points to  Nth node in our case 2 node.
2.Link the node behind current node with the node
ahead of current node, which means now the N-1th node
will point to N+1th node of the list. Which can be implemented
as current->prev->next = current->next

3.If N+1th node is not NULL then link the N+1th node with N-


1th node i.e. now the previous address field of N+1th node will
point to N-1th node. Which can be implemented as current-
>next->prev = current->prev
4. Finally delete the current node from memory and
you are done

Program to delete a node from doubly linked list


//delete first item
struct node* deleteFirst() {
//save reference to first link
struct node *tempLink = head;
//if only one link
if(head->next == NULL) {
last = NULL;
}else {
head->next->prev = NULL;
}h
ead = head->next;
//return the deleted link
return tempLink;
}
Advantages of Doubly linked list
Doubly linked list is one of the important data structures. Here are
various advantages of doubly linked list.
As like singly linked list it is the easiest data structures to
implement.
Allows traversal of nodes in both direction which is not possible
in singly linked list.
Deletion of nodes is easy when compared to singly linked list, as
in singly linked list deletion requires a pointer to the node and
previous node to be deleted. Which is not in case of doubly linked
list we only need the pointer which is to be deleted.
Reversing the list is simple and straightforward.
Can allocate or de-allocate memory easily when required during
its execution.
It is one of most efficient data structure to implement when
traversing in both direction is required.
Disadvantages of Doubly linked list
Not many but doubly linked list has few
disadvantages also which can be listed below:
It uses extra memory when compared to array and
singly linked list.
Since elements in memory are stored randomly,
hence elements are accessed sequentially no direct
access is allowed.
END

You might also like