Docsity

Lists, Linked Lists and their Implementations | CSC 3410, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Lists, Linked Lists and their Implementations | CSC 3410 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Lists, linked lists, and their

implementations

Ken NguyenSpring 2002

Introduction

•^

A vector has two disadvantages:– It is not efficient when the number of elements to be

stored varies widely during a program execution.

  • It is expensive to add or remove items anywhere except

at the end (possibly the beginning)

•^

List:

A

list

is a kind of sequence that supports

bidirectional iterators and allows constant time insert anderase operations anywhere within the sequence, withstorage management handled automatically. Unlike vectorsfast random access to list elements is not supported, butmany algorithms only need sequential access anyway

Basic ideas

template struct Node {

Object elementNode * next;

}; Last node has the next pointer point to NULL.

a^

b^

c^

d

Front of list

Figure 17.

Basic ideas cont...

Inserting a node into a list: Node *tmp = new Node;tmp->element = x;tmp->next = current->next;current->next = tmp;

a^

b

x

current

tmp

Fig. 17.

Header node

  • A header node holds no data but serves to

satisfy the requirement that every node hasa previous node.

a^

b^

c

header

Fig. 17.

header Fig. 17.5 an empty list

Simple Linked list

Node

Node

Node

Node

A list

Circular linked list

Node Node

Node

Node

Node

Doubly linked list

Node

Node

Node

Node

Node

struct Node{

**Object element;Node prev;Node next;

};

Insertion on doubly linked list

newNode = new Node; newNode->element = x;newNode->prev = current;newNode->next = current->next;newNode->prev->next = newNode;newNode->next->prev = newNode;current = newNode;

b

x

a

3 1

2

4

Fig. 17.

Sorted linked list

  • To make a linked list a sorted linked list,

what has to be changed is the insertfunction.

STL List Class

#include

List header

list v;

Default constructor O(1)

list v(aList);

Copy constructor O(n)

v = aList;

Assignment O(n)

v.swap(aList);

Swap values with another list O(1)

v.remove(value);

Remove all occurrences of value O(n)

v.remove_if(predicate);

Remove all values that match the conditionO(n)

list::iterator itr;

Declare a new iterator O(1)

v.begin();

Starting iterator O(1)

v.end();

Ending iterator O(1)

v.rbegin();

Starting backward moving iterator O(1)

v.rend()

Ending backward moving itarator O(1)

v.merge(aList);

Merge with another ordered list O(n)

v.sort();

Place elements in accending order O(nlogn)

STL List Examples

#include list list_one;list_one.push_front(5);list_one.push_back(10);int a = list_one.front();.. .list_one.remove(4);list_one.remove_if(even); //assume even is a functorlist::iterator start = list_one.begin();list::iteartor stop = list_one.end();list_one.erase(start,stop);

Sequences and containers

bitsetdequelist queuestackVector

Header

Class

STL Sequence Container

operations

post: size() == distancebetween i and j.constructs a sequence equalto the range

[i,j

X(i, j)X a(i, j);

post: size() == n.constructs a sequence with

n

copies of t.

X(n, t)X a(n, t);

assertion/notepre/post-condition

returntype

expression