Skip to content

Instantly share code, notes, and snippets.

@hamzamuric
Created May 3, 2018 14:45
Show Gist options
  • Save hamzamuric/96579af837abe4b2ad303080eeaf738a to your computer and use it in GitHub Desktop.
Save hamzamuric/96579af837abe4b2ad303080eeaf738a to your computer and use it in GitHub Desktop.
Stek na svaki nacin :D
/* Implementacija dva steka
* preko jednog niza
*/
#include <stdio.h>
#define SIZE 10
int Stack[SIZE], top1 = -1, top2 = SIZE;
// Da li je stek pun
int isFull()
{
if (top2 - top1 == 1)
return 1;
return 0;
}
// Da li je stek prazan
int isEmpty(int stno)
{
switch (stno) {
case 1: if (top1 == -1) return 1;
else return 0;
break;
case 2: if (top2 == SIZE) return 1;
else return 0;
break;
default: printf("Error\n");
}
}
// Dodaj na vrh steka
void push(int stno, int x)
{
switch (stno) {
case 1:
if (!isFull())
Stack[++top1] = x;
break;
case 2:
if (!isFull())
Stack[--top2] = x;
break;
default:
printf("Incorrect stack number\n");
}
}
// Ukloni sa vrha steka
int pop(int stno)
{
switch (stno) {
case 1:
if (top1 != -1)
return Stack[top1--];
break;
case 2:
if (top2 != SIZE)
return Stack[top2++];
break;
default:
printf("Incorrect stack number\n");
}
}
// Prikaz steka
void display(int stno)
{
int i;
if(isEmpty(stno))
{
printf("empty stack\n");
return;
}
if (stno == 1)
{
for (i = 0; i <= top1; i++)
printf("%d <- ", Stack[i]);
printf("top\n");
}
else
{
for (i = SIZE - 1; i >= top2; i--)
printf("%d <- ", Stack[i]);
printf("top\n");
}
}
int main()
{
push(1, 5);
push(1, 3);
push(1, 6);
push(2, 4);
push(2, 7);
display(1);
display(2);
pop(1);
pop(2);
display(1);
display(2);
return 0;
}
/* Implementacija steka
* preko ulancane liste
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *top, *temp; // Temp zato sto, zasto da ne
// Dodaje na vrh steka
void push(int x)
{
temp = (struct node*) malloc(sizeof(struct node));
temp->data = x;
temp->link = top;
top = temp;
}
// Uklanja sa vrha steka
int pop()
{
if (top == NULL)
{
printf("Empty\n");
return -1;
}
int data = top->data;
top = top->link;
return data;
}
// Prikaz steka
void display()
{
if (top == NULL)
{
printf("Empty\n");
return;
}
temp = top;
printf("TOP -> ");
while (temp)
{
printf(" %d ->", temp->data);
temp = temp->link;
}
printf(" NULL\n");
}
int main()
{
display();
push(5);
push(7);
push(8);
push(1);
push(3);
display();
printf("After removing %d\n", pop());
display();
return 0;
}
#include <stdio.h>
#define SIZE 5
/* Stack je obicni niz, nema razloga za strah
* Top je index vrha u nizu (dokle je niz pun)
*/
int Stack[SIZE], top = -1;
// Da li je stek pun
int isFull()
{
if (top == SIZE - 1)
return 1;
return 0;
}
// Da li je stek prazan
int isEmpty()
{
if (top == -1)
return 1;
return 0;
}
// Dodaje na vrh
void push(int x)
{
if (isFull())
{
printf("Stack Overflow!!!\n");
return;
}
else
{
Stack[++top] = x;
}
}
// Uklanja sa vrha i vraca uklonjenu vrednost
int pop()
{
if (isEmpty())
{
printf("Stack Empty\n");
return -1;
}
else
{
return Stack[top--];
}
}
// Vraca element sa vrha, a ne uklanja ga
int peek()
{
if (isEmpty())
{
printf("Stack Empty\n");
return 0;
}
else
{
return Stack[top];
}
}
// Prikaz steka
void display()
{
if (isEmpty())
{
printf("Stack Empty\n");
return;
}
else
{
for (int i = 0; i <= top; i++)
printf("%d ", Stack[i]);
printf("^ TOP\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment