03 Linked List

Download as pdf or txt
Download as pdf or txt
You are on page 1of 57

Discrete Mathematics: Logic

Data Structure:
List

chap. 4.1-4.2, 4.8


L i st ADT

an ordered sequence of element <A1, A2, A3, . . ., AN>


the size of the list is N
a list of size 0 is an empty list
Ai+1 follows (succeeds) Ai (i<N) and Ai-1 precedes Ai (i>1)
the posiGon of an element Ai in a list is i

operaGons in the List ADT


MakeEmpty (List L): constructor
DeleteList (List L): destructor
Find (List L, Key K): returns the posiGon of the key
Insert (Key K, List L, PosiGon P): insert K aPer P in L
Delete (Key K, List L): delete K from L
Concat (List L1, List L2): returns the concatenaGon of L1 and L2
L i st ADT: a n exa mp l e

List: L = <34, 12, 52, 16, 12>


Find(L, 52): 3
Insert(X, L, 3): 34, 12, 52, X, 16, 12
Delete(52, L): 34, 12, X, 16, 12

Find (List L, Key K): returns the posiGon of the key


Insert (Key K, List L, PosiGon P): insert K aPer P in L
Delete (Key K, List L): delete K from L
L i st ADT: simple implementaGon with array

Insert X aPer the posiGon 3 in the list L

List L 34 12 52 16 12

make space for X

34 12 52 16 12

insert X

34 12 52 X 16 12
L i st ADT: simple implementaGon with array

it is inefficient because . . .
an esGmate of the maximum size of the list is required
it requires overesGmaGng the amount of storage needed for the list
it is hard to insert or delete at the beginning or in the middle of the list
worst case: O(N)
average case: half of the list O(N)
building a list by N successive inserts: O(N2)
L i st ADT: simple implementaGon with array

it is inefficient because . . .
an esGmate of the maximum size of the list is required
it requires overesGmaGng the amount of storage needed for the list
it is hard to insert or delete at the beginning or in the middle of the list
worst case: O(N)
average case: half of the list O(N)
building a list by N successive inserts: O(N2)
L i st ADT: linked list

a linked list consists of a series of structures, which are not


necessarily adjacent in memory
each structure contains an element and a pointer to a structure of its
successor
the last cell’s pointer points to NULL

A1 A2 A3 A4 A5
L i st ADT: linked list

A1 A2 A3 A4 A5
L i st ADT: linked list

A1

1000
A1 0
L i st ADT: linked list

A1 A2

1000 800
A1 800 A2 0
L i st ADT: linked list

A1 A2 A3

1000 800 712


A1 800 A2 712 A3 0
L i st ADT: linked list

A1 A2 A3 A4

1000 992 800 712


A1 800 A4 0 A2 712 A3 992
L i st ADT: linked list

A1 A2 A3 A4 A5

1000 992 800 712 692

A1 800 A4 692 A2 712 A3 992 A5 0


L i st ADT: linked list

A1 A2 A3 A4 A5

1000 992 800 712 692

A1 800 A4 692 A2 712 A3 992 A5 0


L i st ADT: example
second

first 10 20
L i st ADT: example
second
typedef struct Node* PtrToNode;
first 10 20
typedef struct Node {

int data;
PtrToNode link;
};

PtrToNode create2() {
PtrToNode first, second;
MALLOC(first, sizeof(*first));
MALLOC(second, sizeof(*second));
second->link = NULL;
second->data = 20;
first->data = 10;
first->link = second;
return first;
}
L i s t A D T: insertion

A1 A2 A3 A4 A5
L i st ADT: inserGon

34

A1 A2 A3 A4 A5
L
34
L i st ADT: inserGon

34

A1 A2 A3 A4 A5
L
34 p

Insert(X, L, P)

Insert (Key K, List L, PosiGon P): insert K aPer P in L


L i st ADT: inserGon

A1 A2 A3 A4 A5

L p

Insert(X, L, P)

A1 A2 A3 A4 A5

p
L
X
L i st ADT: deleGon

A1 A2 A3 A4 A5

p
L
Delete(A3, L)

A1 A2 A3 A4 A5
p
L

A1 A2 A3 A4 A5

A1 A2 A4 A5

L
l i st ADT: typ e d ecl arati o n fo r a l i n ked l i st

typedef struct Node* PtrToNode;


typedef int ElementType;
typedef PtrToNode Position;
typedef PtrToNode List;

struct Node {
ElementType element;
Position next;
};

List MakeEmptyList();
int IsEmpty(List list);
int IsLast(Position p, List list);

Position Find(ElementType x, List list);


Position FindPrevious(ElementType x, List list);

void Delete(ElementType x, List list);


void Insert(ElementType x, List list, Position p);
void DeleteList(List list);
l i st ADT: MakeE mpty

/* create header node */

List MakeEmptyList() {
struct Node {
List list;
ElementType element;
list = (List) malloc(sizeof(struct Node));
Position next;
list->element = header;
};
list->next = NULL;
return list;
}

header

list
l i st ADT: Is E mpty

/* return true if list is empty */

int IsEmpty(List list) { struct Node {


ElementType element;
} Position next;
};

header true

list

false
header 6

list
l i st ADT: Is E mpty

/* return true if list is empty */

int IsEmpty(List list) { struct Node {


return list->next == NULL; ElementType element;
} Position next;
};

header true

false
header

L
l i st ADT: Is L ast

/* return true if p is the last position in list */

int IsLast(Position p, List list) { struct Node {


ElementType element;
} Position next;
};

header true

list p

header false

list p
l i st ADT: Is L ast

/* return true if p is the last position in list */

int IsLast(Position p, List list) { struct Node {


return p->next == NULL; ElementType element;
} Position next;
};

header true

list p

header false

list p
l i st ADT: F i n d

/* return position of x in list; NULL if not found */

Position Find(ElementType x, List list) {


struct Node {
ElementType element;
Position next;
};

header A1 X A2 A3

list
l i st ADT: F i n d

/* return position of x in list; NULL if not found */

Position Find(ElementType x, List list) {


Position p; struct Node {
p = list->next; ElementType element;
Position next;
};
return p;
}

header A1 X A2 A3

list p
l i st ADT: F i n d

/* return position of x in list; NULL if not found */

Position Find(ElementType x, List list) {


Position p; struct Node {
p = list->next; ElementType element;
while (p != NULL && p->element != x) { Position next;
};
}
return p;
}

header A1 X A2 A3

list p
l i st ADT: F i n d

/* return position of x in list; NULL if not found */

Position Find(ElementType x, List list) {


Position p; struct Node {
p = list->next; ElementType element;
while (p != NULL && p->element != x) { Position next;
p = p->next; };
}
return p;
}

header A1 X A2 A3

list p
l i st ADT: F i n d

/* return position of x in list; NULL if not found */

Position Find(ElementType x, List list) {


Position p; struct Node {
p = list->next; ElementType element;
while (p != NULL && p->element != x) { Position next;
p = p->next; };
}
return p;
}

header A1 X A2 A3

list p

header A1 A2

list p
l i st ADT: F i n d Previ o u s

Position FindPrevious(ElementType x, List list) {


Position p;
p = list;
while (p->next != NULL &&

) { struct Node {
p = p->next; ElementType element;
} Position next;
return p; };
}

header

list

header A1 A2 X A3

list
P
l i st ADT: F i n d Previ o u s

Position FindPrevious(ElementType x, List list) {


Position p;
p = list;
while (p->next != NULL && 

p->next->element != x) { struct Node {
p = p->next; ElementType element;
} Position next;
return p; };
}

header

list

header A1 A2 X A3

list
P
l i st ADT: F i n d Previ o u s

Position FindPrevious(ElementType x, List list) {


Position p;
p = list;
while (p->next != NULL && 

p->next->element != x) { struct Node {
p = p->next; ElementType element;
} Position next;
return p; };
}

header

list

header A1 A2 X A3

list P
l i st ADT: F i n d Previ o u s

Position FindPrevious(ElementType x, List list) {


Position p;
p = list;
while (p->next != NULL && 

p->next->element != x) { struct Node {
p = p->next; ElementType element;
} Position next;
return p; };
}

header

list

header A1 A2 X A3

list P
L i st ADT: I n s ert

void Insert(ElementType x, List list, Position p) {

struct Node {
ElementType element;
Position next;
};

A1 A2 A3 A4 A5

list P
L i s t A D T: I n s e r t

void Insert(ElementType x, List list, Position p) {


Position tmp;
tmp = (Position) malloc(sizeof(struct Node));
if (tmp == NULL) {
FatalError(“Out of space!!!”); struct Node {
} ElementType element;
tmp->element = x; Position next;
};

A1 A2 A3 A4 A5

list P
X

tmp
L i s t A D T: I n s e r t

void Insert(ElementType x, List list, Position p) {


Position tmp;
tmp = (Position) malloc(sizeof(struct Node));
if (tmp == NULL) {
FatalError(“Out of space!!!”); struct Node {
} ElementType element;
tmp->element = x; Position next;
tmp->next = p->next; };

A1 A2 A3 A4 A5

list P
X

tmp
L i s t A D T: I n s e r t

void Insert(ElementType x, List list, Position p) {


Position tmp;
tmp = (Position) malloc(sizeof(struct Node));
if (tmp == NULL) {
FatalError(“Out of space!!!”); struct Node {
} ElementType element;
tmp->element = n; Position next;
tmp->next = p->next; };
p->next = tmp;
}

A1 A2 A3 A4 A5

list P
X

tmp
l i st ADT: D el ete

void Delete(ElementType x, List list) {

struct Node {
ElementType element;
Position next;
};

header A1 A2 X A3

list
l i st ADT: D el ete

void Delete(ElementType x, List list) {


Position p, tmp;

p = FindPrevious(x, list);
struct Node {
ElementType element;
Position next;
};

header A1 A2 X A3

list P
l i st ADT: D el ete

void Delete(ElementType x, List list) {


Position p, tmp;

p = FindPrevious(x, list);
if (!IsLast(p, list)) { /* X is found; delete it */
p->next = ???; /* Bypass deleted cell */

}
}

header A1 A2 X A3

list P
l i s t A D T: D e l e t e

void Delete(ElementType x, List list) {


Position p, tmp;

p = FindPrevious(x, list);
if (!IsLast(p, list)) { /* X is found; delete it */
tmp = p->next;
p->next = tmp->next; /* Bypass deleted cell */
free(tmp);
}
}

header A1 A2 X A3

list P
tmp
l i st ADT: D el eteL i st

void DeleteList(List list) {

header A1 A2 X A3

list
l i st ADT: D el eteL i st

void DeleteList(List list) {


Position p;

p = list->next; /* Header assumed */


list->next = NULL;
while (p != NULL) {
free(p);
p = p->next;
} Is it okay?
}

header A1 A2 X A3

list
l i st ADT: D el eteL i st

void DeleteList(List list) { void DeleteList(List list) {


Position p; Position p, tmp;

p = list->next; p = list->next;
list->next = NULL; list->next = NULL;
while (p != NULL) { while (p != NULL) {
free(p); tmp = p->next;
p = p->next; free(p);
} p = tmp;
} }
}

header A1 A2 X A3

list
D o u b l y L i n ked L i st

a list that contains links to next and previous nodes

struct Node {
ElementType element;
Position next;
Position prev;
};

FindPrevious() in singly linked list is not necessary

list
D o u b l y L i n ked L i st : i n s ert
D o u b l y L i n ked L i st : i n s ert

P
x

tmp

void Insert(ElementType x, List list, Position p) {


Position tmp;

tmp = (Position) malloc(sizeof(struct Node)); struct Node {


if (tmp == NULL) { ElementType element;
FatalError(“Out of space!!!”); Position next;
} Position prev;
tmp->element = x; };

}
D o u b l y L i n ked L i st : i n s ert

P
x

tmp

void Insert(ElementType x, List list, Position p) {


Position tmp;

tmp = (Position) malloc(sizeof(struct Node)); struct Node {


if (tmp == NULL) { ElementType element;
FatalError(“Out of space!!!”); Position next;
} Position prev;
tmp->element = x; };
tmp->next = p->next;
tmp->next->prev = tmp;
}
Doubly Linked List: inser t

P x

tmp

P
x

void Insert(ElementType x, List list, Position p) {


Position tmp;

tmp = (Position) malloc(sizeof(struct Node));


if (tmp == NULL) { struct Node {
FatalError(“Out of space!!!”); ElementType element;
} Position next;
tmp->element = x; Position prev;
tmp->next = p->next; };
tmp->next->prev = tmp;
p->next = tmp;
tmp->prev = p;
}
D o u b l y L i n ked L i st : d el ete

p
D o u b l y L i n ked L i st : d el ete

void Delete(ElementType x, List list) {


Position p;
struct Node {
p = Find(x, list);
ElementType element;
Position next;
p->prev->next = p->next;
Position prev;
};
}
D o u b l y L i n ked L i st : d el ete

void Delete(ElementType x, List list) {


Position p;
struct Node {
p = Find(x, list);
ElementType element;
Position next;
p->prev->next = p->next;
Position prev;
p->next->prev = p->prev;
};
}
Doubly Linked List: delete

void Delete(ElementType x, List list) {


Position p;
struct Node {
p = Find(x, list);
ElementType element;
Position next;
p->prev->next = p->next;
Position prev;
p->next->prev = p->prev;
};
free(p);
}
C i rc u l a rl y L i n ked L i st

Doubly Linked List

Circularly Linked List

Header?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy