Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structure #1: The Array

Similar presentations


Presentation on theme: "Data Structure #1: The Array"— Presentation transcript:

1 Data Structure #1: The Array
Data Structures When interrelated information is stored in a computer’s memory, it is sometimes convenient for the programmer (and for the computer’s memory management) to keep this data in a structured format. However, in the computer’s RAM, space for 100 integers has been allocated something like this: Data Structure #1: The Array Address Contents 00 01 : Memory cell 71 (hex 47) > 47 78 48 87 49 74 A9 80 Memory cell 170 (hex AA) > AA 85 FE FF Example int IQlist[100]; Conceptually, the array looks something like this: Index 1 2 98 99 Contents 120 135 116 128 133 CS Chapter 8 – Data Structures

2 Data Structure #2: The MultidimensionalArray
Address Contents 00 01 : Space for element (0,0) > B2 5E (0,1) > B3 59 (0,2) > B4 64 (0,3) > B5 57 (0,4) > B6 5C (1,0) > B7 44 (1,1) > B8 5A (1,2) > B9 54 (1,3) > BA 4E (1,4) > BB 56 (2,0) > BC 4D (2,1) > BD 5F (2,2) > BE 61 (2,3) > BF (2,4) > C0 58 FF Example int GradeTable[3][5]; Conceptually, the array looks something like this: COLUMN # 1 2 3 4 ROW # 94 89 100 87 92 68 90 84 78 86 77 95 97 88 However, in the computer’s RAM, space for 15 integers has been allocated something like this: CS Chapter 8 – Data Structures

3 Data Structure #3: The Linked List
However, in the computer’s RAM, space for 4 integers has been allocated something like this: Example struct node; typedef node *nodePtr; struct node { int value; nodePtr next; }; nodePtr List; Address Contents 00 : 16 64 3rd item is at address B0 17 B0 4E 5E FF signifies the end of List 4F FF List is located at 9A 9A 61 2nd item is at address 16 9B 58 4th item is at address 4E B1 Conceptually, the linked list looks something like this: 97 100 88 94 CS Chapter 8 – Data Structures

4 Relative Advantages of Arrays and Linked Lists
Require contiguous memory Dynamically locate memory Requires specific size Has flexible size Potentially wastes memory Only uses allocated space Potentially runs out of memory Expands memory as needed Insertion requires rearranging Insertion requires slight relink Deletion requires rearranging Deletion requires slight relink Indexing facilitates searching One-by-one searching required Binary search possible if sorted Sequential search only Straightforward to program Tougher to conceptualize Memory easily cleared after use Complicated garbage collection CS Chapter 8 – Data Structures

5 Comparison: Retrieving a List from a File Chapter 8 – Data Structures
Using an array Using a linked list void GetList(int List[50], int &ListSize) { ifstream file; char fileName[50]; int value; cout << "Enter the name of the file: "; cin >> fileName; file.open(fileName); assert(!file.fail()); ListSize = 0; file >> value; while ((!file.eof()) && (ListSize < 50)) List[ListSize] = value; ListSize++; } file.close(); void GetList(nodePtr &List) { ifstream file; char fileName[50]; int val; nodePtr ptr; cout << "Enter the name of the file: "; cin >> fileName; file.open(fileName); assert(!file.fail()); List = NULL; file >> value; while (!file.eof()) ptr = new node; ptr->value = val; ptr->next = List; List = ptr; } file.close(); CS Chapter 8 – Data Structures

6 Comparison: Sequential Search Chapter 8 – Data Structures
Using an array Using a linked list int Search(int List[50], int ListSize, int soughtVal) { int count; bool found = false; count = 0; while ((!found) && (count < 50)) if (List[count] == SoughtVal) found = true; else count++; } if (found) return List[count]; return -1; int Search(nodePtr List, int soughtVal) { nodePtr currPtr; bool found = false; currPtr = List; while ((!found) && (currPtr != NULL)) if (currPtr->value == soughtVal) found = true; else currPtr = currPtr->next; } if (found) return currPtr->value; return -1 Note that the code is almost identical, but the array version is limited to lists of a certain size. If the list is too long, the array can’t hold it all; if it’s too short, several memory slots are wasted. CS Chapter 8 – Data Structures

7 Chapter 8 – Data Structures
The Stack A stack is a data structure that manages a list of similar items in such a way that all insertions and deletions take place at one designated end of the list. In effect, one end of the list is considered the “top” of the stack, inserting into the list is considered “pushing” an item onto the top of the stack, and deleting from the list is considered “popping” off the top of the stack. Example 3 5 8 1 Initial Stack After “Push 3” After “Push 5” After “Push 8” After “Pop” After “Push 1” CS Chapter 8 – Data Structures

8 Comparison: Stack Implementations Chapter 8 – Data Structures
Using an array Using a linked list void Push(int List[50], int &Top, int item) { if (Top < 49) Top++; List[Top] = item; } int Pop(int List[50], int &Top) int val = -1; if (Top >= 0) val = List[Top]; Top--; return val; void Push(nodePtr &List, int item) { nodePtr ptr = new node; ptr->value = item; ptr->next = List; List = ptr; } int Pop(nodePtr &List) int val = -1; if (nodePtr != NULL) val = nodePtr->value; List = List->next; return val; CS Chapter 8 – Data Structures

9 Example Stack Application: TGL Program Control
Main Program Subprogram A() Subprogram B() Subprogram C() x = 0; y = -2; A(); z = 6; cout << x << y << z << endl; i = 10; j = 46; k = 31; B(); j = 50; cout << i << j << k << endl; r = 400; s = 542; C(); r = 710; s = 365; r = 927; cout << r << s << t << endl; u = 15; v = 57; w = 34; cout << u << v << w << endl; When C finishes, the stack is popped and B resumes. Main: line #3 x:0 y:-2 z:? When Main reaches the A(); step A: line #4 i:10 j:46 k:31 Main: line #3 x:0 y:-2 z:? When A reaches the B(); step B: line #3 r:400 s:542 t:? A: line #4 i:10 j:46 k:31 Main: line #3 x:0 y:-2 z:? When B reaches the C(); step When B finishes, the stack is popped and A resumes. When A finishes, the stack is popped and Main resumes and finishes. CS Chapter 8 – Data Structures

10 Chapter 8 – Data Structures
The Queue A queue is a data structure that manages a list of similar items in such a way that all insertions take place at one end of the list, while all deletions take place at the other end. In effect, one end of the list is considered the “rear” of the queue, where new items enter; and the other end is considered the “front” of the queue, where old items are removed. Example Initial Queue: F R After Insert 2: 7 4 2 F/R After Insert 7: 7 F R After Remove: 4 2 F R After Insert 4: 7 4 F R After Insert 5: 4 2 5 CS Chapter 8 – Data Structures

11 Comparison: Queue Implementations Chapter 8 – Data Structures
Using an array Using a linked list void Insert(int List[50], int &Front, int &Rear, int item) { if (Front != (Rear+1)%50) Rear = (Rear+1)%50; List[Rear] = item; if (Front == -1) Front = Rear; } int Remove(int List[50], int &Front, int &Rear) int val = -1; if (Front > -1) val = List[Front]; if (Front == Rear) Front = Rear = -1; else Front = (Front+1)%50; return val; void Insert(nodePtr &ListFront, nodePtr &ListRear, int item) { nodePtr ptr = new node; ptr->value = item; ptr->next = NULL; if (ListFront == NULL) ListFront = ptr; else ListRear->next = ptr; ListRear = ptr; } int Remove(nodePtr &ListFront, nodePtr &ListRear) int val = -1; if (ListFront != NULL) val = ListFront->value; ListFront = ListFront->next; ListRear = NULL; return val; CS Chapter 8 – Data Structures

12 Example Queue Application: Batch Processing
CPU processing Job A Job A arrives and starts processing: Job Queue: Job B arrives: CPU processing Job A Job Queue: B Jobs C & D arrive: CPU processing Job A Job Queue: B C D Job A completes; Job B starts processing: CPU processing Job B CPU processing Job A Job Queue: C D CS Chapter 8 – Data Structures

13 Chapter 8 – Data Structures
A binary tree is a hierarchical data structure that manages a collection of similar items in such a way that one item is designated as the “root” of the tree, and every other item is either the left or right “offspring” of some previously positioned item. Data Structure #6: The Binary Tree Example Implementation struct node; typedef node *nodePtr; struct node { int value; nodePtr left; nodePtr right; }; nodePtr Tree; 8 3 14 1 5 7 10 12 19 16 23 17 Example: Binary Insertion Tree Each left offspring of a node has a value less than the node’s value Each right offspring of a node has a value greater than or equal to the node’s value CS Chapter 8 – Data Structures

14 Recursive Insertion into a Binary Insertion Tree
void Bin_Insert(nodePtr &Tree, int item) { if (Tree == NULL) nodePtr ptr = new node; ptr->value = item; ptr->left = NULL; ptr->right = NULL; } else if (item < Tree->value) Bin_Insert(Tree->left, item); else Bin_Insert(Tree->right, item); Example: Where will a new node containing the integer 11 be inserted? 8 3 14 1 5 7 10 12 19 16 23 17 11 8 3 14 1 5 7 10 12 19 16 23 17 CS Chapter 8 – Data Structures

15 Recursive Traversal of a Binary Insertion Tree
void Inorder(nodePtr Tree) { if (Tree != NULL) Inorder(Tree->left); cout << Tree->value << endl; Inorder(Tree->right); } 1 3 5 7 8 10 12 8 3 14 1 5 7 10 12 19 16 23 17 Example: Apply Inorder to this binary insertion tree: 14 16 17 19 23 CS Chapter 8 – Data Structures

16 What does this function do to a binary tree?
int Sumac(nodePtr Tree) { int leftbranch, rightbranch; if (Tree == NULL) return 0; else leftbranch = Sumac(Tree->left); rightbranch = Sumac(Tree->right); return leftbranch + rightbranch + Tree->value; } 125 13 5 20 34 22 7 9 15 51 61 34 22 31 9 15 CS Chapter 8 – Data Structures


Download ppt "Data Structure #1: The Array"

Similar presentations


Ads by Google