C 數據結構:鏈棧和順序棧

棧的ADT

  • CreateStack():創建棧
  • IsEmpty(StackList S):判斷是否爲空棧
  • EmptyStack(StackList S):置空棧
  • Push(StackList S):入棧
  • Pop(StackList S):出棧
  • Top(StackList S):獲得棧頂元素

鏈棧(基於鏈表建立的棧)

鏈棧沒有溢出問題,因爲鏈棧不能定義棧的空間大小。

全部代碼

#include<stdio.h>
#include<stdlib.h>
typedef struct Stack
{
	int value;
	struct Stack * next;
}Stack, * StackList;
StackList CreateStack()
{
	///創建棧
	StackList head;
	head = (StackList)malloc(sizeof(Stack));
	if(head == NULL)
		printf("空間不足!\n");
	head->next = NULL;
	return head;
}
bool IsEmpty(StackList S)
{
	///判斷是否爲空棧
	if(S->next == NULL)
		return true;
	else
		return false;
}
void EmptyStack(StackList S)
{
	///置空棧
	while(!IsEmpty(S))
	{
		Stack *node = S->next;
		S->next = S->next->next;
		free(node);
	}
}
void Push(StackList S)
{
	///入棧
	Stack *node;
	while(true)
	{
		printf("請輸入入棧元素(n結束入棧):\n");
		node = (Stack *)malloc(sizeof(Stack));
		scanf("%d",&node->value);
		node->next = S->next;
		S->next = node;
		printf("入棧成功!!\n");
		getchar();
		printf("是否繼續入棧:");
		char c = getchar();
		if(c == 'n')
		{
			printf("結束入棧!!\n");
			break;
		}
		else
			continue;
	}
}
void Pop(StackList S)
{
	///出棧
	while(!IsEmpty(S))
	{
		Stack *node = S->next;
		S->next = S->next->next;
		printf("%d已出棧\n是否繼續出棧",node->value);
		free(node);
		getchar();
		if(getchar() == 'y')
			continue;
		else
		{
			printf("出棧結束!!\n");
			break;
		}
	}
	if(IsEmpty(S))	printf("Error:空棧警告\n");
}
void Top(StackList S)
{
	///打印棧頂元素
	if(IsEmpty(S))
		printf("Error:空棧警告\n");
	else
		printf("棧頂元素爲:%d\n",S->next->value);
}
int main()
{
	StackList S;
	S = CreateStack();
	Push(S);

	Top(S);

	Pop(S);

	Top(S);
	return 0;
}

順序棧(基於數組建立的棧)

全部代碼

#include<stdio.h>
#include<stdlib.h>
#define MinStackSize 20
typedef struct StackList
{
	int Capacity;	//棧的容量
	int TopOfStack;	//棧頂所在的位置
	int * Array;
}*Stack;
Stack CreateStack(int MaxElements)
{
	///創建棧
	Stack S;
	if(MaxElements > MinStackSize)
	{
		printf("棧空間太小!!\n");
		return 0;
	}
	S = (Stack)malloc(sizeof(StackList));
	if(S == NULL)
	{
		printf("創建失敗!!\n");
		return 0;
	}
	S->Array = (int *)malloc(sizeof(int) * MaxElements);
	if(S->Array == NULL)
	{
		printf("空間不足!!\n");
		return 0;
	}
	S->Capacity = MaxElements;
	S->TopOfStack = -1;	//置空棧
	printf("創建成功!!\n");
	return S;
}

int IsEmpty(Stack S)
{
	///判斷是否爲空棧
	return S->TopOfStack == -1;
}

int IsFull(Stack S)
{
	///判斷是否即將溢出
	return S->TopOfStack+1 == S->Capacity; 
}

void Push(Stack S, int x)
{
	///入棧
	int i = 0;
	if(IsFull(S))
		printf("無法插入,空間不足!!\n");
	else
	{
		S->Array[++S->TopOfStack] = x;
	}
}

void Pop(Stack S)
{
	///出棧
	if(IsEmpty(S))
		printf("空棧警告!!\n");
	else
	{
		printf("%d已出棧!!\n",S->Array[S->TopOfStack]);
		S->TopOfStack--;
	}
}

void Top(Stack S)
{
	///打印棧頂元素
	if(IsEmpty(S))
		printf("空棧警告!!\n");
	else
		printf("棧頂元素爲:%d\n",S->Array[S->TopOfStack]);
}
int main()
{
	//順序棧
	Stack S = NULL;
	int x;
	printf("請輸入創建棧的大小:");
	scanf("%d",&x);
	S = CreateStack(x);
	while(true){

		printf("請輸入入棧元素:");
		scanf("%d",&x);
		Push(S,x);
		if(IsFull(S))
			break;
	}
	Top(S);
	Pop(S);
	Top(S);
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章