SlideShare a Scribd company logo
1 of 14
Download to read offline
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;
}

More Related Content

What's hot (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]
 

Similar to Data structure week y 5

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 QueueBalwant Gorad
 
Stacks-and-Queues.pdf
Stacks-and-Queues.pdfStacks-and-Queues.pdf
Stacks-and-Queues.pdfTobyWtf
 
Algorithm and Data Structure - Stack
Algorithm and Data Structure - StackAlgorithm and Data Structure - Stack
Algorithm and Data Structure - StackAndiNurkholis1
 
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.pptxShivam Kumar
 
STACK IN DATA STRUCTURE .pptx
STACK IN DATA STRUCTURE .pptxSTACK IN DATA STRUCTURE .pptx
STACK IN DATA STRUCTURE .pptxCHANCHALSEN2
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queueRajkiran Nadar
 
ADT STACK and Queues
ADT STACK and QueuesADT STACK and Queues
ADT STACK and QueuesBHARATH KUMAR
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptxHalid Assen
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applicationsAhsan Mansiv
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arraystameemyousaf
 

Similar to 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
 

More from karmuhtam

Devre analizi deney malzeme listesi
Devre analizi deney malzeme listesiDevre analizi deney malzeme listesi
Devre analizi deney malzeme listesikarmuhtam
 
Deney 3 ve 4
Deney 3 ve 4Deney 3 ve 4
Deney 3 ve 4karmuhtam
 
Deney 1 ve 2
Deney 1 ve 2Deney 1 ve 2
Deney 1 ve 2karmuhtam
 
Data structure week y 5 1
Data structure week y 5 1Data structure week y 5 1
Data structure week y 5 1karmuhtam
 
Data structure week y 4
Data structure week y 4Data structure week y 4
Data structure week y 4karmuhtam
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3karmuhtam
 
Data structure week 2
Data structure week 2Data structure week 2
Data structure week 2karmuhtam
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1karmuhtam
 
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ıkarmuhtam
 
12. stl örnekler
12.  stl örnekler12.  stl örnekler
12. stl örneklerkarmuhtam
 
11. stl kütüphanesi
11. stl kütüphanesi11. stl kütüphanesi
11. stl kütüphanesikarmuhtam
 
10. istisna isleme
10. istisna isleme10. istisna isleme
10. istisna islemekarmuhtam
 
9. şablonlar
9. şablonlar9. şablonlar
9. şablonlarkarmuhtam
 
8. çok biçimlilik
8. çok biçimlilik8. çok biçimlilik
8. çok biçimlilikkarmuhtam
 
7. kalıtım
7. kalıtım7. kalıtım
7. kalıtımkarmuhtam
 
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ıkkarmuhtam
 
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ıkarmuhtam
 

More from 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
 

Recently uploaded

Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 

Recently uploaded (20)

Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 

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; }