You are on page 1of 13

3. Figure below shows a linked list that contains 3 nodes.

a. Write source code that will delete the node pointed by current (delete the node with value A)

b. Write source code that will delete the node with value B. Redraw the figure to show the new position
of pervious and current.
c. Write source code that will delete the node with value B. Redraw the figure to show the new position
of previous and current.

Explain the purpose of having previous and current for the following operation:

i. Delete Operation:

The purposes of having pervious and current node is to use looping to locate the pointer of the date we
need to delete. Next, when we delete a data, we will have a previous node and current node there will
be no connection between the previous and current node. Therefore, this will not call as a linked list
since there are not linked”. So, we will make a new connection to connect these nodes by using the
previous and current.

ii. Insert Operation:

First, we need current because we wish to insert the data ascending in the list. Then, when we insert the
data, there are no connection to the list. Therefore, we will use allocate the previous data to point to
the new data, and pointer of the new data (current) will point to the next data.
5. Based on the following source codes, show the movement of pointer variable named, kursor in the
linked list in figure list in figure below. Write the output produced by the codes.

a. Write source code to count the number of nodes in the linked list.

b. Write source code to print the values of all nodes in the linked list.

c. Write program codes that will find the largest alphabet in the list and print the position of the
node which contained the alphabet.
7. Program 7.8 is the sources codes for function named find_pos(). This function will find the location of
a node in a linear linked list which is sorted in ascending order. In this function, the integer variable
named item is the key to be searched in the list.

a. If the statement find_pos(35) is executed and implemented on a linear linked list as shown in
Figure 7.16, show the position of the pointer variable previousNode and currentNode in the
figure.
b. Based on the position of the pointer variables header, previousNode and currentNode drawn in
part (a), write a program code that is able to insert the value 35 in the link list. In the program,
you have to use the variable NewNode which is declared and created in the statement below:

C. If the statement find_pos(63) is executed and implemented on a linear linked list as shown in Figure
10, show the position of the pointer variable previousNode and currentNode in the figure.
D. Based on the position of the pointer variables previousNode and currentNode drawn in part (c), write
a program code that is able to insert the value 63 in the linked list. In the program, you have to use the
variable NewNode which is declared and created in the statement below:

E. If the statement find_pos(70) is executed and implemented on a linear link list as shown in Figure
below, show the position of the pointer variable previousNode and currentNode in the figure.
F. Based on the position of the pointer variables previousNode and currentNode drawn in part (e), write
a program code that is able to delete 70 from the linked list.
C. Draw a figure to illustrate the situation when the stack is implemented using linked-list.
Exercise 3: Linked list implementation of stack.

1. What happen when the following operations are performed to the stack? Fill the given table
below to illustrate the situation each time the operation are performed:

a. Stack operation
I. push x
II. push y
III. pop
IV. push z
V. pop
VI. pop
VII. push x

Push X Push Y Pop Push z Pop Pop Push x

y z
x x x x x x

b. Stack operation
I. push x
II. push y
III. pop
IV. push z
V. pop
VI. push x
VII. pop
VIII. pop
IX. pop

Push X Push Y Pop Push z Pop Push X Pop Pop Pop

y z x
x x x x x x x invalid
Exercise4: Stack Application – Balanced Parenthesis

1. One of the stack implementation is to check whether an arithmetic expression has balance
parenthesis. If expression (C*(d-e))/g)) is used as input to the algorithm, show the process of
checking the balancing of parentheses by completing the table below. Based on the final
content of the stack, state whether the expression has balance parentheses or not.

Char Stack operation Balance Stack


(pop/push) Yes/No
Create
Top=
( Push No (
Top=(
( Push No ( (
Top=(
) Pop No (
Top=(
) Pop Yes
Top=empty
) Push yes
Top= empty
) push yes
Top= empty

Exercise 5: Stack Application – Algebraic Expression

a. Show the steps to convert the infix expressions below to postfix and manually.
i. 2*6-3+3*5+5

Postfix Prefix
Input Stack Output Output
2 2 +5+*53-3*62
* * 2
6 * 26
- - 26*
3 - 26*3
+ + 26*3-
3 + 26*3-3
* +* 26*3-3
5 +* 26*3-35
+ + 26*3-35*+
5 + 26*3-35*+5
26*3-35*+5+
ii. (2*6-3)+(3*5+5)

Postfix Prefix
Input Stack Output Output
( (
2 ( 2 +5+*53-3*62
* (* 2
6 (* 26
- (- 26*
3 (- 26*3
) 26*3-
+ + 26*3-
( +( 26*3-
3 +( 26*3-3
* +(* 26*3-3
5 +(* 26*3-35
+ +(+ 26*3-35*+
5 +(+ 26*3-35*+5
26*3-35*+5+

iii. (2*(6-3+3)*5)+5)

Postfix Prefix
Input Stack Output Output
( (
2 ( 2 +5*5*+3-362
* (* 2
( (*( 2
6 (*( 26
- (*(- 26
3 (*(- 263
+ (*(+ 263-
3 (*(+ 263-3
) (* 263-3+
* (* 263-3+*
5 (* 263-3+*5
) 263-3+*5*
+ + 263-3+*5*
5 + 263-3+*5*5
263-3+*5*5+
b. Answer the following question using stack implementation.
i. Using the stack algorithm to convert the infix notation to postfix, show each step to
convert the following infix expression to postfix in the table provided below.

8*(3+4)/2-3*5

Postfix
Input Stack Output
8 8
* * 8
( *( 8
3 *( 83
+ *(+ 83
4 *(+ 834
) * 834+
/ / 834+*
2 / 834+*2
- - 834+*2/
3 - 834+*2/3
* -* 834+*2/3
5 -* 834+*2/35
834+*2/35*
834+*2/35*-

a/b/c-(d+e)*f

Postfix
Input Stack Output
A a
/ / a
B / ab
/ / ab/
C / ab/c
- - ab/c/
( -( Ab/c/
D -( Ab/c/d
+ -(+ Ab/c/d
E -(+ Ab/c/de
) - Ab/c/de+
* -* Ab/c/de+
F -* Ab/c/de+f
Ab/c/de+f*
Ab/c/de+f*-
((a+b)/(c-d)+e*f-g

Postfix
Input Stack Output
( (
( ((
a (( a
+ ((+ a
b ((+ ab
) ( ab+
/ (/ ab+
( (/( ab+
c (/( ab+c
- (/(- ab+c
d (/(- ab+cd
) (/ ab+cd-
+ (+ ab+cd-/
e (+ ab+cd-/e
* (+* ab+cd-/e
F (+* ab+cd-/ef
- (- ab+cd-/ef*+
g ab+cd-/ef*+g
ab+cd-/ef*+g-
ab+cd-/ef*+g-(
Unbalance

You might also like