Data Structure #1: The Array

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Advanced Data Structures Stack –a stack is dynamic data items in a linear order, such that the item first "pushed" in is the last item "popped" out. Think.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
C Programming : Elementary Data Structures 2009/04/22 Jaemin
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Chapter 9: Abstract Data Types and Algorithms Chapter 9 Abstract Data Types and Algorithms Page 84 Two keys to making computer software that works well:
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Chapter 8 Data Abstractions Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Information and Computer Sciences University of Hawaii, Manoa
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
Pointers OVERVIEW.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Data Structure and Algorithms
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
CSE 1342 Programming Concepts

Pointers and Linked Lists
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures Michael J. Watts
Pointers and Linked Lists
Data Structure Interview Question and Answers
12 C Data Structures.
Data Structure and Algorithms
Dr. Bernard Chen Ph.D. University of Central Arkansas
Problems with Linked List (as we’ve seen so far…)
Stacks and Queues.
Programming Abstractions
Data Structures Interview / VIVA Questions and Answers
Stack and Queue APURBO DATTA.
Stack.
Chapter 20: Binary Trees.
Pointers and Dynamic Variables
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Chapter 16-2 Linked Structures
Chapter 21: Binary Trees.
Popping Items Off a Stack Lesson xx
Lesson Objectives Aims
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Pointers and Linked Lists
Chapter 8: Data Abstractions
Programming Abstractions
Chapter 16 Linked Structures
Pointers & Dynamic Data Structures
CSI 1340 Introduction to Computer Science II
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
LINEAR DATA STRUCTURES
Presentation transcript:

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 111.01 Chapter 8 – Data Structures

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 111.01 Chapter 8 – Data Structures

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 111.01 Chapter 8 – Data Structures

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 111.01 Chapter 8 – Data Structures

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 111.01 Chapter 8 – Data Structures

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 111.01 Chapter 8 – Data Structures

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 111.01 Chapter 8 – Data Structures

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 111.01 Chapter 8 – Data Structures

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 111.01 Chapter 8 – Data Structures

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 111.01 Chapter 8 – Data Structures

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 111.01 Chapter 8 – Data Structures

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 111.01 Chapter 8 – Data Structures

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 111.01 Chapter 8 – Data Structures

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 111.01 Chapter 8 – Data Structures

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 111.01 Chapter 8 – Data Structures

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 111.01 Chapter 8 – Data Structures