Docsity

Lists Plus - Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Lists Plus - Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Chapter 6

Lists Plus

Lecture 12

What is a Circular Linked List?

• A circular linked list is a list in which every

node has a successor; the “ last ” element

is succeeded by the “ first ” element.

What is a Doubly Linked List?

• A doubly linked list is a list in which

each node is linked to both

its successor and its predecessor.

Linking the New Node into the List

What are Header and Trailer Nodes?

• A Header Node is a node at the beginning of a list

that contains a key value smaller than any

possible key.

• A Trailer Node is a node at the end of a list that

contains a key larger than any possible key.

• Both header and trailer are placeholding nodes

used to simplify list processing.

A linked list in static storage?

A Linked List as an Array of Records

An Array with Linked List of Values and Free Space

An Array with Three Lists (Including the Free List)

// CLASS TEMPLATE DEFINITION

#include "ItemType.h" // for MAX_ITEMS and ItemType

template // formal parameter list

class StackType

{

public:

StackType( ); bool IsEmpty( ) const; bool IsFull( ) const; void Push( ItemType item ); void Pop( ItemType item ); ItemType Top( );

private:

int top; ItemType items[MAX_ITEMS];

};

StackType numStack;

top 3

[MAX_ITEMS-1] . . .

[ 3 ] 789
[ 2 ] -
[ 1 ] 132

items [ 0 ] 5670

ACTUAL PARAMETER

top 3

[MAX_ITEMS-1] . . .

[ 3 ] Bradley
[ 2 ] Asad
[ 1 ] Rodrigo

items [ 0 ] Max

StackType numStack;

ACTUAL PARAMETER
// SAMPLE CLASS MEMBER FUNCTIONS

template // formal parameter list

StackType::StackType( )

{

top = -1;

}

template // formal parameter list

void StackType::Push ( ItemType newItem )

{

if (IsFull()) throw FullStack(); top++; items[top] = newItem; // STATIC ARRAY IMPLEMENTATION

}

When creating class template

  • Put .h and .cpp code in the same .h file

or

  • Have .h include .cpp file at the end

Recall Definition of Stack

• Logical (or ADT) level: A stack is an

ordered group of homogeneous items

(elements),

• in which the removal and addition of

stack items can take place only at the

top of the stack.

• A stack is a LIFO “last in, first out”

structure.