Docsity

Data Structure Exam with solution, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
1
Answer the following Questions:
1. Write a program by pseudocode to perform the following:
a) Create an ordered single linked list where its head node structure contains two fields:
i) Counter field of type integer
ii) List pointer.
b) The data node structure contains three fields:
i) Value field of type integer
ii) Next pointer to successor of the data node.
c) Add the following data nodes of values: 6, 2, 4, 8 respectively.
d) Delete all data nodes by the following order: 6, 2, 4, 8 respectively.
Note: trace each step.
4
pf3
pf4
pf5

Partial preview of the text

Download Data Structure Exam with solution and more Exams Data Structures and Algorithms in PDF only on Docsity!

Answer the following Questions:

1. Write a program by pseudocode to perform the following:

a) Create an ordered single linked list where its head node structure contains two fields:

i) Counter field of type integer

ii) List pointer.

b) The data node structure contains three fields:

i) Value field of type integer

ii) Next pointer to successor of the data node.

c) Add the following data nodes of values: 6, 2, 4, 8 respectively.

d) Delete all data nodes by the following order: 6, 2, 4, 8 respectively.

Note: trace each step.

2. Write a program by pseudocode to that implements the following:

a) Create the stack, add item to the stack, and delete item from the stack.

struct stack { node first; int count; node last; } ; node createNode(double val) { node node1 = new node(); node1->prev=NULL; node1->data=val; node1->next=NULL; return node1; } stack* push(stack* MyStack,node* newnode) { if (MyStack->count==0) { MyStack->first=newnode; MyStack->last=newnode; MyStack->count=1; } else { MyStack->last->next=newnode; newnode->prev=MyStack->last; MyStack->last=MyStack->last->next; MyStack->count=MyStack->count +1; } return MyStack; } double pop(stack* Mystack) { double val; node* ptr; if (Mystack->count<=1) { ptr=Mystack->last; val=Mystack->last->data; Mystack->last=NULL; delete(ptr); Mystack->count=Mystack->count -1; } else { ptr=Mystack->last; val=Mystack->last->data; Mystack->last=Mystack->last->prev; Mystack->last->next=NULL; delete(ptr); Mystack->count=Mystack->count -1; } return val; }

cout << "Insert item at last \n"; } return Myquee; } quee* DeQuee(quee* Myquee) { node* ptr; if (Myquee->count==0) { cout << " \n"; cout << " Empty quee \n"; cout << " \n"; } else if (Myquee->count==1) { cout << " \n"; cout << "The returned value is "<<Myquee->first->data; cout << " \n"; ptr=Myquee->first; Myquee->first=NULL; delete(ptr); Myquee->count=Myquee->count -1; } else { cout << " \n"; cout << "The returned value is "<<Myquee->first->data; cout << " \n"; ptr=Myquee->first; Myquee->first=Myquee->first->prev; Myquee->first->next=NULL; delete(ptr); Myquee->count=Myquee->count -1; } return Myquee; }

4. In the binary search tree implement the following

a) Create the logical view of a binary search tree using the following data entered as sequential set:

460, 170, 250, 630, 550, 970, 140, 1070, 950, 500, 570, 200, 300, 130 and 160 ,

b) Delete the node of value 1 70.

c) Traverse the tree in the threes type of traverse (inorder, preorder, postorder).

a)

b) Delete the node of value 1 70.

c) Inorder:130,140,160,170,200,250,300,460,500,550,570,630,950,970,

Preorder:460,170,140,130,160,250,200,300,630,550,500,570,970,950,

Postorder:130,160,140,200,300,250,170,500.570,550,950,1070,970,630,

d) Write down a program by pseudocode and use the suitable data structure to implement

the following for the set operations:

Union operation for two sets. Ex: [a,b,c,d]U[x,a,y,d]=[a,b,c,d,x,y].

Intersection operation for two sets. Ex: [a,b,c,d]∩[x,a,y,d]=[a,d].

Difference operation for two sets. Ex: [a,b,c,d]/[x,a,y,d]=[b,c].

Note: trace each step

template<class InputIt1, class InputIt2, class OutputIt, class Compare> OutputIt set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first, Compare comp) { for (; first1 != last1; ++d_first) { if (first2 == last2) return std::copy(first1, last1, d_first); if (comp(*first2, *first1)) { *d_first = *first2++; } else { *d_first = first1; if (!comp(first1, *first2)) ++first2; ++first1; } } return std::copy(first2, last2, d_first); }