Добавил:
Fragga
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:лабораторки по ОС препод Челноков / 1семестр / wi2 / vera / 3 / stack
.c#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//stv - stack value
#define stv char
//pos - current position in stack
long pos=0;
//stck - just stack ...
stv *stck=NULL;
void overflow(void)
{
printf("I'm sorry , but stack is overflow !!!...\n");
}
//procedure NEW_STCK creates a first stack segment
int create(void)
{
stck=(stv *)malloc(sizeof(stv));
if (stck!=NULL)
return 0;
else
{
overflow();
return -1;
}
}
void dispose(void)
{
free(stck);
}
int push(stv element)
{
stck[pos]=element;
pos++;
stck=(stv *)realloc(stck,sizeof(stv));
if (stck!=NULL)
return 0;
else;
{
overflow();
return -1;
}
}
stv pop(void)
{
stv temp;
pos--;
temp=stck[pos];
return temp;
}
int empty(void)
{
if (pos==0)
return 1;
else
return 0;
}
int main(void)
{
stv q;
system("tput clear");
printf("Creating first segment...\n");
create();
printf("Feeling first 100 values ...\n");
for (q=0;q<=100;q++)
if (push(q)!=0)
printf("Can not continue...\nSome errors in tack!");
printf("geting pushed values from stack...\n");
for (q=0;q<=100;q++)
{
printf("value is %d ",pop());
if (empty())
printf("stack is EMPTY\n");
else
printf("stack isn't EMPTY\n");
}
printf("Disposing stack\n");
dispose();
printf("Finished.\nPress ENTER to EXIT!\n");
getchar();
return 0;
}