Documentos de Académico
Documentos de Profesional
Documentos de Cultura
_________________________________
Enrollment #: _________________________________
Class/Section: _________________________________
Objective
This lab session is dedicated to implementation of the selected programs/functions in the midterm exam examination. The session will serve as a recap of topics covered in the first eight
weeks of the semester.
Task 1 :
Give answers to the following.
2. What would be the co ntents of stack s after the following piece o f code is execu ted:
Stack s;
s.push(111);
s.push(222);
int i = s.top();
s.pop();
3. Assume the following doubly-linked list. E ach node in the list is represented by th e
Node class listed as fo llows.
class Node{
public:
char data;
Node * next;
Node * prev;
};
Data Structures and Algorithms
Page 1
p1
Using o nly the pointer p1 to access the list, write sta tements to:
a. Display the contents of the four nodes in the list. (Hint: Write four
statements).
b. Replace W by M and D by T .
Page 2
Task 2 :
Implement the following exercises.
Exercise 1
Implement the functio n int CountOdd(N ode *head) which returns the total number of
nodes containing odd values in a singly linked list of integers. T he argument to the
funct ion represents a pointer to the first no de of the list. The no de of the linked list
is implemente d by t he following class.
class Node{
public:
int data;
Node *next;
};
Exercise 2
A polynomial can be represe nted as a link ed list. For every term in the polynomial
there is one ent ry in t he linked list consisting of the term's coefficient and the degree
as illust rated in t he fo llowing.
Page 3
6 * 2 2 + 10 * 2 1
Exercise 3
Complete the function void m ove(Node * ptr , int n) which accepts a pointer to a node
of a doubly linke d list and moves the no de n positions forw ard. For simplicity,
assume that the values of pt r and n are such that the first and last nodes do not
come into play . An ex ample illustrat ion showing t he original list and t he list after a
sample functio n call is presente d in t he follo wing. Each node of the list has an integer
(data) and two pointe rs (next and prev).
ptr
Original List
10
12
10
12
Page 4
Implement the given exercises and get them checked by your instructor. If you are unable to
complete the tasks in the lab session, deposit this journal alongwith your programs (printed or
handwritten) before the start of the next lab session.
S No.
Exercise
1.
Exercise 1
2.
Exercise 2
3.
Exercise 3
Checked By:
+++++++++++++++++++++++++
Page 5