1 / 22

Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue

Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue. Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University. ADT (Abstract Data Type). ADT

ricky
Download Presentation

Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures (Second Part)Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

  2. ADT (Abstract Data Type) • ADT • specification of a set of data and the set of operations that can be performed on the data • ADT is abstract : independent of concrete implementations • can be thought as an interface (hidden from implementation) • In OOP, ADT is a class. Instance of ADT is an object. • C style ADT long stack_create(); /* create new instance of a stack */ void stack_push(long stack, void *item); /* push an item on the stack */ void *stack_pop(long stack); /* get item from top of stack */ void stack_delete(long stack); /* delete the stack */ long stack; struct foo *f; stack = stack_create(); /* create a stack */ stack_push(stack, f); /* add foo structure to stack */ f = stack_pop(stack); /* get top structure from stack */

  3. Array ADT • Array is a consecutive set of memory locations. • Array ADT is a more general structure Structure Array is objects : A set of pairs < index; value > where for each value from the set item. Index is a finite ordered set of one or more dimensions functions : for all A ∈ Array; i ∈ index; x ∈ item; j; size 2 integer Array Create(j, list) ::= Array Retrieve(A, i) ::= Array Store(A, i, x) ::= end Array

  4. Arrays in C int list[5],*plist[5] • 5 pointers to integersfrom plist[0] to plist[4] • 5 integers from list[0] to list[4] • Implementation • variable Memory Address • list[0]  (= base address) • list[1]  + sizeof(int) • list[2]  + 2 • sizeof(int) • list[3]  + 3 • sizeof(int) • list[4]  + 4 • sizeof(int)

  5. Structure • collections of data of the different types typedef struct PERSON { char name[10]; int age; float salary; } PERSON person;

  6. Using Pointers • Set all pointers to NULL when they are not actually pointing to an object p = NULL *p NULL? A value in location zero? • Explicit type casting pi = malloc(sizeof (int)); pf = (float *) pi;

  7. Heap • malloc • free • Garbage int *pi; pi = (int *)mlloc (sizeof (int)); *pi = 1; pi = (int *) malloc ( sizeof (int)); *pi = 2; pi 1 garbage 2

  8. Garbage • Automatic garbage collection • Memory Leak • Dangling Pointer

  9. Singly Linked Lists ptr a1 a2 an • typedef struct list_node *list_pointer; typedef struct list_node { char data[data]; list_pointer link; }; list_pointer ptr = NULL; /* create a null list */

  10. Necessary capabilities for linked representations. • A mechanism for defining a node’s structure : self-referential structure • A way to create new nodes : malloc • A way to remove nodes :free

  11. ptr ptr • Create a new node ptr = (list_pointer)malloc(sizeof(struct list_node)); • Assign a value to a field in the node. strcpy (ptr  data, “bat”) ; ptr  link = NULL; (ptr  link)  (*ptr).link In general, e   (*e)

  12. q (3) (2) p x (1) (1) p = (list_pointer) malloc ( sizeof (struct list_node)); p  data = x; (2) p  link = q  link; (3) q  link = p; • Insertion (after node q)

  13. q p (1) (3) (2) (1) p = q  link; (2) q  link = (q  link)  link; (3) free (p); • Deletion (after node q)

  14. Stack • An ordered list in which insertions and deletions are made at one end called top. • LIFO (Last-In-First-Out) • Operations • Create • IsEmpty • IsFull • Push • Pop

  15. Implementation of Stack using Array Stack CreateS ( max stack size ) ::= #define MAX STACK SIZE 100 typedef struct { int key ; /* other fields */ } element ; element stack[MAX STACK SIZE] ; int top = –1; Boolean IsEmpty ( stack ) ::= top  0 Boolean IsFull ( stack ) ::= top  MAX_STACK_SIZE – 1

  16. Stack Add ( stack, item ) ::= void add ( int *top, element item ) /* push */ { if ( *top MAX_STACK_SIZE – 1 ) { stack_full() ; return ; } stack[++*top] := item ; } Element Delete ( stack ) ::= element delete ( int *top ) /* pop */ { if ( *top < 0 ) return stack_empty() ; return stack[(*top) – – ]; }

  17. rear • element • front • element • FIFO ( First-In-First-Out ) Queue • An ordered list in which all insertions take place at one end and all deletions take place at the opposite end. • Q = (a0, • • • , an-1)

  18. Implementation using Array. Queue CreateQ ( max_queue_size ) ::= #define MAX_QUEUE_SIZE 100 typedef struct { int key ; /* other fields */ } element ; element queue[MAX_QUEUE_SIZE] ; int rear = – 1 ; int front = – 1 ; Boolean IsEmptyQ ( queue ) ::= front == rear Boolean IsFullQ ( queue ) ::= rear == MAX_QUEUE_SIZE – 1

  19. Queue AddQ ( queue, item ) ::= void addq ( int *rear, element item ) { if ( *rear = = MAX_QUEUE_SIZE – 1 ) { queue_full() ; return ; } queue[++*rear] = item ; } Element DeleteQ (queue ) ::= element deleteq ( int *front, int rear ) { if ( *front = = rear ) return queue_empty() ; return queue[++*front] ; }

  20. front rear Q[0] Q[1] Q[2] Q[4] comments –1 –1 –1 –1 0 1 1 2 –1 0 1 2 2 2 3 3 J1 J1 J2 J1 J2 J3 J2 J3 J3 J3 J4 J4 empty queue addq J1 addq J2 addq J3 delq J1 delq J2 delq J4 delq J3 The queue gradually shifts to the right *A problem in the above implementation

  21. Circular queueimplementation • Regard the array as circular. Initially front = rear = 0 • A circular queue is empty if front == rear before deletion • A circular queue is full if front == rear after insertion • A circular queue holds at most MAX_QUEUE_SIZE - 1 elements

  22. if ( *rear == MAX_QUEUE_SIZE ) • *rear == 0; • else • (*rear)++ ; void addq ( int front, int *rear, element item ) { • if (front == *rear) { • queue_full() ; return; } • queue[*rear] = item ; • } • element deleteq ( int *front, int rear) • { • if ( *front == rear ) • return queue_empty () ; • *front = ( *front + 1) % MAX_QUEUE_SIZE ; • return queue[*front] ; • } • *rear = (*rear+1) % • MAX_QUEUE_SIZE

More Related