Data Structures.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
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.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
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.
C Review. C Program Structure Compilation of a program: gcc –o hello hello.c -o specifies the name of the executable file Other flags: Wall – turn on.
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.
CSC2100B Tutorial 2 List and stack implementation.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Linked Lists. Introduction In linked list each item is embedded in a link Each item has two parts – Data – Pointer to the next item in the list Insert,
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Circular Linked List Singly Circular Linked List Doubly Circular Linked List.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
CSCE 3110 Data Structures & Algorithm Analysis
STACKS & QUEUES for CLASS XII ( C++).
CSE 1342 Programming Concepts
Cpt S 122 – Data Structures Abstract Data Types
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Data Structure By Amee Trivedi.
Lists, Stacks and Queues in C
Chapter 12 – Data Structures
CSCI-255 LinkedList.
UNIT – I Linked Lists.
12 C Data Structures.
12 C Data Structures.
Chapter 15 Lists Objectives
Data Structures and Algorithms
Linked list.
Stacks and Queues CMSC 202.
ENERGY 211 / CME 211 Lecture 12 October 17, 2008.
Stacks and Queues.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
C Review.
Introduction to Data Structure
CMSC 341 Lecture 5 Stacks, Queues
Pointers and Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
Stacks, Queues, and Deques
Object Oriented Programming COP3330 / CGS5409
Stacks, Queues, and Deques
Chapter 8: Data Abstractions
Lesson 6. Types Equality and Identity. Collections.
Review & Lab assignments
Data Structures and Algorithms
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Chapter 17: Linked Lists.
Abstract Data Type Abstract Data Type as a design tool
Stacks and Queues Prof. Michael Tsai 2017/02/21.
Linked Lists.
Lists.
Stacks LIFO C C B B B B A A A A A Push (A) Push (B) Push (C) Pop Pop.
Stacks, Queues, and Deques
Abstract Data Types Stacks CSCI 240
Presentation transcript:

Data Structures

Data Structures Building blocks of a solution The most important factor in designing a solution A smart selection of data structures wins!

Structure in C (review) Array: Collection of Same Types of Data Structure: Collection of Different Types of Data struct { char name[20]; int age; float salary; char hobby[3][20]; } employee; /* name, age, salary, hobby: members */ /* employee: variable of type struct { } */

Structure tag name struct EMPRECORD { char name[20]; int age; float salary; char hobby[3][20]; } employee, former_employee; /* EMPRECORD: tag name for { } */ /* struct EMPRECORD employee, former_employee; */

Member Data Types Primitive Types Array Structure int, float, double, char pointer Array Structure other struct defining struct

Structure Member Access char name[20]; int age; float salary; char hobby[3][20]; } employee; struct_variable.member_name (&struct_variable)->member_name /* employee.name */ /* employee.hobby[3][15] */

Struct Member Initialization char name[20]; int age; float salary; char hobby[3][20]; } employee; employee.name[] = “Neil Diamond”; employee hobby[3][] =“tennis and walking”; employee = {“hong gildong”, 25, 35000.0, “jump”};

Member Name Scope /* unique only within a single structure */ struct EMPRECORD { char name[20]; int age; float salary; char hobby[3][20]; } employee, former_employee; struct PERSON { char address[30]’ }; /* unique only within a single structure */

Structure: Use Example – IP Packet IP Header UDP Header V P X M .. Sequence Number Timestamp Synchronization Source ID First Contributing Source ID …… Last Contributing Source ID Application Data Real-Time Packet Transfer Protocol Message Format

Pointer and Self-Referential Structure struct NODE { int key; struct NODE *next; } node node1, node2, node3; node1.key = 100; node2.key = 250; node3.key = 467; node1.next = node2.next = node3.next = NULL; ; node1 100 null node2 250 null node3 467 null

Example (cont’d) 100 250 467 null node1.next = &node2; struct NODE { int key; struct NODE *next; } node node1, node2, node3; node1.next = &node2; node2.next = &node3; node1 100 node2 250 node3 467 null

Member Access via a pointer node1 100 node2 250 node3 467 null node1.next -> key 250 node1.next -> next -> key 467

Linked Lists (vs. array) You just saw them pros size of the list can grow/shrink easily easy to insert easy to delete easy to lookup cons ?

Stack and Queue List Queue Stack insert/delete an element at any location Queue insert at the head delete at the tail First-In-First-Out (FIFO) Stack only one position to insert and delete Last-In-First-Out (LIFO)

Stack Operations push(x, S) inserts x onto a stack S pop(S) deletes the most new elements from S, and returns it to the callee initialize(S) creates a new empty stack Full(S), Empty(S) check if the stack S is full/empty

Stack Implementation data structures full/empty array - S pointer – top full/empty #define MAX_STACK_SIZE 100 struct stack{ int data[MAX_STACK_SIZE]; int top = -1; } s1, s2, ...; int full(stack s) { if (s.top >= MAX_STACK_SIZE) return (1); else return (0); } int empty(stack s) { if (s.top < 0 ) return (1); else return (0); }

int pop(stack s) { if empty(s) return stack_empty(); return s.data[s.top--]; } void push(int x, stack s) { if (full(s)) { stack_full(); return; } s.data[++top] = x;

List, Queue queue is similar to a stack array vs linked list but list is a little bit difficult if we know the maximum size, array will do if when we know the size, insert/delete ops will waste memory linked list looks good, but link traverse is expensive then, what is your solution?