Data Structures: Lists, Stacks, and Queues - Chapter 3, Slides of Data Structures and Algorithms

A part of the EE 232 Data Structures course materials, focusing on Lists, Stacks, and Queues. It includes programming details, common errors, and problem-solving strategies. Students will learn about abstract data types, list implementation, and common errors in handling linked lists.

Typology: Slides

2020/2021

Uploaded on 07/03/2021

haleema-tallat
haleema-tallat 🇵🇰

5 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Data Structures: Lists, Stacks, and Queues - Chapter 3 and more Slides Data Structures and Algorithms in PDF only on Docsity!

Chapter 3

Lists, Stacks, and Queues

EE 232 Data Structures

Session-18 , Spring- 2020

Programming Details…

/* Return Position of X in L; NULL if not found */

Position

Find( ElementType X, List L )

Position P;

/* 1*/ P = L->Next;

/* 2*/ while( P != NULL && P->Element != X )

/* 3*/ P = P->Next;

/* 4*/ return P;

Running time?

Programming Details…

/* If X is not found, then Next field of returned value is NULL / / Assumes a header */

Position

FindPrevious( ElementType X, List L )

Position P;

/* 1*/ P = L;

/* 2*/ while( P->Next != NULL && P->Next->Element != X )

/* 3*/ P = P->Next;

/* 4*/ return P;

Running time?

The List ADT…

Programming Details…

❖ A free command is

issued to inform the

system that it may

reclaim the space.

❖ A consequence of the

free (P) command is

that the address that

P is pointing to is

unchanged, but the

data that reside at

that address are now

undefined.

/* Assume use of a header node / / Cell pointed to by P->Next is wiped out / / Assume that the position is legal / void Delete( ElementType X, List L ) { Position P, TmpCell; P = FindPrevious( X, L ); if( !IsLast( P, L ) ) { / X is found; delete it / TmpCell = P->Next; / Bypass deleted cell */ P->Next = TmpCell->Next; free( TmpCell ); } }

Running time?

Programming Details…

/* Insert (after (This decision is arbitrary) legal position P) / / Header implementation assumed / void Insert( ElementType X, List L, Position P ) { Position TmpCell; / 1/ TmpCell = malloc( sizeof( struct Node ) ); / 2/ if( TmpCell == NULL ) / 3/ FatalError( "Out of space!!!" ); / 4/ TmpCell->Element = X; / 5/ TmpCell->Next = P->Next; / 6*/ P->Next = TmpCell; }

Running time?

❖ The second common error

is that we think that

declaring a pointer to a

structure creates the

structure

❖ Use malloc to create a

structure

❖ It takes as an argument

the number of bytes to be

allocated and returns a

pointer to the allocated

memory

/* Correct DeleteList algorithm / void DeleteList( List L ) { Position P, Tmp; / 1/ P = L->Next; / Header assumed / / 2/ L->Next = NULL; / 3/ while( P != NULL ) { / 4/ Tmp = P->Next; / 5/ free( P ); / 6*/ P = Tmp; } }

❖ The second common error

is that we think that

declaring a pointer to a

structure creates the

structure

❖ Use malloc to create a

structure

❖ It takes as an argument

the number of bytes to be

allocated and returns a

pointer to the allocated

memory

/* Correct DeleteList algorithm / void DeleteList( List L ) { Position P, Tmp; / 1/ P = L->Next; / Header assumed / / 2/ L->Next = NULL; / 3/ while( P != NULL ) { / 4/ Tmp = P->Next; / 5/ free( P ); / 6*/ P = Tmp; } }

Doubly Linked Lists:

❖ Each node points to not only its successor but

also to its predecessor.

❖ More space is required (for an extra pointer).

❖ Cost of insertions and deletions doubles because

now more pointers to fix.

❖ It simplifies deletion, because you no longer have

to refer to a key by using a pointer to the previous

cell.

NULL A 0 A 1 A 2 A 3 NULL

Circularly Linked Lists:

❑ A popular convention is to have the last cell keep a

pointer back to the first.

❑ This can be done with or without a header and can

also be done with doubly linked lists.

A^ A 2 A 3 A 4

1 A double circularly linked list with no header

Implementation

❖ We could then write

routines to perform

addition, subtraction,

multiplication,

differentiation, and

other operations on

these polynomials.

typedef struct { int CoeffArray[MaxDegree + 1]; unsigned int HighPower; } *Polynomial; void ZeroPolynomial( Polynomial Poly ) { int i; for( i = 0; i <= MaxDegree; i++ ) Poly->CoeffArray[ i ] = 0; Poly->HighPower = 0; }

Lists, Stacks, and Queues (^3) - 17

Implementation

❖ Ignore the time to

initialize the output

polynomials to

zero, then running

time is proportional

to the product of

the degree of the

two input

polynomials

void MultPolynomial( const Polynomial Poly1, const Polynomial Poly2, Polynomial PolyProd ) { int i, j; ZeroPolynomial( PolyProd ); PolyProd->HighPower = Poly1->HighPower + Poly2->HighPower; if( PolyProd->HighPower > MaxDegree ) Error( "Exceeded array size" ); else for( i = 0; i <= Poly1->HighPower; i++ ) for( j = 0; j <= Poly2->HighPower; j++ ) PolyProd->CoeffArray[ i + j ] += Poly1->CoeffArray[ i ] * Poly2->CoeffArray[ j ]; }

❑ Singly Linked lists would definitely be a good alternative

to arrays for sparse polynomials.

1990 1492 2 1000 14 1

P x x x x

P x x x

P 1 10 1000 5 14 1 0 NULL
P^3 1990 -^2 149211 1 5

2 NULL Each node has the coefficient, the exponent, and the pointer to next

Lists, Stacks, and Queues (^3) - 20

Multi-lists

❑ Registration Problem

❖ A university with 40,000 students and 2,

courses needs to be able to generate two types of

reports

  • Lists the registration of each class
  • Lists, by student, the classes that each student

is registered for

❖ Array-based Implementation

  • 2D array of 100 million entries C C C C . . . . S1 S2……………………………..