Vous êtes sur la page 1sur 7

CTIS 152 Algorithms and Data Structures

PROBLEM SET 9 – Linked Lists

1. Make suitable definitions for the following:

a) A node that will contain a character


b) A node that will contain the name of a person
c) A node that will contain the name, date of birth, sex and nationality of a person
d) A node that will contain the ID, name, midterm1, midterm2 and final grades of a student

2. Define the functions for linked list operations (add_beginning, add_after, delete_first,
delete_after), for each list consisting of nodes you defined in Question 1.

3. Using the definitions you made in Question 1 and the functions you defined in Question 2, write
program segments for the following problems:

a) Read a string, and store its characters in a linked list in reverse order.
b) Read information (ID, name, midterm1, midterm2 and final grades) about some students taking a
certain course, and store them in a linked list, in the same order as they are entered. Use 0 as the
sentinel value for the student ID.
c) Read the names of 100 persons from a text file, and store them in a linked list in alphabetical
order.
d) Read information (name, date of birth, sex and nationality) about 500 persons from a binary file,
and store the females and males in two different linked lists, in alphabetical name order.
e) Merge the two lists you created in the previous question, creating a third list.
f) Resolve the previous question, without creating a third list, but using one of the lists as the
resulting list.

Linked Lists – Page 1


4. Using the linked list you created in Question 3.b., write program segments for the following
problems:

a) Find how many of the students got the average grade from the second midterm.
b) Find the name of the student who got the maximum grade from the first midterm.
c) Find the standard deviation of midterm 1.
d) Display the average and the standard deviation of the second midterm, and the IDs of the
students who got the largest and lowest grade of the exam.
e) Find the average of the final exam, excluding the largest and lowest grades of the exam.
f) Output only the final exam grades below the average if the average of the final exam is less than
50, otherwise output only the grades above the average.
g) A student passes a course if his overall grade is 50 or more. Calculate the overall grade of each
student, using 0.3, 0.3 and 0.4 as the weight of each exam, respectively. Display the names of the
students who passed that course and the names of the students who failed, with proper headings.
h) A student takes 0 from an exam only if he did not take the exam. Display the names of the
students who did not take each exam, with proper headings.
i) Sort the students in ascending ID order, using buble sort algorithm.

5. Using the linked list you created in Question 3.c. and the functions you defined in Question 2,
write functions for the following problems. Use recursion, if you think it is proper.

a) Delete the Nth person from the list.


b) Add a new person to the list, without destroying the alphabetical order.
c) Given a name, find the address of the node containing that name.
d) Given a certain letter, find how many persons in the list have a name starting with that letter.
e) Given a certain letter, find how many persons in the list have a name ending with that letter.
f) Given a certain string, display all persons whose names contain that string.
g) Given a certain letter, delete the first person whose name starts with that letter, from the list.
h) Given a certain letter, delete all persons whose names start with that letter, from the list.
i) Display the list in reverse order.

Linked Lists – Page 2


j) Reverse the list.
k) Find the address of the middle node.

6. Trace the following program segments, for the given lists. Assume that list points to the first
node of the list.

a) 34, 5, 2, 1, 9, 8, 0, 3, 6
while (list->data > 1)
list = list->next->next;
printf (“%d\n”, list->data + list->next->data);

b) 1, 2, 3, 4, 5, 6
sum = 0;
while (list->data < 5)
{ sum += list->data;
list = list->next;
}
printf (“%d\n”, sum);

c) 1, 2, 3, 4, 5, 6
sum = 0;
while (list->data < 5)
{ list = list->next;

Linked Lists – Page 3


sum += list->data;
}
printf (“%d\n”, sum);

d) 4, 5, 21, 28, 29, 18, 60, 73, 86


sum = 0;
while (list->data < list->next->data)
{ list = list->next;
sum += list->data;
}
printf (“%d\n”, sum);

e) 4, 5, 21, 28, 29, 18, 60, 73, 86


list->next->next = list;
printf(“%d\n”, list->next->next->next->next->data);
printf(“%d\n”, list->next->next->next->next->next->next->next->data);

7. Trace and find the output of the following program segments, for the given input data. Assume
that maximum stack and queue sizes are 5, and the linked list functions (add_beginning and
add_after) are already defined.

#include <stdio.h>
#include <stack_int.h>
#include <queue_int.h>

Linked Lists – Page 4


typedef struct node_s
{ int data;
struct node_s *next;
} node_t;

stack_t s;
queue_t q;
node_t *headp, *p;
int x, y;
initialize_stack (&s);
InitializeQ (&q);
headp = NULL;
a) INPUT
printf (“%d %d %d\n”, s.top, q.front, q.rear); 15
while (!is_full (s)) 8
{ scanf (“%d”, &x); 11
push (&s, x); 9
if (s.top >= 2) 3
InsertQ (&q, x*2);
else

Linked Lists – Page 5


headp = add_beginning (headp, x/2);
}
printf (“%d %d %d\n”, s.top, q.front, q.rear);
while (!isEmptyQ (q))
{ x = RemoveQ (&q);
y = pop (&s);
printf (“%d %d\n”, x, y);
if (x + y > 18)
headp = add_beginning (headp, x);
else
add_after (headp->next, y);
}
printf (“%d %d %d\n”, s.top, q.front, q.rear);
p = headp;
while (p != NULL)
{ printf (“%d\n”, p->data);
p = p->next;
}

b) INPUT

printf (“%d %d %d\n”, q.front, q.rear, s.top); 15

Linked Lists – Page 6


while (!is_full (s)) 8
{ scanf (“%d”, &x); 10
push (&s, x); 9
if (s.top >= 2) 14
InsertQ (&q, x/2);
else
headp = add_beginning (headp, x*2);
}
printf (“%d %d %d\n”, q.front, q.rear, s.top);
while (!isEmptyQ (q))
{ x = pop (&s);
y = RemoveQ (&q);
printf (“%d %d\n”, x, y);
if (x + y > 15)
headp = add_beginning (headp, x);
else
add_after (headp->next, y);
}
printf (“%d %d %d\n”, q.front, q.rear, s.top);
p = headp;
while (p->next != NULL)
{ printf (“%d\n”, p->data);
p = p->next;
}

8. Discuss the following:

a) In a forward linked list, why is it easier to insert a new node after a specific node rather than
before the specific node? Is inserting before a specific node impossible?

b) We have been using stacks and queues in the form of array implementation. If we used a stack or
a queue in the form of a linked list,
 What would be the conditions for a full stack, an empty stack, a full queue, and an empty
queue?
 What would the functions pop, push, insert, remove do?

c) Although it has many advantages, what is the major disadvantage of a linked list in comparison to
an array?

Linked Lists – Page 7

Vous aimerez peut-être aussi