sub6.cpp

来自「STRUCTURI DE DATE SI ALGORITMI」· C++ 代码 · 共 149 行

CPP
149
字号
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

typedef int Atom;
struct Element
	{
	Atom data;
	Element* link;
	};
typedef Element* Stack;

void init(Stack& S);
int isEmpty(Stack S);
void push(Stack& S,Atom a);
void pop(Stack& S);
Atom top(Stack& S);
void afis(Stack S);
Stack copy(Stack S);
void clear(Stack& S);
void eroare(char s[]);

void main()
{
char s[40];
int c,n,i;
Stack S1;
clrscr();
init(S1);
printf("\nIntrodu numerele :\n");
c=0;
do
	{
	scanf("%d",&n);
	c++;
	while(n<0)
		{
		push(S1,n);
		break;
		}
	if(n>0)
		if(n>=c)
			eroare("Nu puteti scoate mai multe numere \
						      decit ati introdus!");
		else
			{
			printf("\nAm extras :");
			for(i=1;i<=n;i++)
				{
				printf("  %d ",top(S1));
				pop(S1);
				}
			}
	else
		if(n==0)
			{
			printf("\n\nContinutul stivei auxiliare :");
			afis(copy(S1));
			printf("\n");
			}
	}while(c!=10);
printf("\nElementele stivei sunt :\n");
afis(S1);
getch();
}

void init(Stack& S)
{
	S=0;
}


int isEmpty(Stack S)
{
	return(S==0);
}

void push(Stack& S,Atom a)
{
Element *T;
T=new Element;
T->data=a;
T->link=S;
S=T;
}

void pop(Stack& S)
{
Element *T;
T=S;
while(T!=0)
	{
	T=S;
	S=S->link;
	delete(T);
	break;
	}
}

Atom top(Stack& S)
{
Stack T;
T=S;
while(T!=0)
	{
	return(T->data);
	T=T->link;
	}
}


void afis(Stack S)
{
Stack T;
T=S;
while(T!=0)
	{
	if(isEmpty(T))
		printf("\nStiva VIDA!");
	else
		printf("  %d",top(T));
	T=T->link;
	}
}

void clear(Stack& S)
{
	S=0;
}

Stack copy(Stack S)
{
Stack T,V;
T=S;
while(T!=0)
	{
	V->data=T->data;
	V->link=T->link;
	return(V);
	T=T->link;
	}
}

void eroare(char s[])
{
printf("\n\tEROARE!   %s ",s);
getch();
exit(1);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?