Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Data Structure

Similar presentations


Presentation on theme: "Introduction to Data Structure"— Presentation transcript:

1 Introduction to Data Structure
CHAPTER 4 LINKED LISTS 4.1 Pointers 4.2 Singly Linked Lists 4.3 Dynamic Linked Stacks and Queues Polynomials 4.5 Additional List Operations 4.6 Equivalence Relations 4.7 Sparse Matrices 4.8 Doubly Linked Lists

2 Contents Chapter 1 Basic Concepts Chapter 2 Arrays
Chapter 3 Stacks and Queues Chapter 4 Linked Lists Chapter 5 Trees Chapter 6 Graph Chapter 7 Sorting Chapter 8 Hashing Chapter 9 Heap Structures Chapter 10 Search Structures

3 4.1 Pointer Pointer in C int i, *pi; pi = &i; i=10 or *pi=10
pi = malloc(sizeof(int)); /* assign to pi a pointer to int */ int i, *pi; i pi Some integer i pi 10 pi

4 4.2 Singly Linked Lists Ordered Lists Seq. eg.
Non-seq. mapping: Insert “D” Data Link Sequential mapping (not suitable for insertion & deletion) Non-sequential: linked Operations - length - read all - retrieve i-th - store i-th - Insert - delete A C E G X Y insert D A C D E G X Y insert head=1 A C E G X Y D 2 3 4 5 6 7

5 Singly Linked Lists: Delete “G”
Non-seq. mapping: Delete “G” Data Link head=1 A C E G X Y D delete 2 7 4 5 6 3 5 delete G

6 Singly Linked Lists: Arrow Notation
Insert “D” Delete “G” 1 2 A C Y head 6 …… 2 A 3 C E 1 D 7 head G

7 Singly Linked Lists vs. Array
Successive items locate a fixed distance Disadvantages: data movements during insertion and deletion Possible solution Linked list More Examples: Figure 4.1 vs. Figure 4.2 (p.140) Figure 4.3 Figure 4.4

8 Defining a Linked List Node in C
To represent a single node in C To define the structure of a node, we need to know the type of each of its field Example: ThreeLetterNode -- Class definition data link Structure data type: array or char or … pointer ThreeLetterNode typedef struct list_node *list_pointer; typedef struct list_node { char data[3]; list_pointer link; }; data link Some ThreeLetterNode data[0] data[1] data[2]

9 Example: A complex List Structure
The definition of Node A The definition of Node B typedef struct list_nodea *nodea; typedef struct list_nodea { int data1; char data2; float data3; nodea linka; nodeb linkb; }; typedef struct list_nodeb *nodeb; typedef struct list_nodeb { int data; nodeb linkb; }; 55 ‘c’ 3.14 22 A possible configuration list_nodeb list_nodea

10 Example: A complex List Structure (cont.)
The definition of Node A The definition of Node B typedef struct list_nodea *nodea; typedef struct list_nodea { int data1; char data2; float data3; nodea linka; nodeb linkb; }; typedef struct list_nodeb *nodeb; typedef struct list_nodeb { int data; nodeb linkb; }; 55 ‘c’ 3.14 list_nodea 22 23 Another possible configuration list_nodeb

11 Creating a New Node in C Assume the following node structure
typedef struct list_node *list_pointer; typedef struct list_node { char data[3]; list_pointer link; }; Invoke malloc to obtain a storage to hold the node list_pointer ptr = NULL; ptr = (list_pointer) malloc(sizeof(list_node)); ptr NULL ptr

12 Freeing a Node in C Assume the following node structure
typedef struct list_node *list_pointer; typedef struct list_node { char data[3]; list_pointer link; }; Invoke function free to return the node’s storage free(ptr); ptr

13 Example List Manipulation Operations in C
Assume we have classes: ListNode and List Some example list manipulation operations: create2( ) // Create two nodes insert( ) delete( ) Inverting a list concatenating two lists typedef struct list_node *list_pointer; typedef struct list_node { int data; list_pointer link; }; list_pointer ptr = NULL; List ptr list_node

14 Example: Create a Two-node List
list_pointer create2( ) { list_pointer first, second; first = (list_pointer)malloc(sizeof(list_node)); second = (list_pointer)malloc(sizeof(list_node)); second->link = NULL; second->data = 20; first->data = 10; first->link = second; return first; } p.143 first 20 NL NULL first 20

15 Example: Inserting a Node
void insert(list_pointer *ptr, list_pointer node) { list_pointer temp; temp = (list_pointer)malloc(sizeof(list_node)); /* create a new node */ if (IS_FULL(temp)) { fprintf(stder, “The memory is full\n”); exit(1); } temp->data = 50; if (*ptr) { temp->link = node->link; node->link = temp; } else { /* list ptr is empty */ temp->link = NULL; *ptr = temp; } } p.144 /* insert a new node with data = 50 into list ptr after node */ 50 NULL temp ptr node NULL temp ptr . . .

16 Example: Deleting a Node
void delete(list_pointer *ptr, list_pointer trail, list_pointer node) { if (trail) trail->link = node->link; else *ptr = (*ptr)->link; free(node); /* return the node to the system */ } /* delete the node after trail */ p.145 20 NULL ptr node trail How about if trail = NULL?

17 Delete a Node: A Dangling Reference
trail ptr BAT  CAT  MAT  SAT  VAT NULL dangling reference

18 4.3 Linked Stacks and Queues
Stacks and Queues in Linked lists NULL    top element link (a) Linked Stack front (b) Linked Queue rear F R Note: Compare with sequence queue

19 Linked m Stacks and m Queues
Several stacks and queues co-exist: Sequence – not easy to handle “insert” and “delete” Linked – a good choice Representing n stacks Initial condidtion: top[i] = NULL, 0 ≤ i < MAX_STACKS Boundary conditions: top[i] = NULL iff ith stack is empty IS_FULL(temp) iff the memory is full #define MAX_STACKS 10 typedef struct { int key; /* other fields */ } element; typedef struct stack *stack_pointer; typedef struct stack { element item; stack_pointer link; }; stack_pointer top[MAX_STACKS]; top top i.e. NULL = 0  null value

20 Linked m Stacks and m Queues (cont.)
Representing m queues #define MAX_QUEUES 10 typedef struct queue *queue_pointer; typedef struct queue { element item; queue_pointer link; }; queue_pointer front[MAX_QUEUES], rear[MAX_QUEUES]; Initial condidtion: front[i] = NULL, 0 ≤ i < MAX_QUEUES Boundary conditions: front[i] = NULL iff ith queue is empty IS_FULL(temp) iff the memory is full front rear i.e. NULL = 0  null value

21 Linked Stacks: Operations
Add to a linked stack void add(stack_pointer *top, element item) { stack_pointer temp = (stack_pointer) malloc(sizeof (stack)); if (IS_FULL(temp)) { fprintf(stderr, “The memory is full\n”); exit(1); } temp->item = item; temp->link = *top; p.149 top temp

22 Linked Stacks: Operations (cont.)
Delete from a linked stack element delete(stack_pointer *top) { stack_pointer temp = *top; element item; if (IS_EMPTY(temp)) { fprintf(stderr, “The memory is empty\n”); exit(1); } item = temp->item; *top = temp->link; free(temp); return item; p.149 temp top

23 Linked Queues: Operations
Add into a linked queue void addq(queue_pointer *front, queue_pointer *rear, element item) { queue_pointer temp = (queue_pointer) malloc(sizeof (queue)); if (IS_FULL(temp)) { fprintf(stderr, “The memory is empty\n”); exit(1); } temp->item = item; temp->link = NULL; if (*front) (*rear)->link = temp; else *front = temp; *rear = temp; temp front rear p.151

24 Linked Queues: Operations (cont.)
Delete from a linked queue element deleteq(queue_pointer *front) { queue_pointer temp = *front; element item; if (IS_EMPTY(temp)) { fprintf(stderr, “The memory is empty\n”); exit(1); } item = temp->item; *front = temp->link; free(temp); return item; p.151 temp front rear

25 4.4 Polynomials Consider a polynomial
Tackle the reasonable-sized problem Node structure e.g. A = 3x14 + 2x8 + 1 Note: in Array term: represented by a node coef exp link A 3 14 2 8 1 ^ ^ 3 2 …… 1

26 Polynomial Representation
Type declaration typedef struct poly_node *poly_pointer; typedef struct poly_node { int coef; int expon; poly_pointer link; }; poly_pointer a, b, d; p.152 coef exp link poly

27 Polynomial Representation: Example
Examples a null -3 10 b null

28 Adding Polynomials : d = a + b
Adding Polynomials: Figure 4:12 d = a + b Case 1: a->exp = b->exp a 1 0 a b -3 10 b d 11 14 d

29 Adding Polynomials : d = a + b (cont.)
Case 2: a->exp < b->exp 1 0 a b 11 14 -3 10 d d

30 Adding Polynomials: d = a + b (cont.)
Case 3: a->exp > b->exp a b d

31 Algorithm for adding two polynomials
Adding Polynomials: Algorithm Algorithm for adding two polynomials (C in Program 4.10, p.154) // copy rest of a // copy rest of b

32 Adding Polynomials: Complexity Analysis
(1) Coefficient additions 0  additions  min(m, n) where m (n) denotes the number of terms in A (B). (2) Exponent comparisons extreme case em-1 > fm-1 > em-2 > fm-2 > … > e0 > f0 m+n-1 comparisons (3) Creation of new nodes m + n new nodes Summary: O(m+n), where m (n) denotes the number of terms in A (B).

33 Consider a Polynomial expression:
Erasing Polynomials Consider a Polynomial expression: e (x) = a (x) * b (x) + d (x) polynomial a, b, d, e; a = read_poly(); // read and create polynomial a b = read_poly(); // read and create polynomial b d = read_poly(); // read and create polynomial c temp = pmult(a, b); e = padd(temp, d); print_poly(e) ; Before and after the above procedure been executed Free all nodes?

34 Erasing Polynomials (cont.)
C program void earse(poly_pointer *ptr) { /* erase the polynomial pointed to by ptr */ poly_pointer temp; while (*ptr) { temp = *ptr; *ptr = (*ptr)->link; free(temp); } Erasing complexity: O(n), n: nonzero terms not efficient!! ptr null temp

35 Circular List Representation
Representing Polynomials as Circularly Linked Lists a The operation for erasing polynomial would be very efficient by maintaining our own list of freed node, i.e., free pool (see next slide)

36 Circular List Representation (cont.)
Concept of polynomial erasing with free storage pool ptr 14 3 2 8 1 avail temp 1. temp = ptr->link; 2. ptr->link = avail; 3. avail = temp; p.159 Erasing complexity: Circularly List: O(1) Chain List: O(n), n: nonzero terms

37 STORAGE POOL The STORAGE POOL (available pool/space, free space pool)
the set of all node, which can be allocated a free space available for a new node Method Method 2 AV n storage pool AV

38 STORAGE POOL: Array Method
Method 1: Array Method 1 2 n storage pool AV procedure GETNODE(I) if AV > n then call NO_MORE_NODE; I  AV; AV  AV + 1; end GETNODE

39 STORAGE POOL: Linked Method
Method 2: Linked Method the set of all nodes not currently in use is linked together. Procedure GETNODE(x) if AV=0 then call NO_MORE_NODE; x  AV; AV  LINK(AV); end procedure RET(x) LINK(x)  AV; AV  x; end AV AV X

40 Circular List Representation with Head
Problem with circular linked lists: How to represent zero polynomial? Solution: introducing an additional Head Node (1) Zero (need a head node) a -1 Zero polynomial (2) Non-zero Polynomial a exp -1

41 Circular with Head: d = a + b
exp -1 b exp -1 d=a+b exp -1 1 0 See p. 161, Program 4.16

42 Algorithm for adding two polynomials
Adding Polynomials: Algorithm Comparison Algorithm for adding two polynomials Chain Data Structure Circularly with Head Node (C in Program 4.10, p.154) (C in Program 4.16, p.161) // copy rest of a // copy rest of b ??? when ???

43 4.5 Additional List Operations
Linked Lists Operations create insert delete get_node ret_node invert concatenate

44 Chain Linked List: Invert
Inverting a Chain first 1 2 3 7 NULL first 7 6 5 1 NULL list_pointer invert(list_pointer lead) { list_pointer middle, trail; middle = NULL; while (lead) { trail = middle; middle = lead; lead = lead->link; /* lead moves to next node */ middle->link = trail; /* link middle to preceding node */ } return middle; } p.164, Program 4.17

45 Chain Linked List: Invert (cont.)
first 1 2 3 7 NULL lead middle lead = first; middle = NULL; first 1 2 3 7 NULL middle lead trail = middle; middle = lead trail NULL first 1 2 3 7 NULL middle lead lead = lead->link; middle->link = trail; trail NULL

46 Chain Linked List: Invert (cont.)
trail first 1 2 3 7 NULL lead middle lead = lead->link; middle->link = trail; trail = middle; middle = lead

47 Chain Linked List: Invert (cont.)
first 1 2 3 7 NULL trail middle lead lead = lead->link; middle->link = trail; NULL . . . first 7 6 5 1 NULL middle first = middle; lead NULL

48 Chain Linked List: Concatenate
. . . this 1 2 3 7 NULL p . . . b 11 12 13 17 NULL this 1 2 3 7 p b 11 12 13 17 NULL . . .

49 Circular Linked Lists: Adding New Node
Adding new node to front or rear Problem: move down the whole list when add an item to the front. first 1 2 3 7 . . . first 1 2 3 7 . . . New x first 1 2 3 7 . . . New x rear front

50 Circular Lists: Adding New Node (cont.)
Circular lists: with pointer to last node . . . 1 2 3 7 last 1 2 3 7 . . . last New x Add to front x->link = last->link; last->link = x; Add to rear ???

51 Circular Linked Lists (cont.)
Inserting at the front [Program 4.19, p.165] void insert_front(list_pointer *ptr, list_pointer node) { if (IS_EMPTY(*ptr) { /* empty List */ *ptr = node ; node->link = node ; } else { node->link = (*ptr)->link ; (*ptr)->link = node ; } } ptr node . . . 1 2 3 7 ptr node . . . 1 2 3 7 ptr

52 Linked List for Sparse Matrix
Circular linked list representation of a sparse matrix has two types of nodes: head node: tag, down, right, and next entry node: tag, down, row, col, right, value Head node i is the head node for both row i and column i. down tag right down tag row col right f i j aij next value Head node Entry node Setup for aij A 4x4 sparse matrix

53 Linked List for Sparse Matrix (cont.)
4 2 1 3 -4 12 11 -15 H0 H1 H2 H3 Matrix head

54 Doubly Linked List Motivation: Problem of singly linked lists
To efficiently delete a node, we need to know its preceding node It’s hard to find the node precedes a node ptr. Solution: using doubly linked list Each node in a doubly linked list has at least three fields left link field (llink) data field (item) right link field (rlink) llink data rlink

55 Doubly Linked List (cont.)
A head node is also used to implement operations more easily. llink item rlink Head Node llink item rlink Empty List

56 Doubly Linked List (cont.)
Deletion from a doubly linked circular list llink item rlink Head Node

57 Doubly Linked List (cont.)
Insertion into a doubly linked circular list node node newnode


Download ppt "Introduction to Data Structure"

Similar presentations


Ads by Google