Está en la página 1de 5

Lab Journal Lab 9

Data Structures and Algorithms


Lab Journal - Lab 9
Name:

_________________________________

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.

1. The valu e of 10 2 8 * + 3 - is: _______

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

Lab Journal Lab 9

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 .

c. Insert a node conta ining value S a t the end of the list.

Data Structures and Algorithms

Page 2

Lab Journal Lab 9

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.

The following class can be employed to model a node of t he Polynomial list .


class PolyNode{
public:
int coeff;
int exponent;
PolyNode * next;
};

Data Structures and Algorithms

Page 3

Lab Journal Lab 9


Complete the functio n int Evaluate(PolyNode *head, int val) which accepts a pointer to the
first no de of a polynomial list and an inte ger value. The function should evaluate the
polynomial at t he giv en value. As an example, t he a bove poly nomial evaluate d at
value 2 should return 4 * 2 3

6 * 2 2 + 10 * 2 1

6 * 2 0 . For simplicity, assume t hat a

funct ion power(i nt x, int n) is available to compute x n .

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

New List after function call: move(ptr,2);

Data Structures and Algorithms

Page 4

Lab Journal Lab 9

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:

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

Data Structures and Algorithms

Page 5

También podría gustarte