Veri Yapilari Stacks Stacks Queues Lists Stack nedir Stack de tanimli islemler Push Pop Empty Full Inialization Stack in gerceklestirilmesi 10/7/2004 Veri yapilari 3.1 10/7/2004 Veri yapilari 3.2 Stack Push 10/7/2004 Veri yapilari 3.3 10/7/2004 Veri yapilari 3.4
Pop Stack Islemleri Bu stack ten cikarilir Stack i initialize et Stack in ustundeki item i pop et Stack in uzerine bir item push et Stack empty controlu Stack full controlu Stack in uzunlugunu bul Stack i clear et (sil) 10/7/2004 Veri yapilari 3.5 10/7/2004 Veri yapilari 3.6 Stack ozellikleri Pop ve push islemleri stack in ustunde yapilir Last-In First Out (LIFO) Fonksiyonlari cagirma esnasinda kullanilir Stack e fonksiyon parametreleri ve fonksiyonda tanimli local variable lar fonksiyon cagrimi esnasinda stack e itilirler Recursive fonksiyonlari gerceklestirmede kullanilir Postfix expression larin hesaplanmasinda 10/7/2004 Veri yapilari 3.7 Postfix ve Infix notasyonlari Bir cok compiler lar programlaradaki infix notasyonunda yazili expressionlari postfix notasyonuna cevirerek bellekte saklar Infix notation: operator ilgili operandlarin arasinda yazilir ornek: (3+4) x 2 Postfix notation: operator ilgili operandlardan sonra gelir ornek: 3 4 + 2 x Bu yazimda parantezlere gerek yoktur 10/7/2004 Veri yapilari 3.8
Postfix expression larin stack yardimiyle hesaplanmasi ornek Stack postfix expression larin hesaplanmasinda kullanilabilir Empty stack le baslanip, postfix expression soldan saga dogru taranir. Bir operand karsilasildiginda o operand stack e itilir. Bir operator ile karsilasildiginda ise o operatorun gerek duydugu sayida operand stack ten cekilir Daha sonra pop edilen iki operanda operator uygulanir ve bulunan sonuc tekrar stack e itilir. Yukaridaki uc adim postfix expression in tamami taranincaya kadar devam edilir 3 4 + 2 x Symbol processed initial 3 4 + 2 x Stack 3 3 4 7 7 2 14 Actions push 3 push 4 pop 4, pop 3; 3+4 = 7 push 7 push 2 pop 2, pop 7; 7x2 = 14 push 14 10/7/2004 Veri yapilari 3.9 10/7/2004 Veri yapilari 3.10 Stack Implementation #define MAXSTACK 20 Stack: top: top index : 2 1 0 int top; float entry[maxstack]; 10/7/2004 Veri yapilari 3.11 struct StackRec int top; float entry[maxstack]; ; typedef struct StackRec Stack;; entry: 10/7/2004 Veri yapilari 3.12..
#ifndef STACKH #define STACKH #include <stdbool.h> #define MAXSTACK 20 struct StackRec int top; float entry[maxstack]; ; typedef struct StackRec Stack; void intializestack(stack* stackptr); bool stackempty(const Stack* stackptr); bool stackfull(const Stack* stackptr); void push(stack* stackptr, float item); float pop(stack* stackptr); #include <stdio.h> #include <stdlib.h> #include stack.h void initializestack(stack* stackptr) stackptr -> top = -1; stackptr: addr of Stack Stack top: entry: -1.... #endif 10/7/2004 Veri yapilari 3.13 10/7/2004 Veri yapilari 3.14 bool stackempty(const Stack* stackptr) if (stackptr->top < 0) return true; else return false; bool stackfull(const Stack* stackptr) if (stackptr->top >= MAXSTACK-1) return true; else return false; 10/7/2004 Veri yapilari 3.15 void push(stack* stackptr, float item) if (stackfull(stackptr)) fprintf(stderr, Stack is full\n ); exit(1); else stackptr->top++; stackptr->entry[stackptr->top] = item; 10/7/2004 Veri yapilari 3.16
float pop(stack* stackptr) float item; if (stackempty(stackptr)) fprintf(stderr, Stack is empty\n ); exit(1); else item = stackptr->entry[stackptr->top]; stackptr->top--; Sequence: -1.5 2.3 6.7 - once elementleri teker teker - stack e it. - daha sonra elementleri stack den cek. return item; 10/7/2004 Veri yapilari 3.17 10/7/2004 Veri yapilari 3.18 2.3 6.7 6.7-1.5 2.3-1.5 10/7/2004 Veri yapilari 3.19 10/7/2004 Veri yapilari 3.20
6.7 2.3-1.5 2.3-1.5 6.7 10/7/2004 Veri yapilari 3.21 10/7/2004 Veri yapilari 3.22-1.5 6.7 2.3 10/7/2004 Veri yapilari 3.23 6.7 2.3-1.5 10/7/2004 Veri yapilari 3.24
module reverse () initialize the Stack loop input nextnumber if (end of input) then exit loop if (Stack is not full) then push nextnumber onto Stack loop if (Stack is empty) then exit loop pop nextnumber off the Stack output nextnumber 10/7/2004 Veri yapilari 3.25 #include <stdio.h> #include stack.h int main() Stack thestack; float next; initializestack(&thestack); printf( Enter number sequence: ); while (scanf( %f, &next)!= EOF) if (!stackfull(&thestack)) push(&thestack, next); while (!stackempty(&thestack)) next = pop(&thestack); printf( %f, next); printf( \n ); 10/7/2004 Veri yapilari 3.26 Linked-List Implementation of Stack Stack linked-list data structure i olarak temsil edilebilir Push ve pop islemleri liste nin basindan yapilir (performance acisindan) Dolayisiyle liste backward olarak temsil edilmeli L L a b c x a b c push(x,l) 10/7/2004 Veri yapilari 3.27 10/7/2004 Veri yapilari 3.28
L L a b c b c pop(l,x) typedef struct CELL *STACK; struct CELL int element; STACK next; ; enum BOOLEAN TRUE,FALSE; void initialize(stack *ps) *ps = NULL; enum BOOLEAN isempty(stack *ps) return (*ps == NULL) enum BOOLEAN isfull(stack *ps) return FALSE; enum BOOLEAN pop(stack *ps, int *px) if ( *ps == NULL) return FALSE; else *px = *ps->element *ps = *ps ->next; return TRUE; enum BOOLEAN push (int x, STACK *ps) STACK newcell; newcell = (STACK) malloc(sizeof(struct CELL)); newcell->element = x; newcell->next = *ps; *ps = newcell; return TRUE; 10/7/2004 Veri yapilari 3.29 10/7/2004 Veri yapilari 3.30