Algorithms and data structures Protected by 7.6.2014.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

COMP171 Fall 2005 Lists.
Stacks, Queues, and Linked Lists
Linked Lists.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Algorithms and data structures
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
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.
Algorithms and data structures Protected by
Data Structures: A Pseudocode Approach with C
1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Algorithms and data structures Protected by
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Introduction to Data Structures Systems Programming.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Introduction to Data Structures Systems Programming Concepts.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Algorithms and data structures Protected by
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
CHP-3 STACKS.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
Sven Koerber-Abe, 2013 Begrüßen und Verabschieden Begrüßen und Verabschieden.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Grammatik: sollen Sven Koerber-Abe, 2015.
STACKS & QUEUES for CLASS XII ( C++).
Data Structures Using C, 2e
Chapter 12 – Data Structures
5.13 Recursion Recursive functions Functions that call themselves
Grammatik: nehmen, essen, möchten
Grammatik: wohnen mögen
Data Structures and Algorithms
Dr. Bernard Chen Ph.D. University of Central Arkansas
Stacks and Queues.
Stack and Queue APURBO DATTA.
Training of Trainers Workshop
Writing the Methods Section
Einkaufen in Deutschland
Writing the Results Section
Training of Trainers Workshop
Linked List (Part I) Data structure.
Preparing Tables and Figures: Some Basics
The Structure of Journal Articles
Grammatik: Das ist ein …
Barbara Gastel INASP Associate
Ja / Doch Sven Koerber-Abe, 2013.
Data Structures and Algorithms
Grammatik: wohnen mögen
Writing the Introduction
Grammatik: ich, mich Sven Koerber-Abe, 2014.
Grammatik: nehmen, essen, möchten
Abstract Data Types Stacks CSCI 240
Presentation transcript:

Algorithms and data structures Protected by

Creative Commons n You are free to: share copy and redistribute the material in any medium or format share copy and redistribute the material in any medium or format adapt remix, transform, and build upon the material adapt remix, transform, and build upon the material n Under the following terms: Attribution You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. Attribution You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. NonCommercial You may not use the material for commercial purposes. NonCommercial You may not use the material for commercial purposes. ShareAlike If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. ShareAlike If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. No additional restrictions You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits. Text copied from / 21Algorithms and data structures, FER Notices: You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation. No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material.

Stack

Algorithms and data structures, FER4 / Stack n Data structure where the last stored record is processed first n Required operations: Addition ( push ) of elements at the top of the stack Addition ( push ) of elements at the top of the stack Removing ( pop ) of elements from the top of the stack Removing ( pop ) of elements from the top of the stack Initialisation of an empty stack Initialisation of an empty stack A single operation push or pop requires an equal execution time, regardless of the number of already stored elements A single operation push or pop requires an equal execution time, regardless of the number of already stored elements n The case when the stack is full may require the allocation of additional memory and repeated execution of the program An empty stack does not necessarily imply an error An empty stack does not necessarily imply an error

Algorithms and data structures, FER5 / MAXSTOG=5 Stack – array implementation n It can be implemented using a static data structure According to the principle Last In First Out ( LIFO ), elements are added to or removed from the one-dimensional array of a given structure According to the principle Last In First Out ( LIFO ), elements are added to or removed from the one-dimensional array of a given structure The value marking the top position of the stack is updated The value marking the top position of the stack is updated n Initialisation of an empty stack: Resetting the position of the top Resetting the position of the top StogPoljem (StackByArray) #define MAXSTACK 5 typedef struct { int top, array[MAXSTACK]; } Stack; void init_stack(Stack *stack){ stack->top = -1; } stack->top MAXSTACK=5

Algorithms and data structures, FER6 / Push an element on the stack int push(int element, Stack *stack) { if (stack->top>= MAXSTACK-1) return 0; stack->top++; stack->array[stack->top] = element; return 1; } Determine the complexity! Calling program: Stack stack; init_stack(&stack); push(5, &stack); push(2, &stack); push(7, &stack); push(-4, &stack); push(1, &stack); push(9, &stack); stack->top O(1)

Algorithms and data structures, FER7 / Pop an element from the stack int pop (int *element, Stack *stack) { if (stack->top < 0) return 0; *element = stack->array[stack->top]; stack->top--; return 1; } Calling program: pop(&element, &stack); Determine the complexity! stack->top O(1)

Algorithms and data structures, FER8 / Exercises n Write the function that pops elements from a stack implemented with a static array. The single element is a floating point number. If the operation failed, the function returns 0, otherwise 1. Write the function that pushes elements on a stack imlemented with a static array with maximum capacity of MAXR records. A record to be stored on stack consists of an integer and 10 floating point numbers. If the operation failed, the function returns 0, otherwise 1. The function prototype is Write the function that pushes elements on a stack imlemented with a static array with maximum capacity of MAXR records. A record to be stored on stack consists of an integer and 10 floating point numbers. If the operation failed, the function returns 0, otherwise 1. The function prototype is int push(struct node element, Stack *stack);

Algorithms and data structures, FER9 / Exercises There is an unformatted file stack.dat on disk, organised as a stack. At the file beginning it is written the maximum allowed capacity of the stack, expressed as the number of records ( int ) and the address of the last element written on the stack ( long ). An element on the stack is a record with the passed examination data for a student: There is an unformatted file stack.dat on disk, organised as a stack. At the file beginning it is written the maximum allowed capacity of the stack, expressed as the number of records ( int ) and the address of the last element written on the stack ( long ). An element on the stack is a record with the passed examination data for a student: ID number ( long ) ID number ( long ) Name and family name (24+1 character) Name and family name (24+1 character) Code of the course ( int ) Code of the course ( int ) Grade ( short ) Grade ( short ) Define the data type Stack and write the necessary functions for handling the stack. As the main program use the main from StogPoljem.c (StackByArray.c) Define the data type Stack and write the necessary functions for handling the stack. As the main program use the main from StogPoljem.c (StackByArray.c)

Lists

Algorithms and data structures, FER11 / Basic terms Linear list A=(a 1,a 2,...a n ) is a data structure consisting of an ordered sequence of elements, selected from a data set Linear list A=(a 1,a 2,...a n ) is a data structure consisting of an ordered sequence of elements, selected from a data set A linear list is empty if it contains n=0 elements A linear list is empty if it contains n=0 elements List elements a i are called atoms List elements a i are called atoms n A list can be implemented using the static data structure - array

Algorithms and data structures, FER12 / List implementation n The dynamic data structure to implement a list consists of a pointer to the first list element and of an arbitrary number of atoms n Each atom consists of a data part and a pointer to the next list element n For each list atom, the memory is allocated at the moment when needed to store data, and it is released when data are deleted n Granulation is of the size atom struct at { int element; struct at *next; }; typedef struct at atom; next element atom

Algorithms and data structures, FER13 / Empty and non empty list Empty list 5242 Non empty list head

Algorithms and data structures, FER14 / Stack – list implementation n Stack, earlier implemented with an array, can also be implemented with a linear list Insertion to and deletion from the list is performed on the same end of the list Insertion to and deletion from the list is performed on the same end of the list Head of the list serves as the top of the stack Head of the list serves as the top of the stack StogListom (StackByList) struct at { type element; struct at *next; }; typedef struct at atom; typedef struct{ atom *top; } Stack; void init_stack(Stack *stack){ stack->top = NULL; } stack->top

Algorithms and data structures, FER15 / Stack – list implementation (push element) int push(type element, Stack *stack) { atom *new; if ((new = (atom*) malloc (sizeof (atom))) != NULL) { new->element = element; new->next = stack->top; stack->top = new; return 1; } else return 0; } Calling program: Stack stack; init_stack (&stack); push (5, &stack); stack->top 5 new

Algorithms and data structures, FER16 / int push (type element, Stack *stack) { atom *new; if ((new = (atom*) malloc (sizeof (atom))) != NULL) { new->element = element; new->next = stack->top; stack->top = new; return 1; } else return 0; } Calling program: Stack stack; init_stack(&stack); push (5, &stack); push (3, &stack); stack->top 5 3 new Complexity? O(1) Stack – list implementation (push new element)

Algorithms and data structures, FER17 / int pop (type *element, Stack *stack) { atom *aux; if (stack->top == NULL) return 0; *element = stack->top->element; aux = stack->top->next; /* address of the new top */ free (stack->top);/* release the old top */ stack->top = aux; /* set the new top */ return 1; } Calling program: pop (&element, &stack); stack->top 5 3 aux Stack – list implementation (pop element) - I

Algorithms and data structures, FER18 / int pop (type *element, Stack *stack) { atom *aux; if (stack->top == NULL) return 0; *element = stack->top->element; aux = stack->top->next; /* address of the new top */ free (stack->top); /* release the old top */ stack->top = aux; /* set the new top */ return 1; } Calling program: pop (&element, &stack); stack->top 5 aux Stack – list implementation (pop element) - II

Algorithms and data structures, FER19 / Stack – list implementation (pop element from empty stack) int pop (type *element, Stack *stack) { atom *aux; if (stack->top == NULL) return 0; *element = stack->top->element; aux = stack->top->next; /* address of the new top */ free (stack->top); /* release the old top */ stack->top = aux; /* set the new top */ return 1; } Calling program: pop (&element, &stack); stack->top aux Complexity? O(1)

Algorithms and data structures, FER20 / Memory usage n Representation of stack using a list requires more memory per data (because a pointer is surplus) More flexibility is achieved More flexibility is achieved n Multiple stacks can simultaneously use the same memory space n Memory usage is proportional to the size of data on the stack, not determined by the maximum stack capacity n On the other hand, capacity of a single stack is limited only by the available memory