Linked List Queue: Implementation and Operations, Exams of Data Structures and Algorithms

An explanation of how to implement a linked list queue data structure in c, including the creation, destruction, addition of elements at the back, removal of elements from the front, testing for presence of an element, and removal of an element by value. The document also discusses the importance of maintaining pointers to both the first and last links in the queue for efficient operations.

Typology: Exams

Pre 2010

Uploaded on 08/30/2009

koofers-user-k1x
koofers-user-k1x 🇺🇸

8 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Worksheet 10: Linked list Queue Name:
Worksheet 10: Linked List Queue, pointer to Tail
One problem with the linked list stack is that it only permits rapid access to the
front of the collection. This was no problem for the stack operations described in
the last lesson, but would present a difficulty if we tried to implement a queue,
where elements were inserted at one end and removed from the other.
A solution to this problem is to maintain two references into the collection of links.
As before, the data field firstLink refers to the first link in the sequence. But now a
second data field named lastLink will refer to the final link in the sequence:
(To reduce the size of the image the fields in the links have been omitted,
replaced with the graphic arrow). Both the firstLink and lastLink fields can be null
if a list is empty. Removing a data field from the front is the same as before, with
the additional requirement that when the last value is removed and the list
becomes empty the variable lastLink must also be set to null.
Because we have a link to the back, it is easy to add a new value to the end of
the list.
An Active Learning Approach to Data Structures using C 1
pf3

Partial preview of the text

Download Linked List Queue: Implementation and Operations and more Exams Data Structures and Algorithms in PDF only on Docsity!

Worksheet 10: Linked List Queue, pointer to Tail

One problem with the linked list stack is that it only permits rapid access to the front of the collection. This was no problem for the stack operations described in the last lesson, but would present a difficulty if we tried to implement a queue , where elements were inserted at one end and removed from the other. A solution to this problem is to maintain two references into the collection of links. As before, the data field firstLink refers to the first link in the sequence. But now a second data field named lastLink will refer to the final link in the sequence: (To reduce the size of the image the fields in the links have been omitted, replaced with the graphic arrow). Both the firstLink and lastLink fields can be null if a list is empty. Removing a data field from the front is the same as before, with the additional requirement that when the last value is removed and the list becomes empty the variable lastLink must also be set to null. Because we have a link to the back, it is easy to add a new value to the end of the list.

Complete the implementation of the LinkedListQueue based on these ideas. Each operation should have a constant execution time. Use an assertion to check that the queue is not empty prior to removing an element. Make sure all link fields are properly freed when no longer needed. Think carefully about how the values of firstLink and lastLink are set the first time a value is inserted into the queue, and when the last element is removed. struct linkedListQueue { struct link *firstLink; struct link *lastLink; }; void linkedListQueueCreate (struct linkedListQueue *s) { s->firstLink = s->lastLink = 0; } void linkedListQueueDestroy (struct linkedListQueue *s) { while (! linkedListQueueIsEmpty(s)) linkedListQueueRemovefront(s); } void linkedListQueueAddBack (struct linkedListQueue *s, EleType d) { } void linkedListQueueRemoveFront (struct linkedListQueue *s) { } EleType linkedListQueueFront (struct linkedListQueue *s) { } int linkedListQueueIsEmpty (struct linkedListQueue *s) { }