c语言简单题目,高手解答下
发布网友
发布时间:2024-09-25 18:53
我来回答
共3个回答
热心网友
时间:2024-09-28 21:40
//这个是数组实现的,无需用到isEmpty()函数
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int stack[128] = {0};
int length = -1;
void Push(int a)
{
if(length < 127)
{
length ++;
stack[length] = a;
printf("成功,输入的元素是:%d\n",a);
}
else
printf("失败,堆栈已满\n");
}
void Pop()
{
if(length >= 0)
{
printf("成功,取出的元素是:%d\n",stack[length]);
length --;
}
else
printf("失败,堆栈内无元素");
}
void Clear()
{
length = -1;
printf("成功清空堆栈\n");
}
int main()
{
char aChar;
int aInt, status;
status = 1;
do
{
printf("What to do?(p(Pop)/u(pUsh)/c(Clear)/q(Quit)):\n");
scanf("%c", &aChar);
if(aChar == 'u')
{
printf("Please enter a number:");
scanf("%d", &aInt);
printf("此处压栈\n");
Push(aInt);
}
else if(aChar == 'p')
{
printf("此处弹栈\n");
Pop();
}
else if(aChar == 'c')
{
printf("此处清空\n");
Clear();
}
else if(aChar == 'q')
{
printf("退出\n");
break;
}
fflush(stdin);
}while(status);
return 0;
}
//这个是链表实现的
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct list
{
int data;
struct list * next;
}*List;
List stack;
void build()
{
stack = (List)malloc(sizeof(list));
stack->data = 0;
stack->next = NULL;
}
bool isEmpty()
{
if(stack->next == NULL)
return true;
else
return false;
}
void Push(int a)
{
if(stack->data < 128)
{
List q,p;
p = (List)malloc(sizeof(list));
p->data = a;
p->next = stack->next;
stack->next = p;
printf("成功,输入的元素是:%d\n",p->data);
}
else
printf("失败,堆栈已满\n");
}
void Pop()
{
if(!isEmpty())
{
List p = stack->next,q;
printf("成功,取出的元素是:%d\n",p->data);
stack->data --;
q = stack;
q->next = p->next;
free(p);
}
else
printf("失败,堆栈内无元素");
}
void Clear()
{
List p,q,r;
p = stack;
if(p->next)
{
while(p->next->next)
{
r = stack;
q = r->next;
r->next = q->next;
free(q);
}
}
p = stack->next;
free(p);
stack->next = NULL;
printf("成功清空堆栈\n");
}
int main()
{
char aChar;
int aInt, status;
status = 1;
build();
do
{
printf("What to do?(p(Pop)/u(pUsh)/c(Clear)/q(Quit)):\n");
scanf("%c", &aChar);
if(aChar == 'u')
{
printf("Please enter a number:");
scanf("%d", &aInt);
printf("此处压栈\n");
Push(aInt);
}
else if(aChar == 'p')
{
printf("此处弹栈\n");
Pop();
}
else if(aChar == 'c')
{
printf("此处清空\n");
Clear();
}
else if(aChar == 'q')
{
printf("退出\n");
break;
}
fflush(stdin);
}while(status);
return 0;
}
热心网友
时间:2024-09-28 21:41
给你一个我写的,你参考一下:
这是一个堆栈的模板类; 你可以将 Type 定义为你需要的任何类型,希望对你有帮助, 如果还有不明白的地方加我
QQ: 1026613980 注明 [编程].
Stack.h
//////////////////////////////////////////////////////////////////////////////////////*/
#include <assert.h>
#include <iomanip.h>
#include <stdlib.h>
template <class Type> class Stack
{
public:
Stack( int size = 10 );
~Stack() { delete []elements; }
void Push( const Type &value );
Type Pop(void);
Type GetTop(void) const;
void MakeEmpty(void) { top = -1; };
bool IsEmpty(void) const { return top == -1; }
bool IsFull(void) const { return top == maxSize - 1; }
void Traverse(void) const;
private:
int top;
Type *elements;
int maxSize;
};
////////////////////////////////////////////////////////////////////////////////////////
//
//
// Implementation of member functions
//
//
////////////////////////////////////////////////////////////////////////////////////////
template <class Type> Stack<Type>::Stack( int size )
{
if ( size < 1 )
{
cerr << "Illegal Stack Size" << endl;
exit(1);
}
maxSize = size;
top = -1;
elements = new Type[maxSize];
assert( NULL != elements );
}
template <class Type> void Stack<Type>::Push( const Type &value )
{
assert( !IsFull() );
elements[++top] = value;
}
template <class Type> Type Stack<Type>::Pop(void)
{
assert( !IsEmpty() );
return elements[top--];
}
template <class Type> Type Stack<Type>::GetTop(void) const
{
assert( !IsEmpty() );
return elements[top];
}
template <class Type> void Stack<Type>::Traverse(void) const
{
assert( !IsEmpty() );
for ( int i = 0; i <= top; i++ )
{
cout << elements[i] << endl;
}
return;
}
热心网友
时间:2024-09-28 21:43
哇,这还简单啊。。。真得要高手才行了