CP264 A8 Assignment

You might also like

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

CP264 Spring 2022 A8

A8: Stacks and Queues

General Instructions:

1- Create a C project in eclipse called A8. Download "A8.zip" file and extract the
contents to the A8 project.
2- Rename: "A8_template.txt" to "A8.c". Enter your credentials on top of the file.
3- Copy the following files to your project:
a. process.h and process.c
b. data.h and data.c
c. node.h and node.c
4- Use the file "A8_test.c” to test your solution. Compare the output with those
presented in "A8_output.txt"
5- When you are done, you need to submit only your "A8.c” file. Do not export
the project or upload a .zip file.
General Instructions:

In R8, you have learned about the Stack and Queue ADT implementation using the C
language. In R9, you have learned about the C implementation of the Linked List ADT.
In this assignment, you will work towards implementing stacks and queues using
linked lists.

One way to do that is to redefine the stack and queue structs as the following:

typedef struct{ typedef struct{


int capacity; int capacity;
Linked_List* list; Linked_List* list;
}Stack; }Queue;

Then, you can call the linked list functions to do stack and queue operations. For
instance, the implementation of a pop_stack will be a simple call to:

Qutaiba Albluwi © 2022


CP264 Spring 2022 A8

remove_linked_list(<stack_parameter>,0), which removes the first element in the

linked list. Also, the implementation of insert_queue, will be a call to the


insert_linked_list function at the last index.

However, the above approach is not good for several reasons. First, the linked list
implementation is relatively larger than that of stack and queue. This means we will
be using a large library for something simple. Second, if there is an error, the error
message will have the tag of linked list. This will reveal to the user some information
about the underlying implementation which goes against the ADT principle. There are
other reasons, which can be discussed in other settings1.

A better approach would be to implement the Stack and Queue ADTs using linked list
techniques, not the Linked List ADT. This means both ADTs will contain nodes, which
behave like a linked list. To do that, we will only need to redefine the Stack and Queue
structs, while keeping all function prototypes the same. Of course, the
implementation of these functions would be different, but this is indifferent to the
user, and is done seamlessly.

If you go to A8.h, you will see that the following definitions are provided:

typedef struct { typedef struct {


int capacity; int capacity;
Node* _top; Node* _front;
} Stack; int _size;
} Queue;
Note that, in both ADTs, the only "public" data member is the capacity, which means
the user does not care what and how we internally implement the ADT.

You can also notice that the Queue contains a data member called _size, while the
Stack does not. Indeed, we can implement each one of them while using _size or

1
One other reason is that for the sake of this assignment, using the above would be boring and less fun.

Qutaiba Albluwi © 2022


CP264 Spring 2022 A8

without using it. However, we chose to provide different definitions to diversify the
implementations.

In both ADTs, the create function dynamically creates the ADT and sets the pointers
to NULL. When adding data elements, the push/insert function should create a node
to encompass the given data and then add it to the end of the linked list. For the
pop/remove, the function should remove the proper element, retain a copy of the
data and destroy the node.

For the len function, if you are using _size, then it is straightforward. Otherwise, you
need to traverse the list, counting the nodes along the way.

The output of executing the above implementations should match those produced by
the array implementation. Indeed, the A8_test.c file is almost identical to R8_test.c
file.

Qutaiba Albluwi © 2022

You might also like