CSE 2320 - Assignments - Assignment 5

Submit to Blackboard before the deadline. You will be able to revise your answers until the deadline with no penalty.

IMPORTANT: By submitting your answers, you are certifying that these answers have been exclusively your own work. All students enrolled in this course are expected to adhere to the UT Arlington Honor Code:

I pledge, on my honor, to uphold UT Arlington's tradition of academic integrity, a tradition that values hard work and honest effort in the pursuit of academic excellence. I promise that I will submit only work that I personally create or contribute to group collaborations, and I will appropriately reference any work from other sources. I will follow the highest standards of integrity and uphold the spirit of the Honor Code.


Task 1 (30 pts.)

Write a function int stack_length(Stack stack) that counts and returns the number of items in a stack, and satisfies the following requirements:

As an example, consider the following main function:

int main()
{
  Stack stack = newStack();
  
  pushInt(stack, 10);
  pushInt(stack, 20);
  pushInt(stack, 30);
  printf("before counting the length, the stack is:\n");
  printIntStack(stack);

  int length = stack_length(stack);
  printf("the length of the stack is %d\n", length);
  printf("after counting the length, the stack is:\n");
  printIntStack(stack);

  destroyStack(stack);}
If you correctly implement the stack_length function, this main function should print:
before counting the length, the stack is:

position 0 from the top: 30
position 1 from the top: 20
position 2 from the top: 10

the length of the stack is 3
after counting the length, the stack is:

position 0 from the top: 30
position 1 from the top: 20
position 2 from the top: 10
Of course, it is not sufficient for your function to only behave correctly in the above example, it should work with any example, and stacks of any size.


Task 2 (20 pts.)

Write a function void reverse_stack(Stack stack) that reverses the order of items in its argument stack, under the following requirements: If you are in doubt, feel free to e-mail your code to the instructor to verify if it complies with these requirements.

As an example, consider the following main function:

int main()
{
  Stack stack = newStack();
  
  pushInt(stack, 10);
  pushInt(stack, 20);
  pushInt(stack, 30);
  printf("before reversing, the stack is:\n");
  printIntStack(stack);

  reverse_stack(stack);
  printf("after reversing, the stack is:\n");
  printIntStack(stack);

  destroyStack(stack);
}
If you correctly implement the reverse_stack function, this main function should print:
before reversing, the stack is:

position 0 from the top: 30
position 1 from the top: 20
position 2 from the top: 10

after reversing, the stack is:

position 0 from the top: 10
position 1 from the top: 20
position 2 from the top: 30
Of course, it is not sufficient for your function to only behave correctly in the above example, it should work with any example, and stacks of any size.


Task 3 (30 pts.)

File queues.h defines an interface for FIFO queues. Complete this implementation by writing code in a file called queues_arrays.c. Your implementation should be array-based for this task. Feel free to use/modify existing code from stacks_arrays.c.

Your put and get functions should produce errors and exit the program when the queue is respectively full or empty, in exactly the same way in which push and pop behave in stacks_arrays.c.

As an example, consider the following main function:

int main()
{
  Queue queue = newQueue1(4);
  
  int value;
  putInt(queue, 15);
  putInt(queue, 20);

  value = getInt(queue);
  printf("got value %d\n", value);

  putInt(queue, 30);
  putInt(queue, 7);
  putInt(queue, 25);

  value = getInt(queue);
  printf("got value %d\n", value);

  putInt(queue, 12);

  value = getInt(queue);
  printf("got value %d\n", value);

  value = getInt(queue);
  printf("got value %d\n", value);

  destroyQueue(queue);
}
If you correctly implement the FIFO queue interface, this main function should print:
got value 15
got value 20
got value 30
got value 7
Of course, it is not sufficient for your function to only behave correctly in the above example, it should work with any example, and queues of any size.


Task 4 (20 pts.)

File queues.h defines an interface for FIFO queues. Complete this implementation by writing code in a file called queues_lists.c. Your implementation should be list-based for this task. Feel free to use/modify existing code from stacks_lists.c.

Your get function should produce an error and exit the program when the queue is empty, in exactly the same way in which pop behave in stacks_lists.c.

The example main function from task three should produce the exact same output for task 4.


How to submit

The assignment should be submitted via Blackboard. Submit a ZIPPED directory called assignment.zip. No other forms of compression are accepted, contact the instructor or TA if you do not know how to produce .zip files. The zipped directory should contain the following documents:

As stated on the course syllabus, programs must be in C or Java, and must run on omega.uta.edu. If you want to do this in any language other than C, you have to re-implement in that language all the code that is currently provided and implemented in C. If you want to use another language or platform, you must obtain prior approval via e-mail by the instructor.

IMPORTANT: Pay close attention to all specifications on this page, including file names and submission format. Even in cases where your answers are correct, points will be taken off liberally for non-compliance with the instructions given on this page (such as wrong file names, wrong compression format for the submitted code, and so on). The reason is that non-compliance with the instructions makes the grading process significantly (and unnecessarily) more time consuming. Contact the instructor or TA if you have any questions.


Back to the list of assignments.