المكدس Stack (abstract data type) The stack is a very common data structure used in programs. It hold objects, usually all of the same type. Stacks are.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
Topic 15 Implementing and Using Stacks
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.
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.
Topic 15 Implementing and Using Stacks
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
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.
Objectives of these slides:
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
EENG212 Algorithms and Data Structures
For more notes and topics VISIT: IMPLEMENTATION OF STACKS eITnotes.com.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Data Structures & Algorithms
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
Stacks Objective After this lecture you will be able to: Describe a stack Describe the representation of stack using linear array Describe the representation.
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
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.
Stacks Chapter 3 Objectives Upon completion you will be able to
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
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.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Chapter 4 Stacks
Stacks and Queues Chapter 4.
COMPSCI 107 Computer Science Fundamentals
CS 201 Data Structures and Algorithms
MEMORY REPRESENTATION OF STACKS
Homework 4 questions???.
Stacks.
Data Structures – Week #3
Algorithms and Data Structures
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Stacks – Calculator Application
Visit for more Learning Resources
Stacks – Calculator Application
PART II STACK APPLICATIONS
Stack.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Abstract Data Types and Stacks
Stacks CS-240 Dick Steflik.
Stack.
LINEAR DATA STRUCTURES
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

المكدس Stack (abstract data type) The stack is a very common data structure used in programs. It hold objects, usually all of the same type. Stacks are linear data structures, which all additions and deletion are restricted to one end that is Top. واحدة من هياكل البيانات الشائعة الاستخدام داخل البرامج. تحتوي علي عناصر من نفس النوع. من أنواع البيانات الخطية linear data structure. جميع عمليات الإضافة والحذف تتم من نهاية واحدة في stack و هي قمة الـ stack.

In computer science, a stack is a last in, first out (LIFO) abstract data type and linear data structure. A stack can have any abstract data type as an element, but is characterized by two fundamental operations, called push and pop. The push operation adds a new item to the top of the stack, or initializes the stack if it is empty. If the stack is full and does not contain enough space to accept the given item, the stack is then considered to be in an overflow state. The pop operation removes an item from the top of the stack. A pop either reveals previously concealed items, or results in an empty stack, but if the stack is empty then it goes into underflow state (It means no items are present in stack to be removed).computer scienceLIFOabstract data typedata structure abstract data typeelementoverflow

مبدأ عملها الداخل أولا هو الخارج آخرا LIFO. يعتبر المكدس نوع من أنواع البيانات المجردة أي يمكن اشتقاق كائنات من نفس نوعه والعمل عليها. تتميز بعمليتان POP, PUSH. العملية PUSH لإضافة عنصر بيان إلي قمة المكدس TOP أو عمل initialization للمكدس إذا كان فارغ أما إذا كان ممتلئ فان عملية الإضافة تسبب stack overflow. العملية POP لحذف عنصر بيان من قمة المكدس TOP و إذا كان المكدس فارغ فان عملية الحذف ( استخراج العناصر ) تسبب stack underflow.

A stack is a restricted data structure, because only a small number of operations are performed on it. The nature of the pop and push operations also means that stack elements have a natural order. Elements are removed from the stack in the reverse order to the order of their addition: therefore, the lower elements are those that have been on the stack the longest. Here are the minimal operations for an abstract stack : Push : Places an object on the top of the stack. Pop : Removes an object from the top of the stack and produces that object. العمليات التي تتم علي المكدس تكون بسيطة ( إضافة – حذف ). نتيجة لطبيعة عمليات الإضافة و الحذف في المكدس تكون عناصر المكدس موضوعة بترتيب معين, حيث العناصر تخرج ( حذف ) من المكدس في ترتيب معاكس لدخولها ( إضافة ) المكدس.

Order produced by a stack: Array is a sort of linear data structure. However, you can access any element in an array--not true for a stack, since you can only deal with the element at its top. المصفوفة نستطيع الوصول لأي عنصر بها أما المكدس فلا لأننا نتعامل فقط مع العنصر الموجود بقمة المكدس. بفرض أن لدينا مكدس يحتوي علي حروف ونريد إجراء عمليات إضافة و حذف لعناصر المكدس. 1- We begin with stack empty: // المكدس فارغ في البداية stack

2- Push(stack, A), giving: | A | <-- top Stack 3- Push(stack, B), giving: | B | <-- top | A | Stack

4- remove an item, letter = Pop(stack), giving: | A | <-- top | B | stack letter 5- Push(stack, C), giving: | C | <-- top | A | stack

مثال : بفرض ان لدينا stack تحتوي علي العناصر A,B ( حروف ) فان TOP=1, العنصر B موجود في TOP | A | B | | | | 1 | top اذا اضفنا العنصر C الي المكدس Push(stack, 'C') ينتج : | A | B | C | | | 2 | top اذا حذفنا عنصر من المكدس letter = Pop(stack) : | A | B | | | | 1 | | C | top letter استخدام المصفوفة لتمثيل المكدس

# include int top; int items[30]; void push(int x) { if (top == 30) // full stack { cout<<"Error: stack overflow\n"; } else { top++; items[top] = x; } } استخدام المصفوفة لتمثيل المكدس

int pop() { if (top < 0) // empty stack { cout<<"Error: stack underflow\n"; } else { return items[top--]; } } void main() { top=-1; push(10); push(8); cout<<pop()<<endl; }

اضافة عقدة لبداية القائمة : (PUSH) struct node { int data; node *next; }; typedef struct node *PtrToNode; typedef PtrToNode List; عمليات الاضافة PUSH و الحذف POP ستتم من بداية القائمة HEAD استخدام القائمة لتمثيل المكدس

node* PUSH(List l, int info) //where l is a head { node *temp; if (l==NULL) { l=new node; l->data=info; l->next=NULL; } else { temp = new node; temp->data = info; temp->next=l; l = temp; } return l; // return with head node }

void main() { List Head; Head=NULL; // STACK IS EMPTY Head=PUSH(Head,20); Head=PUSH(Head,40); Head=PUSH(Head,80); }

عبور القائمة (Traverse) void traverse(List l ) { node *temp=l; while( temp!=NULL ) { cout data<<" "; temp = temp->next; }

حذف عقدة من بداية القائمة (POP) node* POP(List head) { node *temp; temp = head; head = temp->next; delete temp; return head; }

void main() { List Head; Head=NULL; Head=PUSH(Head,20); Head=PUSH(Head,40); Head=PUSH(Head,80); traverse(Head); cout<<endl; Head=POP(Head); traverse(Head); }

in most processors, several different registers may be used as additional stack pointers as needed. Stack in main memory Stack Applications تطبيقات المكدس هناك العديد من التطبيقات في حياتنا اليومية بدءا من رصة الكتب بالمكتبة الي رصة الورق في الطابعة. [1] Converting a decimal number into a binary number1

Algorithm: function outputInBinary(Integer n) Stack s = new Stack while n > 0 do Integer bit = n modulo 2modulo s.push(bit) if s is full then return error end if n = floor(n / 2)floor end while while s is not empty do output(s.pop()) end while end function

[2] Expression evaluation and syntax parsing2 Evaluation of an infix expression that is fully parenthesized Input: (((2 * 5) - (1 * 2)) / (11 - 9)) Output: 4 Calculators employing reverse Polish notation use a stack structure to hold values. Expressions can be represented in prefix, postfix or infix notations and conversion from one form to another may be accomplished using a stack. Many compilers use a stack for parsing the syntax of expressions, program blocks etc. before translating into low level code. Most programming languages are context-free languages, allowing them to be parsed with stack based machines.reverse Polish notationcontext-free languages Analysis: Five types of input characters Opening bracket Numbers Operators Closing bracket New line character

Data structure requirement: A character stack Algorithm 1. Read one input character 2. Actions at end of each input (2.1) Opening brackets Push into stack and then Go to step (1) (2.2) Number Push into stack and then Go to step (1) (2.3) Operator Push into stack and then Go to step (1) (2.4) Closing brackets Pop from character stack (2.4.1) if it is closing bracket, then discard it, Go to step (1) (2.4.2) Pop is used three times: first popped element is assigned to op2 second popped element is assigned to op third popped element is assigned to op1 Evaluate op1 op op2 Convert the result into character,pop '(', and push the result into the stack (2.5) New line character : Pop from stack and print the answer 3. STOP

Result: The evaluation of the fully parenthesized infix expression is printed as follows: Input String: (((2 * 5) - (1 * 2)) / (11 - 9)) Input Symbol Stack (from bottom to top) Operation ( ( ( ( 2 ( ( ( 2 * ( ( ( 2 * 5 ( ( ( 2 * 5 ) ( ( 10

2 * 5 = 10 and push - ( ( 10 - ( ( ( 10 - ( 1 ( ( 10 - ( 1 * ( ( 10 - ( 1 * 2 ( ( 10 - ( 1 * 2 ) ( ( * 2 = 2 & Push ) ( = 8 & Push /

( 8 / ( ( 8 / ( 11 ( 8 / ( 11 - ( 8 / ( ( 8 / ( ) ( 8 / = 2 & Push ) 4 8 / 2 = 4 & Push New line Empty Pop & Print

struct stack { int top; int items[30]; } ; void push(struct stack &ps, int x) { if (ps.top == 30) // full stack { cout<<"Error: stack overflow\n"; } else { ps.top++; ps.items[ps.top] = x; } } استخدام المصفوفة لتمثيل المكدس عملية الاضافة PUSH تستخدم لعمل Initialize للمكدس بالاضافة الي الاضافة. الاضافة تتم بوضع القيمة ( البيان ) في المصفوفة items بالتركيبة stack ثم زيادة top بمقدار 1. لابد من عمل اختبار للمصفوفة اذا كانت ممتلئة ام لا. لاغي

int pop(struct stack &ps) { if (ps.top < 0) // empty stack { cout<<"Error: stack underflow\n"; } else { return ps.items[ps.top--]; } } void main() { stack P; P.top=-1; push(P,10); push(P,8); cout<<pop(P)<<endl; } عملية الحذف pop تستخدم لاستخراج عنصر من المكدس ثم نقصان top بمقدار 1. لابد من عمل اختبار للمصفوفة اذا كانت فارغة ام لا. لاغي