ENEE150 Discussion 09 Section 0101 Adam Wang.

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

Linked List Alternate approach to maintaining an array of elements Rather than allocating one large group of elements, allocate elements as needed Q: how.
Linked Lists.
PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
Symbol Table.
CS 261 – Data Structures AVL Trees. Binary Search Tree: Balance Complexity of BST operations: proportional to the length of the path from the root to.
Binary Trees. DCS – SWC 2 Binary Trees Sets and Maps in Java are also available in tree-based implementations A Tree is – in this context – a data structure.
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.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Advanced Pointers and Structures Pointers in structures Memory allocation Linked Lists –Stacks and queues Trees –Binary tree example.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Slides by Donald W. Smith
COMP 103 Linked Structures
Chapter 16: Linked Lists.
ENEE150 Discussion 04 Section 0101 Adam Wang.
IT Department – Bunda Mulia University
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Chapter 12 – Data Structures
5.13 Recursion Recursive functions Functions that call themselves
Higher Order Tries Key = Social Security Number.
UNIT – I Linked Lists.
Linked Lists & Hash Tables
12 C Data Structures.
ENEE150 Discussion 07 Section 0101 Adam Wang.
Containers and Lists CIS 40 – Introduction to Programming in Python
Recitation 13 Searching and Sorting.
ENEE150 Discussion 02 Section 0101 Adam Wang.
Simple Sorting Algorithms
UNIT III TREES.
Programming Abstractions
ENEE150 Discussion 06 Section 0101 Adam Wang.
Programming Abstractions
Lecture 22 Binary Search Trees Chapter 10 of textbook
ENEE150 Discussion 13 Section 0101 Adam Wang.
UNIX PROCESSES.
Trees.
Chapter 20: Binary Trees.
Chapter 10: Non-linear Data Structures
Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CSCI206 - Computer Organization & Programming
Chapter 16-2 Linked Structures
Chapter 21: Binary Trees.
Circular Buffers, Linked Lists
Optimizing Malloc and Free
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
Lesson Objectives Aims
B- Trees D. Frey with apologies to Tom Anastasio
B- Trees D. Frey with apologies to Tom Anastasio
Programming Abstractions
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Tim Ehrlich Growing Arrays in C.
Linked List.
Homework Any Questions?.
B- Trees D. Frey with apologies to Tom Anastasio
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Indexing 4/11/2019.
Lists.
Data Structures & Algorithms
Priority Queues & Heaps
CSCS-200 Data Structure and Algorithms
Indexing, Access and Database System Architecture
Presentation transcript:

ENEE150 Discussion 09 Section 0101 Adam Wang

Overview Hash tables and Trees Project 3 Quiz

Hash Tables An array of linked lists (called buckets) Each piece of data has a hash code This code gets mapped to an index in the array The entry gets added to the linked list Ideally, each linked list has very few nodes Very fast for looking up data pieces quickly Other variations: hashmap, treemap, linkedhashmap

Trees Like linked lists, except each node now has two pointers Binary Search tree: Every child to the left is less than the parent, and every child to the right is greater than the parent When searching for an element, we only have to search on average log(N) nodes We’ll see later that it’s natural to use recursion for trees

Project 3: Bus Simulator Simulates a bus moving through bus stops and adding/removing passengers The program will take in a passengers file and a log file The passenger file contains all the data about when each passenger will come and which bus stop they will get on/get off You will write to the log file who gets on/gets off at each time step You will use a counter to simulate time moving; think of it as printing out what has happened during each hour of the simulation

Sample passenger file First number is number of bus stops Each line after that represents a passenger 1st number: arrival time (“arrival hour”) Ascending order since it’s a simulation 2nd number: Starting bus stop 3rd number: Destination bus stop Bus only goes forwards; some passengers have to go all the way around

Recommended data structures First thing you’ll do is malloc an array of bus stops You know the size from the first number in the passengers file Then you’ll initialize the bus This doesn’t need to be malloc’d At each time step you’ll add/remove passengers to/from the bus/bus stop

Global variables you might want to use

Recommended methods

In main Open the passenger and log files to be read and written argv[1] is the passenger file and argv[2] is the log file; “r” for read and “w” for write Read in number of bus stops and initialize array of bus stops, as well as the bus Each bus stop you have to initialize the ID as well While Loop: runs until done reading passenger file and no more passengers in the ENTIRE system Each iteration of the loop simulates one timestep/one “hour” When done, remember to print stats of the simulation

Inside while loop (continued) Print to log file the status of each bus stop Use method fprintf 1st time step won’t have any passengers Read in new passengers from file that are arriving at this time Use fscanf The FILE* will recall where you last left off reading it at Remove any passengers getting off at this stop (disembark_bus) Add any passengers at this stop getting on (board_bus) Increase wait times of passengers at all the bus stops (age_passengers) Move bus to the next station Increase time counter

void initialize_system() Malloc the array of bus stops Initialize the bus Remember to initialize all the fields to 0/NULL Each bus stop also has a unique ID representing its stop #

int get_passengers(FILE *inFile) Reads in any passengers arriving at the current time step May be more than one, or none at all! For each passenger, malloc and initialize each of its fields Insert that passenger into the proper bus stop (insert_passenger) Increment total passengers, # waiting at that station, etc. Return value: whether any new passengers were read in Obviously this method can return whatever you want it to

void disembark_bus() Remove any passengers on the bus getting off at the current bus stop Iterate through passenger linked list If passenger’s destination is this stop, Remove from the list (delete_passenger) Decrement the bus's number of passengers Decrement total number of passengers Record wait time of passenger

void board_bus() Adds all passengers waiting at the current bus stop to the bus Look up list concatenation Remember: we can just copy over pointers, because memory has already been allocated We also need to increment # of passengers on the bus, and set number of passengers at bus stop to 0

void age_passengers() Increment wait time of all the passengers waiting at each bus stop Simple loop for each bus stop, and loop for each passenger

void write_stops (FILE *logFile) Writes the status of each bus stop to the log file Remember, the format has to match EXACTLY what’s in the expected output

Void insert_passenger (passenger **head, passenger *to_add) Inserts “to_add” into the linked list “head” Make this easy for you: just insert at the head

Void delete_passenger (passenger **head, passenger *to_del) Deletes “to_del” from the linked list You don’t have to follow this format; use the delete method from class if you want You don’t even need this separate method at all

void write_stats(FILE *logFile) Writes final stats of the simulation Make use of global variables sim_time, total_passengers, etc. for this

Extra Only the passenger linked lists and the array of bus stops needs to be dynamically allocated Up to you what to do with the bus and each bus stop Lucky for you, you are not required to free any memory Add to the head of the linked list whenever you can (easy) Hardest part is just keeping track of all the variables

Quiz wget ece.umd.edu/~aw97/quizzes/quiz3.c Dynamically allocate a struct and insert it into the tail of a linked list submit 2017 fall enee 150 0101 103 quiz3.c