0% found this document useful (0 votes)
4 views66 pages

03_linkedList

The document provides a detailed overview of the List Abstract Data Type (ADT), including its specifications, operations, and implementations using both arrays and linked lists. It discusses various operations such as insertion, deletion, and finding elements, along with their complexities and inefficiencies in array implementations. Additionally, it includes code snippets for creating and managing linked lists in C, demonstrating the structure and functions associated with the List ADT.

Uploaded by

Ahn Gabriel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views66 pages

03_linkedList

The document provides a detailed overview of the List Abstract Data Type (ADT), including its specifications, operations, and implementations using both arrays and linked lists. It discusses various operations such as insertion, deletion, and finding elements, along with their complexities and inefficiencies in array implementations. Additionally, it includes code snippets for creating and managing linked lists in C, demonstrating the structure and functions associated with the List ADT.

Uploaded by

Ahn Gabriel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

Discrete Mathematics: Logic

Data Structure:
List
L i s t A D T: s p e c i f i c a t i o n

An ordered sequence of elements <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 and Ai-1 precedes Ai
the position of an element Ai in a list is I (1 ≤ i ≤ N)

Operations in the List ADT


MakeEmpty (): constructor, returns List
DeleteList (List L): destructor
Find (List L, Key K): returns the position of K
Insert (Key K, List L, Position P): insert K after P in L
Delete (Key K, List L): delete K from L
Concat (List L1, List L2): returns the concatenation of L1 and L2
L i s t A D T: a n e x a m p 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 position of K


Insert (Key K, List L, Position P): insert K after P in L
Delete (Key K, List L): delete K from L
L i s t A D T: simple implementation with array

Insert(‘X’, L, 3) Insert X after the position 3 in the list L

List L 34 12 52 16 12
L i s t A D T: simple implementation with array

Insert(‘X’, L, 3) Insert X after the position 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 s t A D T: simple implementation with array

It is inef cient because . . .


an estimate of the maximum size of the list is required
it requires overestimating 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)
fi
L i s t A D T: simple implementation with array

It is inef cient because . . .

The maximum size of the list needs to be estimated


It requires overestimating 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)
fi
L i s t A D T: implementation with linked list

A linked list consists of a series of structures that 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 s t A D T: linked list

A1

1000
A1 0
L i s t A D T: linked list

A1 A2

1000 800
A1 800 A2 0
L i s t A D T: linked list

A1 A2 A3

1000 800 712


A1 800 A2 712 A3 0
L i s t A D T: linked list

A1 A2 A3 A4

1000 992 800 712


A1 800 A4 0 A2 712 A3 992
L i s t A D T: linked list

A1 A2 A3 A4 A5

1000 992 800 712 692

A1 800 A4 692 A2 712 A3 992 A5 0


L i s t A D T: linked list

A1 A2 A3 A4 A5

1000 992 800 712 692

A1 800 A4 692 A2 712 A3 992 A5 0


L i s t A D T: representation of linked list

10 20
L i s t A D T: representation of linked list

typedef struct listNode *listPointer;


typedef struct listNode {
10 20 int data;
listPointer link;
} node;
listPointer create2(){
listPointer rst, second;
MALLOC( rst, sizeof(* rst));
MALLOC(second, sizeof(*second));
second ->link = NULL;
second -> data = 20;
rst -> data = 10;
rst->link = second;
return rst;
}
fi
fi
fi
fi
fi
fi
L i s t A D T: representation of linked list
second
typedef struct listNode *listPointer;
typedef struct listNode {
rst 10 20 int data;
listPointer link;
} node;
Int main() {
listPointer rst, second;
rst = (node*)malloc(sizeof(node));
second = (node*)malloc(sizeof(node));
second ->link = NULL;
second -> data = 20;
rst -> data = 10;
rst->link = second;
free( rst);
free(second);
return 0;
}
fi
fi
fi
fi
fi
fi
L i s t A D T: insertion

A1 A2 A3 A4 A5

MakeEmpty (): constructor, returns List


DeleteList (List L): destructor
Find (List L, Key K): returns the position of K
Insert (Key K, List L, Position P): insert K after P in L
Delete (Key K, List L): delete K from L
Concat (List L1, List L2): returns the concatenation of L1 and L2
L i s t A D T: insertion

A1 A2 A3 A4 A5

L p

Insert (Key K, List L, Position P)


Insert(‘X’, L, P)
: insert K after P in L
L i s t A D T: insertion

A1 A2 A3 A4 A5

L p

Insert (Key K, List L, Position P)


Insert(‘X’, L, P)
: insert K after P in L

A1 A2 A3 A4 A5

p
L
X
L i s t A D T: deletion
A1 A2 A3 A4 A5

p
L
Delete(A3, L)

A1 A2 A3 A4 A5

L p

A1 A2 A4 A5

L
l i s t A D T: type declaration for a linked list

typedef struct Node* PtrToNode;


typedef PtrToNode Position;
typedef PtrToNode List;
typedef int ElementType;
struct Node
{
ElementType Element;
Position Next;
};

List MakeEmpty();
int IsEmpty( List L );
int IsLast( Position P, List L );
Position Find( ElementType X, List L );
Position FindPrevious ( ElementType X, List L );
void Delete( ElementType X, List L );
void Insert ( ElementType X, List L, Position P );
void DeleteList ( List L );
l i s t A D T: M a ke E m p t y

/* create header node */ typedef struct Node* PtrToNode;


typedef PtrToNode Position;
List MakeEmpty( ) typedef PtrToNode List;
{ typedef int ElementType
List L;
L = (List)malloc(sizeof(struct Node)); struct Node
L->element = header; {
L->next = NULL; ElementType Element;
return L; Position Next;
} };

header

L
l i s t A D T: I s E m p t y

/* return true if L is empty */


struct Node
{
int IsEmpty( List L )
ElementType Element;
{
//What do you want to check? Position Next;
};
}

header true →1

false →0
header 6

L
l i s t A D T: I s E m p t y

/* return true if L is empty */


struct Node
{
int IsEmpty( List L )
ElementType Element;
{
Position Next;
return L->Next == NULL;
};
}

header true

false
header 6

L
l i s t A D T: I s L a s t

/* return true if P is the last position in list L */


struct Node
{
int IsLast( Position P, List L )
ElementType Element;
{
Position Next;
//What do you want to check?
};
}

header 6 true

L
p

header 6 7 false

L p
l i s t A D T: I s L a s t

/* return true if P is the last position in list L */


struct Node
{
int IsLast( Position P, List L )
ElementType Element;
{
Position Next;
return P->Next == NULL;
};
}

header 6 true

L
p

header 6 7 false

L p
l i s t A D T: F i n d
/* return position of X in L; NULL if not found */

Position Find( ElementType X, List L ) struct Node


{ {
ElementType Element;
Position Next;
};

header A1 X A2 A3

L
l i s t A D T: F i n d
/* return position of X in L; NULL if not found */

Position Find( ElementType X, List L )


{
Position P; //temporary variable used to find X

P = L->Next; //points the first element

return P;
}

header A1 X A2 A3

L p
l i s t A D T: F i n d
/* return position of X in L; NULL if not found */

Position Find( ElementType X, List L ) struct Node


{ {
Position P; ElementType Element;
Position Next;
P = L->Next; };
while( P != NULL && P->Element != X )
P = P->Next;

return P;
}

header A1 X A2 A3

L p p
l i s t A D T: F i n d
/* return position of X in L; NULL if not found */

Position Find( ElementType X, List L ) struct Node


{ {
Position P; ElementType Element;
Position Next;
P = L->Next; };
while( P != NULL && P->Element != X )
P = P->Next;

return P;
}

Stop condition

header header A1 A2

L p L p

header A1 X A2 A3

L p
l i s t A D T: F i n d P rev i o u s

Position FindPrevious( ElementType X, List L ) struct Node


{ {
Position P; ElementType Element;
Position Next;
P = L; };
while( P->Next != NULL && )
P = P->Next;
return P;
}

Stop condition

header header A1 A2

L L P
P
l i s t A D T: F i n d P rev i o u s

Position FindPrevious( ElementType X, List L ) struct Node


{ {
Position P; ElementType Element;
Position Next;
P = L; };
while( P->Next != NULL && P->Next->Element != X )
P = P->Next;
return P;
}
P->Next

header A1 A2 X A3

L P

header A1 A2 X A3

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

void Insert( ElementType X, List L, Position P ) struct Node


{ {
ElementType Element;
Position Next;
};

A1 A2 A3 A4 A5

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

void Insert( ElementType X, List L, Position P )


{ struct Node
Position TmpCell; {
ElementType Element;
TmpCell = malloc( sizeof( struct Node ) ); Position Next;
if ( TmpCell == NULL ) };
FatalError( “Out of space!!!” );

TmpCell->Element = X;

A1 A2 A3 A4 A5

L P
X

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

void Insert( ElementType X, List L, Position P )


{ struct Node
Position TmpCell; {
ElementType Element;
TmpCell = malloc( sizeof( struct Node ) ); Position Next;
if ( TmpCell == NULL ) };
FatalError( “Out of space!!!” );

TmpCell->Element = X;
TmpCell->Next = P->Next;

A1 A2 A3 A4 A5

L P
X

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

void Insert( ElementType X, List L, Position P )


{ struct Node
Position TmpCell; {
ElementType Element;
TmpCell = malloc( sizeof( struct Node ) ); Position Next;
if ( TmpCell == NULL ) };
FatalError( “Out of space!!!” );

TmpCell->Element = X;
TmpCell->Next = P->Next;
P->Next = TmpCell;
}

A1 A2 A3 A4 A5

L P
X

TmpCell
l i s t A D T: D e l e t e

void Delete( ElementType X, List L )


{

header A1 A2 X A3

L
l i s t A D T: D e l e t e

void Delete( ElementType X, List L )


{
Position P, TmpCell;

P = FindPrevious( X, L );

header A1 A2 X A3

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

void Delete( ElementType X, List L )


{
Position P, TmpCell;

P = FindPrevious( X, L );
if ( !IsLast( P, L ) ) /* Assumption of header use */
{ /* X is found; delete it */

P->Next = ???; /* Bypass deleted cell */

}
}

header A1 A2 X A3

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

void Delete( ElementType X, List L )


{
Position P, TmpCell;

P = FindPrevious( X, L );
if ( !IsLast( P, L ) ) /* Assumption of header use */
{ /* X is found; delete it */
TmpCell = P->Next;

}
}

header A1 A2 X A3

L P
TmpCell
l i s t A D T: D e l e t e

void Delete( ElementType X, List L )


{
Position P, TmpCell;

P = FindPrevious( X, L );
if ( !IsLast( P, L ) ) /* Assumption of header use */
{ /* X is found; delete it */
TmpCell = P->Next;
P->Next = TmpCell->Next; /* Bypass deleted cell */

}
}

header A1 A2 X A3

L P
TmpCell
l i s t A D T: D e l e t e

void Delete( ElementType X, List L )


{
Position P, TmpCell;

P = FindPrevious( X, L );
if ( !IsLast( P, L ) ) /* Assumption of header use */
{ /* X is found; delete it */
TmpCell = P->Next;
P->Next = TmpCell->Next; /* Bypass deleted cell */
free( TmpCell );
}
}

header A1 A2 A3

L P
TmpCell
l i s t A D T: D e l e t e

void Delete( ElementType X, List L )


{
Position P, TmpCell;

P = FindPrevious( X, L );
if ( !IsLast( P, L ) ) /* Assumption of header use */
{ /* X is found; delete it */
TmpCell = P->Next;
P->Next = TmpCell->Next; /* Bypass deleted cell */
free( TmpCell );
}
}

header A1 A2 X ???
L P
l i s t A D T: D e l e t e

void Delete( ElementType X, List L )


{
Position P, TmpCell;

P = FindPrevious( X, L );
if ( !IsLast( P, L ) ) /* Assumption of header use */
{ /* X is found; delete it */
TmpCell = P->Next;
P->Next = TmpCell->Next; /* Bypass deleted cell */
free( TmpCell );
}
}

header A1 A2 X

L P
TmpCell
l i s t A D T: D e l e t e

void Delete( ElementType X, List L )


{
Position P, TmpCell;

P = FindPrevious( X, L );
if ( !IsLast( P, L ) ) /* Assumption of header use */
{ /* X is found; delete it */
TmpCell = P->Next;
P->Next = TmpCell->Next; /* Bypass deleted cell */
free( TmpCell );
}
}

What does FindPrevious(X, L) return?


l i s t A D T: F i n d P rev i o u s Review

Position FindPrevious( ElementType X, List L ) struct Node


{ {
Position P; ElementType Element;
Position Next;
P = L; };
while( P->Next != NULL && P->Next->Element != X )
P = P->Next;
return P;
}

Stop condition

header header A1 A2

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

void Delete( ElementType X, List L )


{
Position P, TmpCell;

P = FindPrevious( X, L );
if ( !IsLast( P, L ) ) /* Assumption of header use */
{ /* X is found; delete it */
TmpCell = P->Next;
P->Next = TmpCell->Next; /* Bypass deleted cell */
free( TmpCell );
}
}

header A1 A2

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

void DeleteList( List L )


{

header A1 A2 X A3

L
l i s t A D T: D e l e t e L i s t

void DeleteList( List L )


{
Position P;

P = L->Next; /* Header assumed */


L->Next = NULL;
while( P != NULL )
{
free( P );
P = P->Next; Is it okay?
}
}

header A1 A2 X A3

L
l i s t A D T: D e l e t e L i s t

void DeleteList( List L )


{
Position P;

P = L->Next; /* Header assumed */


L->Next = NULL;
while( P != NULL )
{
free( P );
P = P->Next; Is it okay?
}
}

header A1 A2 X A3

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

void DeleteList( List L )


{
Position P;

P = L->Next; /* Header assumed */


L->Next = NULL;
while( P != NULL )
{
free( P );
P = P->Next; Is it okay?
}
}

header A2 X A3

L P
l i s t A D T: D e l e t e L i s t
void DeleteList( List L ) void DeleteList( List L )
{ {
Position P; Position P, Tmp;

P = L->Next; /* Header assumed */ P = L->Next; /* Header assumed */


L->Next = NULL; L->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

L P

header A1 A2 X A3

L P Tmp
header A2 X A3

L P
Review
l i s t A D T: F i n d P rev i o u s

Position FindPrevious( ElementType X, List L ) struct Node


{ {
Position P; ElementType Element;
Position Next;
P = L; };
while( P->Next != NULL && P->Next->Element != X )
P = P->Next;
return P;
}

header

header A1 A2 X A3

L P
Doubly Linked List

A list that contains links to the next and previous nodes

struct Node
{
ElementType Element;
Position Next;
Position Prev;
};

FindPrevious() in singly linked list is not necessary in the doubly linked list

L
Doubly Linked List: inser t
Doubly Linked List: inser t

P
x

TmpCell

void Insert( ElementType X, List L, Position P ) struct Node


{ {
Position TmpCell; ElementType Element;
Position Next;
TmpCell = malloc( sizeof( struct Node ) ); Position Prev;
if ( TmpCell == NULL ) };
FatalError( “Out of space!!!” );

TmpCell->Element = X;

}
Doubly Linked List: inser t

P
x

TmpCell

void Insert( ElementType X, List L, Position P ) struct Node


{ {
Position TmpCell; ElementType Element;
Position Next;
TmpCell = malloc( sizeof( struct Node ) ); Position Prev;
if ( TmpCell == NULL ) };
FatalError( “Out of space!!!” );

TmpCell->Element = X;
TmpCell->Next = P->Next;
TmpCell->Next->Prev = TmpCell;
}
Doubly Linked List: inser t

P
x

TmpCell

void Insert( ElementType X, List L, Position P ) struct Node


{ {
Position TmpCell; ElementType Element;
Position Next;
TmpCell = malloc( sizeof( struct Node ) ); Position Prev;
if ( TmpCell == NULL ) };
FatalError( “Out of space!!!” );

TmpCell->Element = X;
TmpCell->Next = P->Next;
TmpCell->Next->Prev = TmpCell;

}
Doubly Linked List: inser t

P
x

TmpCell

P
x
TmpCell
void Insert( ElementType X, List L, Position P ) struct Node
{ {
Position TmpCell; ElementType Element;
Position Next;
TmpCell = malloc( sizeof( struct Node ) ); Position Prev;
if ( TmpCell == NULL ) };
FatalError( “Out of space!!!” );

TmpCell->Element = X;
TmpCell->Next = P->Next;
TmpCell->Next->Prev = TmpCell;
P->Next = TmpCell;
TmpCell->Prev = P
}
Doubly Linked List: delete
Doubly Linked List: delete

p
P->Previous P->Next

void Delete( ElementType X, List L)


{
Position P;
P = Find(X, L);

P->Prev->Next = P->Next;

}
Doubly Linked List: delete

p
P->Previous P->Next

void Delete( ElementType X, List L)


{
Position P;
P = Find(X, L);

P->Prev->Next = P->Next;
P->Next->Prev = P->prev;

}
Doubly Linked List: delete

void Delete( ElementType X, List L)


{
Position P;
P = Find(X, L);

P->Prev->Next = P->Next;
P->Next->Prev = P->prev;
free(P);
}
Circularly Linked List

Doubly Linked List


Circularly Linked List

Doubly Linked List

Circularly Linked List

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