Stack Data Structures (Arabic) PDF

Document Details

PreciousHarpGuitar

Uploaded by PreciousHarpGuitar

Misr University for Science and Technology

Tags

data structures programming computer science stack

Summary

This document includes questions and examples of data structures, including stack implementation in C++. It's intended for undergraduate students studying data structures and algorithms.

Full Transcript

# علوم الحاسب ## الفرقة الثانية ### Session 3 ### هياكل البيانات ## Data Structures ## Q.1. Define pointer. A pointer is a variable which can store the address of another variable. A pointer is also a variable, so it has its own memory address. **Memory address:** 1020 1024 1032 - 100 integer - 1...

# علوم الحاسب ## الفرقة الثانية ### Session 3 ### هياكل البيانات ## Data Structures ## Q.1. Define pointer. A pointer is a variable which can store the address of another variable. A pointer is also a variable, so it has its own memory address. **Memory address:** 1020 1024 1032 - 100 integer - 1024 pointer **To define pointers** - type\* pointer\_name; - type \*pointer\_name; **Note:** type is the type of data pointed to (int, char, double) ## Q.2. Using the next memory address, write output for the following code. **Memory address:** 1020 1024 1032 - 88 - 100 - 1024 - ... ```c++ int a = 100; int *p = &a; cout << a << endl; cout << &a << endl; cout << p << endl; cout << *p << endl; cout << &p << endl; ``` Output: - 100 - 1024 - 1024 - 100 - 1032 ## Q.3. Write output for the following code. ```c++ #include <iostream> #include <conio.h> using namespace std; void main() { int *p, x; p = &x; cout << "Enter a number \n"; cin >> x; cout << "\n THE VALUE OF X(THROUGH POINTER) IS " << *p; cout << "\n THE VALUE OF X IS " << X; cout << "\n THE VALUE OF p " << p; *p = 101; cout << "\n THE VALUE OF X(THROUGH POINTER) IS " << *p; cout << "\n THE VALUE OF X IS " << X; cout << "\n THE VALUE OF p " << p <<endl; _getch(); } ``` **Enter a number:** 100 **Output:** - THE VALUE OF X(THROUGH POINTER) IS 100 - THE VALUE OF X IS 100 - THE VALUE OF p 00000075467AFBE4 - THE VALUE OF X(THROUGH POINTER) IS 101 - THE VALUE OF X IS 101 - THE VALUE OF p 00000075467AFBE4 ## Q.4. What is Structure? Give an example for structure. A structure is a collection of data items (fields) or variables of different data types that are referenced under the same name. A structure provides convenient means of keeping related information together. ```c++ #include <iostream> #include <conio.h> using namespace std; struct emp { char name[25]; int age; float salary; }; void main() { emp e; cout << "Enter the name, age and salary \n"; cin.get(e.name, 25); cin >> e.age >> e.salary; cout << "\n NAME IS " << e.name << "\n AGE IS " << e.age << "\n SALARY IS " << e.salary << endl; _getch(); } ``` **Enter the name, age and salary:** - Ali - 25 - 5000 **Output:** - NAME IS Ali - AGE IS 25 - SALARY IS 5000 ## Q.5. What is stack? A stack is an Abstract Data Type (ADT), it behaves like a real-world stack, for example - a deck of cards or a pile of plates, etc. - Stack ADT allows all data operations at one end only. [True] - At any given time, we can only access the top element of a stack. [True] - Stack works as LIFO (Last-in-first-out). - The insertion operation is called PUSH. - Removal operation is called POP. - push() Pushing (storing) an element on the stack. - pop() Removing (accessing) an element from the stack. - Peek() get the top data element of the stack, without removing it. - isFull() check if stack is full. - isEmpty() check if stack is empty. - A stack can be implemented by means of Array, Structure, Pointer, and Linked List. - Stack can either be a fixed size (array) one or it may have a sense of dynamic resizing. ## Stack Implementation ```c++ #include <iostream> using namespace std; int stack[5]; int top = 0; void push(int); int pop(); void traverse(); int is_empt(); int top_element(); int main() { int element, choice; for (;;) { cout << "Stack Operations.\n"; cout << "1. Insert into stack (Push operation).\n"; cout << "2. Delete from stack (Pop operation).\n"; cout << "3. Print top element of stack.\n"; cout << "4. Check if stack is empty.\n"; cout << "5. Traverse stack.\n"; cout << "6. Exit.\n"; cout << "Enter your choice.\n"; cin >> choice; switch (choice) { case 1: if (top == 5) { cout << "Error: Overflow\n\n"; } else { cout << "Enter a value to insert.\n"; cin >> element; push(element); } break; case 2: if (top == 0) cout << "Error: Underflow.\n\n"; else { element = pop(); cout << "Element removed from the stack is " << element << "\n"; } break; case 3: if (!is_empt()) { element = top_element(); cout << "Element at the top of the stack is " << element; } else cout << "The stack is empty.\n\n"; break; case 4: if (is_empt()) cout << "The stack is empty.\n\n"; else cout << "The stack isn't empty.\n\n"; break; case 5: traverse(); break; case 6: exit(0); } } } void push(int value) { stack[top] = value; top++; } int pop() { top--; return stack[top]; } int top_element() { return stack[top - 1]; } int is_empt() { if (top == 0) return 1; else return 0; } void traverse() { int d; if (top == 0) { cout << "The stack is empty.\n\n"; return; } cout << "There are" << top << "elements in the stack.\n"; for (d = top - 1; d >= 0; d--) cout << "\n" << stack[d]; } ``` ## Three applications of stacks: - Expression evaluation. - Backtracking (game playing, finding paths, exhaustive searching) - Memory management, run-time environment for nested language features.

Use Quizgecko on...
Browser
Browser