Вы находитесь на странице: 1из 10

Lab Manual for Data Structures

Lab 8
Doubly Linked List and DECK Implementations

Department of Computer Science Page 93


MAJU, Islamabad
_________________ Lab 8: Doubly Linked List and DECK Implementations

Table of Contents
1. Introduction 95
1.1 Doubly Linked Lists 95
1.2 Relevant Lecture Readings 95

2. Activity Time boxing 95

3. Objectives of the experiment 95

4. Concept Map 96
4.1 Doubly Linked Lists in C++ 96
4.2 Double ended queue (DECK): 98

5. Homework before Lab 99


5.1 Problem Solution Modeling 99
5.2 Problem description: 99
5.3 Practices from home 99
5.3.1 Task-1 99
5.3.2 Task-2 99

6. Procedure & Tools 99


6.1 Tools 99
6.2 Walk through Tasks [Expected time = 20 mins] 99

7. Practice Tasks 101


7.1 Practice Task 1 [Expected time = 30 mins] 101
7.2 Practice Task 2 [Expected time = 20 mins] 101
7.3 Out comes 101

8. Midterm [Expected time = 60 mins for two tasks] 102

9. Evaluation criteria 102

10. Further Readings 102


10.1 Web sites related to C++ tutorials about linked lists 102
10.2 Web sites containing supporting material 102

Department of Computer Science Page 94


MAJU, Islamabad
_________________ Lab 8: Doubly Linked List and DECK Implementations

Lab 8: Doubly Linked List and DECK Implementations.

1. Introduction

This lab will introduce concept of dynamic memory based data structures such as linked
lists. In case of some real world applications we don’t know about the storage requirements of
program before program execution, in that scenario dynamic data storage systems are useful. We
will learn about linear, circular and doubly linked list structures and how linked lists are helpful
to store data during program execution.

1.1 Doubly Linked Lists

A doubly linked list is a linked list in which every node has a next pointer and a back
pointer. In other words, every node contains the address of the next node (except the last node),
and every node contains the address of the previous node (except the first node). See Figure 4.

Figure 4: Doubly Linked List.

1.2 Relevant Lecture Readings

a) Revise Lecture No. 21 and 22 available at \\dataserver\jinnah$\ in instructor’s


folder.
b) From books: C++ Data Structures by Nell Dale (Page 334 - 358) and Data
structures using C++ by D. S. Malik (Page 266 – 280, 326 – 338, 310 - 320).

2. Activity Time boxing

Table 1: Activity Time Boxing


Task No. Activity Name Activity time Total Time
5.1 Design Evaluation 20mins 20mins
6.2 Walk through tasks 20mins 20mins
7 Practice tasks 20 mins for task 1 and 2, 30 70mins
mins for task 3
9 Evaluation Task 60mins for all assigned tasks 60mins

3. Objectives of the experiment

 To understand and implement linear linked lists with their operations in C++.
 To understand and implement circular linked lists with their operations in C++.
Department of Computer Science Page 95
MAJU, Islamabad
_________________ Lab 8: Doubly Linked List and DECK Implementations

 To understand and implement doubly linked lists with their operations in C++.

4. Concept Map
This concept map will help students to understand the main concepts of topic covered in
lab.

4.1 Doubly Linked Lists in C++

Insertion of a node in a doubly linked list involves some cases which are given below:

Case 1: Insertion in an empty list


Case 2: Insertion at the beginning of a nonempty list
Case 3: Insertion at the end of a nonempty list
Case 4: Insertion somewhere in a nonempty list

Following code segment can be used to insert a node in a doubly linked list:
template <class Type>
void doublyLinkedList<Type>::insert(const Type& insertItem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
nodeType<Type> *newNode; //pointer to create a node
bool found;
newNode = new nodeType<Type>; //create the node
newNode->info = insertItem; //store the new item in the
node
newNode->next = NULL;
newNode->back = NULL;
if (first == NULL) //if list is empty, newNode is the only
node
{
first = newNode;
last = newNode;
count++;
}
else
{
found = false;
current = first;
while (current != NULL && !found) //search the list
if (current->info >= insertItem)
found = true;
else
{
trailCurrent = current;
current = current->next;
}
Department of Computer Science Page 96
MAJU, Islamabad
_________________ Lab 8: Doubly Linked List and DECK Implementations

if (current == first) //insert newNode before first


{
first->back = newNode;
newNode->next = first;
first = newNode;
count++;
}
else
{
//insert newNode between trailCurrent and current
if (current != NULL)
{
trailCurrent->next = newNode;
newNode->back = trailCurrent;
newNode->next = current;
current->back = newNode;
}
else
{
trailCurrent->next = newNode;
newNode->back = trailCurrent;
last = newNode;
}
count++;
}}}//end insert
To delete a node from a doubly linked list, following cases should be considered:

Case 1: The list is empty.


Case 2: The item to be deleted is in the first node of the list, which would require us to
change the value of the pointer first.
Case 3: The item to be deleted is somewhere in the list.
Case 4: The item to be deleted is not in the list.

template <class Type>


void doublyLinkedList<Type>::deleteNode(const Type&
deleteItem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
bool found;
if (first == NULL)
cout << "Cannot delete from an empty list." << endl;
else if (first->info == deleteItem) //node to be deleted is
//the first node
{
current = first;
first = first->next;
Department of Computer Science Page 97
MAJU, Islamabad
_________________ Lab 8: Doubly Linked List and DECK Implementations

if (first != NULL)
first->back = NULL;
else
last = NULL;
count--;
delete current;
}
else
{
found = false;
current = first;
while (current != NULL && !found) //search the list
if (current->info >= deleteItem)
found = true;
else
current = current->next;
if (current == NULL)
cout << "The item to be deleted is not in "
<< "the list." << endl;
else if (current->info == deleteItem) //check for equality
{
trailCurrent = current->back;
trailCurrent->next = current->next;
if (current->next != NULL)
current->next->back = trailCurrent;
if (current == last)
last = trailCurrent;
count--;
delete current;
}
else cout << "The item to be deleted is not in list."
<<endl;}

4.2 Double ended queue (DECK):


A double ended queue is an abstract data structure. It can be implemented either using a linked
list or an array. Its implementation depends upon the programmer. Think of it as a special type of
queue which can accept elements both at the head and the tail.

On the other hand, a doubly linked list is a concrete data structure, i.e. its implementation would
be same for every programmer. It is a way for storing data.

Following cases should be considered while implementing DECK:


Case 1: Insert at the start of the queue
Case 2: Delete from the start of queue
Case 3: Insert at the end of the queue
Case 4: Delete from the end of queue
Department of Computer Science Page 98
MAJU, Islamabad
_________________ Lab 8: Doubly Linked List and DECK Implementations

5. Homework before Lab


This homework will help students to study the concepts of topic before start of lab.

5.1 Problem Solution Modeling

After studying the introduction and concept map sections you should be ready to provide
the solution of following problems. Design the solutions of the problems in C++ and
bring your code to lab so that lab instructor should assess and grade it.

5.2 Problem description:

Write a program of doubly linked list of objects of “person” class. Attributes of “person”
class (privately defined) are per_id (int), per_name (string) and per_age (int). “person”
class contains member functions (publicly defined): constructor, input and output
functions. You are required to define a class for doubly linked list. This class should
contain the member functions to insert, delete nodes from linked list. This class should
also contain the member functions to display values of all nodes of linked list.

5.3 Practices from home

5.3.1 Task-1
Compare linear linked list with doubly linked list and provide three differences between
the two structures.

5.3.2 Task-2
List three real world examples in which doubly linked list structures can be used.

6. Procedure & Tools


This section provides information about tools and programming procedures used for the
lab.

6.1 Tools

Microsoft Visual Studio 2008 with Visual C++ compiler configured.

6.2 Walk through Tasks [Expected time = 20 mins]

Following screens in figure 5 to7 represent source code implementation of a doubly


linked list class in Microsoft Visual Studio 2008. You are required to type, debug and execute
this program in Microsoft Visual Studio 2008.

This linked list program provides different functions like addition of node at start of list,
deletion of node from list, displaying elements of list.

Department of Computer Science Page 99


MAJU, Islamabad
_________________ Lab 8: Doubly Linked List and DECK Implementations

Figure 5: Implementation of doubly linked list

Figure 6: Implementation of doubly linked list

Department of Computer Science Page 100


MAJU, Islamabad
_________________ Lab 8: Doubly Linked List and DECK Implementations

Figure 7: Implementation of doubly linked list

7. Practice Tasks
This section will provide information about the practice tasks which are required to be performed
in lab session. Design solutions of problems discussed in each task and place solution code in a
folder specified by your lab instructor.

7.1 Practice Task 1 [Expected time = 30 mins]

Write a program which should implement a doubly linked list. Elements of this doubly linked list
should be of float type, user will provide values as input for elements of this doubly linked list.
Your program should Remove Duplicate nodes in DLL.

7.2 Practice Task 2 [Expected time = 20 mins]


Implement all the four cases of DECK mentioned in section 4.2.

7.3 Out comes

After completing this lab, student will be able to understand and develop programs related to
linear linked lists, circular linked lists and doubly linked lists in C++ using Microsoft Visual
Studio 2008 environment.

Department of Computer Science Page 101


MAJU, Islamabad
_________________ Lab 8: Doubly Linked List and DECK Implementations

8. Midterm [Expected time = 60 mins for two tasks]

The lab instructor will give you midterm exam.

9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).

Table 2: Evaluation of the Lab


Sr. No. Task No Description Marks
1 4 Problem Modeling 20
2 6 Procedures and Tools 10
3 7,8 Practice tasks 35
4 8.1 Evaluation Tasks (Unseen) 20
5 Comments 5
6 Good Programming Practices 10

10. Further Readings


10.1 Web sites related to C++ tutorials about linked lists
1. http://www.cprogramming.com/tutorial/lesson15.html
2. http://www.element14.com/community/community/code_exchange/blog/2013/03/26/c-
tutorial--linked-list

10.2 Web sites containing supporting material


1. http://www.learnerstv.com/Free-Computer-Science-Video-lectures-ltv733-Page1.htm
2. http://hughesbennett.co.uk/public/cpp/umbc/index.html
3. http://www.cphstl.dk/Report/Double-ended-priority-queues/paper.pdf

Department of Computer Science Page 102


MAJU, Islamabad

Вам также может понравиться