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

Data Structures and Algorithms Lab Journal - Lab 5 Doubly Linked List

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Lab Journal – Lab 5

Data Structures and Algorithms

Lab Journal - Lab 5 Doubly Linked List


Name: _________________________________

Enrollment #: _________________________________

Class/Section: _________________________________

Objective

This lab session is aimed at introducing students to doubly linked list. In addition, the students
will also implement a number of utility functions involving doubly linked lists.

Task 1 :

Give answers to the following.

1 Consider the following doubly linked list.


.

Write C++ statements to:

a. Print the value 26 using the pointer ‘last’:


cout<<last->prev->data

b. Print the value 17 using the pointer ‘first:


cout<< first->next->data

c. Print the address of the node containing value 9 using the pointer ‘last’:
Node* temp=last;
while(temp->data!=9)
temp=temp->prev;
cout<<temp;

Data Structures and Algorithms Page 1


Lab Journal – Lab 5

2
. Given the following linked list, state what does each of the following statements refer to?

first->data;

last->next;

first->next->prev;

first->next->next->data;

last->prev->data;

3
. Redraw the following list after the given instructions are executed:

cout<<ptr;// address of 17
cout<<ptr->next;// address of 20
cout<< ptr->next->prev;//address of 17
cout<<ptr->prev;//address of 15
ptr->next->prev = ptr->prev; // update prev of 20 which was
address //of 17 with address of 15
cout<<ptr->prev;//address of 15
cout<<ptr->prev->next;//address of 17

ptr->prev->next = ptr->next; // update next of 15 which was address

Data Structures and Algorithms Page 2


Lab Journal – Lab 5

//of 17 with address of 20

delete ptr;// delete node whose address is stored in ptr

Task 2 :

Implement the following exercises.

Exercise 1
Implement the priority queue using doubly linked list. Provide the standard enqueue, dequeue
and traverse operations in the class.
Exercise 2
Implement the class Doubly Linked List to create a list of integers. You need to provide the
implementation of the member functions as described in the following.
class List
{
private:
Node * head;
public:
List();
~List();
// Checks if the list is empty or not
bool IsEmptyList();

// Inserts a new node at the start of the list


void insert_begin(int value);

// Inserts a new node at the end of the list


void insert_end(int value);

// Inserts a new node with value ‘newV’ after the node


containing value ‘oldV’. If a node with value ‘oldV’ does
not exist, inserts the new node at the end.
void insertafter(int oldV, int newV); //insert middle

// Deletes the node containing the specified value


void deleteFirstNode(int value);

Data Structures and Algorithms Page 3


Lab Journal – Lab 5

// Displays the values stored in the list


void traverse();
};

InsertLast insertLast
deleteFirst deleteLast
Queue Stack
12345 54321

Exercise 3

Implement the Circular Queue using a doubly linked list. Provide EnqueueBeginning(),
EnqueueEnd(), DequeueBeginning(), DequeueEnd() and Traverse() functions in the class.

S No. Exercise Checked By:


1. Exercise 1

2. Exercise 2

3. Exercise 3

+++++++++++++++++++++++++

Data Structures and Algorithms Page 4

You might also like