Online Assignment 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Online Assignment 2

Duration: 1h

You have to use C language to write your code

Question 1 (2 points). Given a double linked list containing integer numbers

DL_head

2 3 11 7

NULL
NULL

Where DL_head is a pointer pointing to the first node of the double linked list. A structure of nodes of
double linked list as following:

struct doubleNode{

int data;

struct doubleNode * next;

struct doubleNode* previous;

};

typedef struct doubleNode * PtrToDoubleNode;

Please write a function to insert a node with data X at the end of double linked list. The format of this
function is:

void Insert_End(PtrToDoubleNode DL_head, int X)

Question 2 (3 points) Given the infix expression: 5*(6+2)-12:4

a. Provide the postfix expression of the above expression (1 point).


b. Provide the contain of a stack step by step when we scan the postfix expression for evaluation
using the following format (2 points).

For example

Step 1 Scan postfix expression Content of stack


1 Read 5 5
2 … …
Question 3 (1 point)

What is the content of the queue after the following sequence of pushes (or enqueue) and pops (or
dequeue):

Queue q; push( q,3 ); push( q,5 ); push( q,2 ); push( q,15 ); push( q,42 ); pop(q); pop(q); push( q,14 );
push( q,7 ); pop(); push( q,9 ); pop(q); pop(q); push( q,51 ); pop(q); pop(q);

Question 4 (2 points)

Given a queue of integers with only 4 basic operations:

-int dequeue(Queue Q): delete the integer at head of the queue

-void enqueue(Queue Q, int X): add an integer X at tail of the queue

-int top_Queue (Queue Q): return an integer at head of the queue without deletion.

-int isEmpty_Q(Queue Q): check the queue is empty or not

Given a stack of integers with only 4 basic operations:

-void push (Stack S, int X): insert an integer at head of the stack

-void pop (Stack S): Delete an integer at head of the stack

-void top_Stack (Stack S): return an integer at head of the stack without deletion

-int isEmpty_S(Stack S): check the stack is empty or not

By only using queue and stack with their 8 basic functions, please write a C program to combine two
sorted queues Q1 and Q2 into one sorted queue Q3. Assuming that Q1 is already in increasing order, Q2
is already in decreasing order, and output Q3 should be in increasing order.

You don’t need to write codes for 8 basic functions. Assuming that they are already implemented.

For example: Q1 (Head to Tail): 1, 5, 9, 40; Q2 (Head to Tail): 30, 25, 7, 5, 4

Output Q3 (Head to Tail): 1, 4 , 5, 5, 7, 9, 25, 30, 40

Question 5 (2 points) A word is inversible if we reverse the letters of the word and we receive a same
word. For example, “hannah” is inversible because if we reverse this word, we also receive the
“hannah”. Given a word stored in a single linked list. Write an efficient algorithm in C language to check
if a word is inversible or not. Array is not allowed in the algorithm.

You might also like