Worksheet 17: Linked List Introduction List Stack | CS 261, Assignments of Data Structures and Algorithms

Material Type: Assignment; Professor: Budd; Class: DATA STRUCTURES; Subject: Computer Science; University: Oregon State University; Term: Winter 2009;

Typology: Assignments

Pre 2010

Uploaded on 08/30/2009

koofers-user-wo0-1
koofers-user-wo0-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Worksheet 17: Linked List Introduction List Stack Name:
An Active Learning Approach to Data Structures using C
1
Worksheet 17: Linked List Introduction, List Stack
In Preparation: Read Chapter 6 to learn more about the Stack data type.
In Lesson 16 you built a Stack using a dynamic array as the underlying container. A
weakness of the Dynamic Array is that elements are stored in a contiguous block. As a
consequence, when a new element is inserted into the middle of the collection, all the
adjacent elements must be moved in order to make space for the new value.
An alternative approach is to use the idea of a
Linked List. In a linked list each value is stored in a
separate block of memory, termed a link. In addition
to a value, each link contains a reference to the next
link in sequence. As a data structure, a link can be
described as shown at right.
We can visualize collection formed out of links as follows. A data field named firstLink
will hold the first link in the collection. Each link refers to the next. The final link will
have a null value in the next field:
The simplest data structure to create using links is a Stack. When a new element is
pushed on the stack a new link will be created and placed at the front of the chain.
struct link {
EleType value;
struct link * next;
};
pf2

Partial preview of the text

Download Worksheet 17: Linked List Introduction List Stack | CS 261 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Worksheet 17: Linked List Introduction List Stack Name:

An Active Learning Approach to Data Structures using C 1

Worksheet 17: Linked List Introduction, List Stack

In Preparation : Read Chapter 6 to learn more about the Stack data type.

In Lesson 16 you built a Stack using a dynamic array as the underlying container. A

weakness of the Dynamic Array is that elements are stored in a contiguous block. As a

consequence, when a new element is inserted into the middle of the collection, all the

adjacent elements must be moved in order to make space for the new value.

An alternative approach is to use the idea of a

Linked List. In a linked list each value is stored in a

separate block of memory, termed a link. In addition

to a value, each link contains a reference to the next

link in sequence. As a data structure, a link can be

described as shown at right.

We can visualize collection formed out of links as follows. A data field named firstLink

will hold the first link in the collection. Each link refers to the next. The final link will

have a null value in the next field:

The simplest data structure to create using links is a Stack. When a new element is

pushed on the stack a new link will be created and placed at the front of the chain.

struct link { EleType value; struct link * next; };

Worksheet 17: Linked List Introduction List Stack Name:

An Active Learning Approach to Data Structures using C 2

To remove a link the variable firstLink is simply changed to point to the next element in

the chain. The space for the Link must then be freed.

The following is the beginning of an implementation of a LinkedListStack based on

these ideas. Complete the implementation. Each operation should have constant time

performance. Use an assertion to ensure that when a top or pop is performed the stack has

at least one element. When you pop a value from the stack, make sure you free the link

field.

struct link { EleType value; struct link * next; }; struct linkedListStack { struct link *firstLink; } void linkedListStackInit (struct linkedListStack * s) { s->firstLink = 0; } void linkedListStackFree (struct linkedListStack *s) { while (! linkedListStackIsEmpty(s)) linkedListStackPop(s); } void linkedListStackPush (struct linkedListStack *s, double d) { struct link * newLink = (struct link *) malloc(sizeof(struct link)); assert (newLink != 0); } double linkedListStackTop (struct linkedListStack *s) { } void linkedListStackPop (struct linkedListStack *s) { } int linkedListStackIsEmpty (struct linkedListStack *s) { }