Linked List Deque Implementation in C, Exams of Data Structures and Algorithms

A worksheet on implementing a linked list deque using c programming language. It includes the structure definition, function prototypes, and function implementations for various operations such as initialization, destruction, adding elements at the front and back, and removing elements from the front and back. The document also encourages readers to explore the use of a single sentinel value for both the front and back of the deque, resulting in a circular queue.

Typology: Exams

Pre 2010

Uploaded on 08/30/2009

koofers-user-tdw
koofers-user-tdw 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Worksheet 11: Linked List Deque Name:
An Active Learning Approach to Data Structures using C
1
Worksheet 11: Linked List Deque
In this lesson and the following we will develop a reasonably complete linked list
abstraction. Our list will use sentinels on both the front and the back of the list,
and double links. In this lesson we will emphasize the deque aspects of the list.
In the following lesson we will add more operations.
A deque, you recall, allows
insertions at both the
beginning and the end of the
container. The interface is
shown at right.
Sentinels ensure the list is
never completely empty.
They also mean that all
instructions can be described
as special cases of more
general routines. One of
these, removeLink, you
developed in the previous
lesson. Another internal
routine, addBefore, can be
described as “insert a new
link immediate before an existing link”. Write this new routine, and then show how
all insertions can be handled by this one routine. As you add and remove
elements make sure you keep the value of the size field up to date. When a value
is removed from a list make sure you free the associated link fields. Use an
assertion to check that allocations of new links are successful, and that you do
not attempt to remove a value from an empty list.
Finish the implementation of the LinkedList based on these ideas:
backSentinel
frontSentinel
sentinel
7
4
Sentinel
struct list {
int size;
struct dlink *frontSentinel;
struct dlink *backSentinel;
};
void listInit (struct list *lst);
void listDestroy (struct list *lst);
void listAddFront (struct list *lst, EleType d);
void listAddBack (struct list *lst, EleType d);
void listRemoveFront (struct list *lst);
void listRemoveBack (struct list *lst);
EleType listFront (struct list *lst);
EleType listBack (struct list *lst);
int listSize (struct list *lst);
pf3
pf4

Partial preview of the text

Download Linked List Deque Implementation in C and more Exams Data Structures and Algorithms in PDF only on Docsity!

Worksheet 11: Linked List Deque

In this lesson and the following we will develop a reasonably complete linked list abstraction. Our list will use sentinels on both the front and the back of the list, and double links. In this lesson we will emphasize the deque aspects of the list. In the following lesson we will add more operations. A deque, you recall, allows insertions at both the beginning and the end of the container. The interface is shown at right. Sentinels ensure the list is never completely empty. They also mean that all instructions can be described as special cases of more general routines. One of these, removeLink, you developed in the previous lesson. Another internal routine, addBefore, can be described as “insert a new link immediate before an existing link”. Write this new routine, and then show how all insertions can be handled by this one routine. As you add and remove elements make sure you keep the value of the size field up to date. When a value is removed from a list make sure you free the associated link fields. Use an assertion to check that allocations of new links are successful, and that you do not attempt to remove a value from an empty list. Finish the implementation of the LinkedList based on these ideas: backSentinel frontSentinel sentinel 7 4 Sentinel struct list { int size; struct dlink *frontSentinel; struct dlink *backSentinel; }; void listInit (struct list *lst); void listDestroy (struct list *lst); void listAddFront (struct list *lst, EleType d); void listAddBack (struct list *lst, EleType d); void listRemoveFront (struct list *lst); void listRemoveBack (struct list *lst); EleType listFront (struct list *lst); EleType listBack (struct list *lst); int listSize (struct list *lst);

void listInit (struct list *lst); { } void listDestroy (struct list *lst) { } void listAddBefore (struct dlink * lnk, EleType d) { } void listAddFront (struct list *lst, EleType d) { } void listAddBack (struct list *lst, EleType d) { } void listRemoveFront (struct list *lst) { }

On Your Own

  1. Explain how the use of a tail sentinel allows us to view insertions at both the front and the back of the deque as special cases of the same operation. What is this operation?
  2. What are algorithmic time complexities of the deque operations in this implementation?
  3. Suppose you wanted to test your implementation of the linked list deque. What would be good boundary test cases? Write a test harness to execute the linked list deque using your test cases. An alternative to the use of both a head and tail sentinel is the use of a single sentinel value for both positions. This is shown in the following illustration. Now there are no null pointers whatsoever, as every link points to another link. Insertions and removals from the front of the deque are performed to one side of the sentinel, while insertions and removals from the back are performed on the opposite side. This structure is termed a circular queue.