SlideShare une entreprise Scribd logo
1  sur  14
Télécharger pour lire hors ligne
Chapter 5
Data Structures
Assoc. Prof. Dr. Oğuz FINDIK
2016-2017
KBUZEM
KARABUK UNIVERSITY
1
 A stack is an abstract data type, commonly used in most
programming languages.
 Like real world applications, stack allows all data operations
at one end only. At any given time, we can only Access the
top element of a stack.
 This feature makes it LIFO data structure. LIFO stands for
Last-in-first-out. Here the element which is placed last, is
accessed first. In stack terminology, insertion operation is
called PUSH operation and removal operation is called POP
operation.
2
Stacks
push() – pushing an element into the stack
pop() – get an element from stack and remove it ,
peek() – get the element from top of the stack, without
removing it.
isFull()- check stack whether is full or not.
isEmpty() - check stack whether is empty or not.
3
Basic Operations
int peek() {
return stack[top];
}
bool isfull() {
if(top == MAXSIZE)
return true;
else
return false;
}
4
peek,isFull and isEmpty function
bool isempty() {
if(top == -1)
return true;
else
return false;
}
 The process of putting a new data element onto stack is known
as PUSH operation. Push operation invoşves series of steps –
 Step 1 − Check if stack is full.
 Step 2 − If stack is full, produce error and exit.
 Step 3 − If stack is not full, increment top to point next empty
space.
 Step 4 − Add data element to the stack location, where top is
pointing.
 Step 5 − return success.
5
PUSH operation
6
Push operation
void push(int data) {
if(!isFull()) {
top = top + 1;
stack[top] = data;
}else {
printf("Could not
insert data, Stack is
full.n");
}
}
A POP operation may involve the following steps −
Step 1 − Check if stack is empty.
Step 2 − If stack is empty, produce error and exit.
Step 3 − If stack is not empty, access the data element at which top is
pointing.
Step 4 − Decrease the value of top by 1.
Step 5 − return success
7
POP operation
8
POP operations
int pop(int data) {
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
}else {
printf("Could not retrieve
data, Stack is empty.n");
}
}
9
Example
bool isEmpty() {
extern indis;
bool temp = true;
if (indis == - 1)
temp = true;
else
temp = false;
return temp;}
int push(char c, char *s) {
extern indis;
if (!isFull(indis)) {
indis++;
*(s + indis) = c;
return 1;
} else
return 0;}
* myfunctions.c
#include "myheaderfile.h"
int strl_ln(char *s1) {
int length = 0;
while (s1 != '0') {
length++;
s1++;
}
return length++;}
bool isFull() {
extern indis;
bool temp = true;
if (indis == STACKSIZE-1)
temp = true;
else
temp = false;
return temp;}
10
Example
char pop(char *s) {
extern indis;
if (!isEmpty(indis))
return s[indis--];
}
char peek(char *s) {
extern indis;
if (!isEmpty(indis))
return s[indis];
}
11
Example (MYHEADERFILE.h)
#ifndef MYHEADERFILE_H_
#define MYHEADERFILE_H_
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define STACKSIZE 10
#define N 10
typedef enum
bool{true=1,false=0}bool;
int str_ln(char *);
int push(char c,char*);
char pop(char*);
char peek(char*);
bool isFull();
bool isEmpty();
#endif /* MYHEADERFILE_H_ */
12
Example (Ders.c)
#include "myheaderfile.h"
int indis=-1;
char stack[STACKSIZE];
int main(void) {
push('s',stack);push('a',stack);pu
sh('f',stack);
push('r',stack);push('a',stack);pu
sh('n',stack);
push('b',stack);push('o',stack);
push('l',stack);push('u',stack);pu
sh('u',stack);
int i =0;
for(i=0;i<STACKSIZE;i++){
printf("%c",pop(stack));
}
return EXIT_SUCCESS;
}
13
Example 2
int genericPush(void *c, int
boyut) {
extern indis,stack;
if (!isFull(indis)) {
indis++;
memcpy(stack, c, boyut);
stack = stack + boyut;
return 1;
} else
return 0;
}
void *genericPop( int boyut) {
extern indis,stack;
if (!isEmpty(indis)) {
indis--;
stack = stack - boyut;
return stack;
}
}
14
Example 2
#include "myheaderfile.h"
void *stack;
int indis = -1;
typedef double myType;
#define N 10
int main(void) {
stack = calloc(N, sizeof(void *));
myType s[] = { 65.0, 66.2, 67.4, 68.3,
69, 70, 71.3,72.5,73.4,74.5 };
char *p = calloc(N, sizeof(void *));
int i;
myType value;
int lengthOfVariable = sizeof(myType);
printf("length Of Variable:%dn",
lengthOfVariable);
for (i = 0; i < N; i++) {
value = s[i];
if (!genericPush(&value,
lengthOfVariable)) {
printf("stack doldu ");
}
}
for (i = 0; i < N; i++) {
printf("%7.2f", *(myType*)
genericPop(lengthOfVariable));
}
return EXIT_SUCCESS;
}

Contenu connexe

Tendances

data structure, stack, stack data structure
data structure, stack, stack data structuredata structure, stack, stack data structure
data structure, stack, stack data structure
pcnmtutorials
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
Abbott
 
Stack data structure
Stack data structureStack data structure
Stack data structure
Tech_MX
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5
ecomputernotes
 

Tendances (20)

Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
stack & queue
stack & queuestack & queue
stack & queue
 
Stack
StackStack
Stack
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Stack and its operations
Stack and its operationsStack and its operations
Stack and its operations
 
Stack & queue
Stack & queueStack & queue
Stack & queue
 
data structure, stack, stack data structure
data structure, stack, stack data structuredata structure, stack, stack data structure
data structure, stack, stack data structure
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Computer notes - Josephus Problem
Computer notes - Josephus ProblemComputer notes - Josephus Problem
Computer notes - Josephus Problem
 
Stack Implementation
Stack ImplementationStack Implementation
Stack Implementation
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Stack
StackStack
Stack
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
 
Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]
 

Similaire à Data structure week y 5

Stacks-and-Queues.pdf
Stacks-and-Queues.pdfStacks-and-Queues.pdf
Stacks-and-Queues.pdf
TobyWtf
 
Stack and its operation implemented with array new - Copy.pptx
Stack and its operation implemented with array new - Copy.pptxStack and its operation implemented with array new - Copy.pptx
Stack and its operation implemented with array new - Copy.pptx
Shivam Kumar
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
tameemyousaf
 

Similaire à Data structure week y 5 (20)

What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
Stacks-and-Queues.pdf
Stacks-and-Queues.pdfStacks-and-Queues.pdf
Stacks-and-Queues.pdf
 
Algorithm and Data Structure - Stack
Algorithm and Data Structure - StackAlgorithm and Data Structure - Stack
Algorithm and Data Structure - Stack
 
Stack of Data structure
Stack of Data structureStack of Data structure
Stack of Data structure
 
901230 lecture5&6
901230 lecture5&6901230 lecture5&6
901230 lecture5&6
 
Stack and its operation implemented with array new - Copy.pptx
Stack and its operation implemented with array new - Copy.pptxStack and its operation implemented with array new - Copy.pptx
Stack and its operation implemented with array new - Copy.pptx
 
STACK IN DATA STRUCTURE .pptx
STACK IN DATA STRUCTURE .pptxSTACK IN DATA STRUCTURE .pptx
STACK IN DATA STRUCTURE .pptx
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
ADT STACK and Queues
ADT STACK and QueuesADT STACK and Queues
ADT STACK and Queues
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptx
 
Chapter 4 stack
Chapter 4 stackChapter 4 stack
Chapter 4 stack
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
 
Stacks
StacksStacks
Stacks
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 

Plus de karmuhtam

Plus de karmuhtam (20)

Devre analizi deney malzeme listesi
Devre analizi deney malzeme listesiDevre analizi deney malzeme listesi
Devre analizi deney malzeme listesi
 
Deney 6
Deney 6Deney 6
Deney 6
 
Deney 5
Deney 5Deney 5
Deney 5
 
Deney 3 ve 4
Deney 3 ve 4Deney 3 ve 4
Deney 3 ve 4
 
Deney 1 ve 2
Deney 1 ve 2Deney 1 ve 2
Deney 1 ve 2
 
Data structure week y 5 1
Data structure week y 5 1Data structure week y 5 1
Data structure week y 5 1
 
Data structure week y 4
Data structure week y 4Data structure week y 4
Data structure week y 4
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 
Data structure week 2
Data structure week 2Data structure week 2
Data structure week 2
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1
 
13. sınıfları başlık dosyaları
13.  sınıfları başlık dosyaları13.  sınıfları başlık dosyaları
13. sınıfları başlık dosyaları
 
12. stl örnekler
12.  stl örnekler12.  stl örnekler
12. stl örnekler
 
11. stl kütüphanesi
11. stl kütüphanesi11. stl kütüphanesi
11. stl kütüphanesi
 
10. istisna isleme
10. istisna isleme10. istisna isleme
10. istisna isleme
 
9. şablonlar
9. şablonlar9. şablonlar
9. şablonlar
 
8. çok biçimlilik
8. çok biçimlilik8. çok biçimlilik
8. çok biçimlilik
 
7. kalıtım
7. kalıtım7. kalıtım
7. kalıtım
 
6. this işaretçisi ve arkadaşlık
6. this işaretçisi ve arkadaşlık6. this işaretçisi ve arkadaşlık
6. this işaretçisi ve arkadaşlık
 
5. kurucu, yok edici ve kopyalama fonksiyonları
5. kurucu, yok edici ve kopyalama fonksiyonları5. kurucu, yok edici ve kopyalama fonksiyonları
5. kurucu, yok edici ve kopyalama fonksiyonları
 
4. yapılar
4. yapılar4. yapılar
4. yapılar
 

Dernier

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Dernier (20)

Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 

Data structure week y 5

  • 1. Chapter 5 Data Structures Assoc. Prof. Dr. Oğuz FINDIK 2016-2017 KBUZEM KARABUK UNIVERSITY 1
  • 2.  A stack is an abstract data type, commonly used in most programming languages.  Like real world applications, stack allows all data operations at one end only. At any given time, we can only Access the top element of a stack.  This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here the element which is placed last, is accessed first. In stack terminology, insertion operation is called PUSH operation and removal operation is called POP operation. 2 Stacks
  • 3. push() – pushing an element into the stack pop() – get an element from stack and remove it , peek() – get the element from top of the stack, without removing it. isFull()- check stack whether is full or not. isEmpty() - check stack whether is empty or not. 3 Basic Operations
  • 4. int peek() { return stack[top]; } bool isfull() { if(top == MAXSIZE) return true; else return false; } 4 peek,isFull and isEmpty function bool isempty() { if(top == -1) return true; else return false; }
  • 5.  The process of putting a new data element onto stack is known as PUSH operation. Push operation invoşves series of steps –  Step 1 − Check if stack is full.  Step 2 − If stack is full, produce error and exit.  Step 3 − If stack is not full, increment top to point next empty space.  Step 4 − Add data element to the stack location, where top is pointing.  Step 5 − return success. 5 PUSH operation
  • 6. 6 Push operation void push(int data) { if(!isFull()) { top = top + 1; stack[top] = data; }else { printf("Could not insert data, Stack is full.n"); } }
  • 7. A POP operation may involve the following steps − Step 1 − Check if stack is empty. Step 2 − If stack is empty, produce error and exit. Step 3 − If stack is not empty, access the data element at which top is pointing. Step 4 − Decrease the value of top by 1. Step 5 − return success 7 POP operation
  • 8. 8 POP operations int pop(int data) { if(!isempty()) { data = stack[top]; top = top - 1; return data; }else { printf("Could not retrieve data, Stack is empty.n"); } }
  • 9. 9 Example bool isEmpty() { extern indis; bool temp = true; if (indis == - 1) temp = true; else temp = false; return temp;} int push(char c, char *s) { extern indis; if (!isFull(indis)) { indis++; *(s + indis) = c; return 1; } else return 0;} * myfunctions.c #include "myheaderfile.h" int strl_ln(char *s1) { int length = 0; while (s1 != '0') { length++; s1++; } return length++;} bool isFull() { extern indis; bool temp = true; if (indis == STACKSIZE-1) temp = true; else temp = false; return temp;}
  • 10. 10 Example char pop(char *s) { extern indis; if (!isEmpty(indis)) return s[indis--]; } char peek(char *s) { extern indis; if (!isEmpty(indis)) return s[indis]; }
  • 11. 11 Example (MYHEADERFILE.h) #ifndef MYHEADERFILE_H_ #define MYHEADERFILE_H_ #include <stdio.h> #include <stdlib.h> #include <time.h> #define STACKSIZE 10 #define N 10 typedef enum bool{true=1,false=0}bool; int str_ln(char *); int push(char c,char*); char pop(char*); char peek(char*); bool isFull(); bool isEmpty(); #endif /* MYHEADERFILE_H_ */
  • 12. 12 Example (Ders.c) #include "myheaderfile.h" int indis=-1; char stack[STACKSIZE]; int main(void) { push('s',stack);push('a',stack);pu sh('f',stack); push('r',stack);push('a',stack);pu sh('n',stack); push('b',stack);push('o',stack); push('l',stack);push('u',stack);pu sh('u',stack); int i =0; for(i=0;i<STACKSIZE;i++){ printf("%c",pop(stack)); } return EXIT_SUCCESS; }
  • 13. 13 Example 2 int genericPush(void *c, int boyut) { extern indis,stack; if (!isFull(indis)) { indis++; memcpy(stack, c, boyut); stack = stack + boyut; return 1; } else return 0; } void *genericPop( int boyut) { extern indis,stack; if (!isEmpty(indis)) { indis--; stack = stack - boyut; return stack; } }
  • 14. 14 Example 2 #include "myheaderfile.h" void *stack; int indis = -1; typedef double myType; #define N 10 int main(void) { stack = calloc(N, sizeof(void *)); myType s[] = { 65.0, 66.2, 67.4, 68.3, 69, 70, 71.3,72.5,73.4,74.5 }; char *p = calloc(N, sizeof(void *)); int i; myType value; int lengthOfVariable = sizeof(myType); printf("length Of Variable:%dn", lengthOfVariable); for (i = 0; i < N; i++) { value = s[i]; if (!genericPush(&value, lengthOfVariable)) { printf("stack doldu "); } } for (i = 0; i < N; i++) { printf("%7.2f", *(myType*) genericPop(lengthOfVariable)); } return EXIT_SUCCESS; }