栈-----------(数据结构作业)

栈作为一种数据结构,是一种只能在一端进行插入和删除操作的特殊线性表。它按照后进先出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据,最后一个数据被第一个读出来。栈具有记忆作用,对栈的插入与删除操作中,不需要改变栈底指针。

栈是允许在同一端进行插入和删除操作的特殊线性表。允许进行插入和删除操作的一端称为栈顶,另一端为栈底;栈底固定,而栈顶浮动;栈中元素个数为零时称为空栈。插入一般称为进栈,删除则称为退栈。栈也称为后进先出表。

 压栈

void push(CharStackPtr paraStackPtr, int paraValue) 
{
    // Step 1. Space check. 
    if (paraStackPtr->top >= MAXSIZE - 1) 
	{
        printf("Cannot push element: stack full.\r\n");
        return;
    }//Of if

    // Step 2. Update the top.
	paraStackPtr->top ++;

	// Step 3. Push element.
    paraStackPtr->data[paraStackPtr->top] = paraValue;
}// Of push

出栈

char pop(CharStackPtr paraStackPtr)
{
	// Step 1. Space check.
	if(paraStackPtr->top < 0)
	{
		printf("Cannot pop element: stack empty.\r\n");
		return '\0';
	}
	// Step 2. Update the top.
	paraStackPtr->top --;
	// Step 3. Push element.
	return paraStackPtr->data[paraStackPtr->top + 1]; 
}

总代码

#include <stdio.h>
#include <malloc.h>
 
#define STACK_MAX_SIZE 10
 
 
// Linear stack of integers. The key is data.
 
typedef struct CharStack
{
	int top;
	
	int data[STACK_MAX_SIZE];//The maximum length is fixed.  
} *CharStackPtr;
 
// Output the stack.
 
void outputStack(CharStackPtr paraStack)
{
	int i;
	for (i = 0;i <= paraStack->top; i++)
	{
		printf("%c",paraStack->data[i]);
	}//of for i
	
	printf("\r\n");
} // of outputStack
 
// Initalize an empty char stack.No error checing for this function.
// @param paraStacPtr The pointer to the stack.It must be a pointer to change the stack.
// @param paraValus An int array storing all element.
 
CharStackPtr charStackInit()
{
	CharStackPtr resultPtr = (CharStackPtr)malloc(sizeof(CharStack));
	resultPtr->top = -1;
	
	return resultPtr; 
}// of charStackInit
 
// Push an element to the stack.
// @param paraValue The value to be pushed.
 
void push(CharStackPtr paraStackPtr,int paraValue)
{
	// Step 1.Space check.
	if (paraStackPtr->top >= STACK_MAX_SIZE -1)
	{
		printf("Cannot push element: stack full.\r\n");
		return;
	}// of if
	
	// Step 2. Uptade The top.
	paraStackPtr->top ++;
	
	// Step 3. Push element.
	paraStackPtr->data[paraStackPtr->top] = paraValue; 	
}//of push 
 
// Pop an element from the stack.
// @return The poped value. 
 
char pop(CharStackPtr paraStackPtr)
{
	//step 1. Space Check.
	if (paraStackPtr->top < 0)
	{
		printf("Connot pop element;stack empty.\r\n");
		return '\0';
	}//of if
	
	// Step 2. Update the top.  
	paraStackPtr->top --;
	
	//Step 3. Push element.
	return paraStackPtr->data[paraStackPtr->top + 1];
}// of pop
 
// Test the push function.
void pushPopTest()
{
	printf("------pushPopTest begins.-------\r\n");
	
	//Initialize.
	CharStackPtr tempStack = charStackInit();
	printf("After initialization,the stack is:");
	outputStack(tempStack);
	
	//Pop.
	char  ch;
	for( ch == 'a';ch <'m';ch ++)
	{
		printf("Pushing %c.\r\n",ch);
		
		push(tempStack,ch);
		outputStack(tempStack);
	}//of for i 
	printf("----pushPopTest ends.-----\r\n");
 } // of pushPopTest
 
// The entrance  
 
void main()
{
	pushPopTest(); 
 } // of main

运行结果

------pushPopTest begins.-------
After initialization,the stack is:
Pushing  .

Pushing .
 
Pushing .
 
Pushing .
 
Pushing .
 
Pushing .
 
Pushing .
 
Pushing .
 
Pushing.
 
Pushing         .
 
Pushing
.
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
.ushing
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing .
Cannot push element: stack full.
 
Pushing  .
Cannot push element: stack full.
 
Pushing !.
Cannot push element: stack full.
 
Pushing ".
Cannot push element: stack full.
 
Pushing #.
Cannot push element: stack full.
 
Pushing $.
Cannot push element: stack full.
 
Pushing %.
Cannot push element: stack full.
 
Pushing &.
Cannot push element: stack full.
 
Pushing '.
Cannot push element: stack full.
 
Pushing (.
Cannot push element: stack full.
 
Pushing ).
Cannot push element: stack full.
 
Pushing *.
Cannot push element: stack full.
 
Pushing +.
Cannot push element: stack full.
 
Pushing ,.
Cannot push element: stack full.
 
Pushing -.
Cannot push element: stack full.
 
Pushing ..
Cannot push element: stack full.
 
Pushing /.
Cannot push element: stack full.
 
Pushing 0.
Cannot push element: stack full.
 
Pushing 1.
Cannot push element: stack full.
 
Pushing 2.
Cannot push element: stack full.
 
Pushing 3.
Cannot push element: stack full.
 
Pushing 4.
Cannot push element: stack full.
 
Pushing 5.
Cannot push element: stack full.
 
Pushing 6.
Cannot push element: stack full.
 
Pushing 7.
Cannot push element: stack full.
 
Pushing 8.
Cannot push element: stack full.
 
Pushing 9.
Cannot push element: stack full.
 
Pushing :.
Cannot push element: stack full.
 
Pushing ;.
Cannot push element: stack full.
 
Pushing <.
Cannot push element: stack full.
 
Pushing =.
Cannot push element: stack full.
 
Pushing >.
Cannot push element: stack full.
 
Pushing ?.
Cannot push element: stack full.
 
Pushing @.
Cannot push element: stack full.
 
Pushing A.
Cannot push element: stack full.
 
Pushing B.
Cannot push element: stack full.
 
Pushing C.
Cannot push element: stack full.
 
Pushing D.
Cannot push element: stack full.
 
Pushing E.
Cannot push element: stack full.
 
Pushing F.
Cannot push element: stack full.
 
Pushing G.
Cannot push element: stack full.
 
Pushing H.
Cannot push element: stack full.
 
Pushing I.
Cannot push element: stack full.
 
Pushing J.
Cannot push element: stack full.
 
Pushing K.
Cannot push element: stack full.
 
Pushing L.
Cannot push element: stack full.
 
Pushing M.
Cannot push element: stack full.
 
Pushing N.
Cannot push element: stack full.
 
Pushing O.
Cannot push element: stack full.
 
Pushing P.
Cannot push element: stack full.
 
Pushing Q.
Cannot push element: stack full.
 
Pushing R.
Cannot push element: stack full.
 
Pushing S.
Cannot push element: stack full.
 
Pushing T.
Cannot push element: stack full.
 
Pushing U.
Cannot push element: stack full.
 
Pushing V.
Cannot push element: stack full.
 
Pushing W.
Cannot push element: stack full.
 
Pushing X.
Cannot push element: stack full.
 
Pushing Y.
Cannot push element: stack full.
 
Pushing Z.
Cannot push element: stack full.
 
Pushing [.
Cannot push element: stack full.
 
Pushing \.
Cannot push element: stack full.
 
Pushing ].
Cannot push element: stack full.
 
Pushing ^.
Cannot push element: stack full.
 
Pushing _.
Cannot push element: stack full.
 
Pushing `.
Cannot push element: stack full.
 
Pushing a.
Cannot push element: stack full.
 
Pushing b.
Cannot push element: stack full.
 
Pushing c.
Cannot push element: stack full.
 
Pushing d.
Cannot push element: stack full.
 
Pushing e.
Cannot push element: stack full.
 
Pushing f.
Cannot push element: stack full.
 
Pushing g.
Cannot push element: stack full.
 
Pushing h.
Cannot push element: stack full.
 
Pushing i.
Cannot push element: stack full.
 
Pushing j.
Cannot push element: stack full.
 
Pushing k.
Cannot push element: stack full.
 
Pushing l.
Cannot push element: stack full.
 
----pushPopTest ends.-----

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值