C語言 鏈棧

<code>#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

typedef int DataType; /* 鏈棧元素類型 */
typedef struct node {
\tDataType data; /* 鏈棧元素 */
\tstruct node *next; /* 鏈棧元素後繼指針 */
} SNode, *LinkStack; /* 鏈棧結點類型、鏈棧類型 */

void wait()
{
\tprintf("\\n請按任意鍵...\\n");
\tgetche();
}

int go_on()
{
\tint flag = 1;
\tchar choice;
\twhile(1)
\t{
\t\tprintf("\\n繼續嗎?[Y/N]");
\t\tchoice = getche();
\t\tif(choice == 'Y' || choice == 'y')
\t\t\tbreak;
\t\telse if(choice == 'N' || choice == 'n') {
\t\t\tflag= 0;
\t\t\tbreak;
\t\t}
\t}
\treturn (flag);
}

/* 初始化,構造一個空的帶頭結點的鏈棧 */
void Init_LinkStack(LinkStack *S)
{
\t*S = (SNode *)malloc(sizeof(SNode));
\tif(*S == NULL) {
\t\tprintf("\\n內存分配失敗.\\n");
\t\texit(-1);
\t}
\t(*S)->next = NULL;
}

/* 判斷帶頭結點的鏈棧是否為空 */
int Empty_LinkStack(LinkStack S)

{
\tif(S->next == NULL)
\t\treturn (1);
\telse
\t\treturn (0);
}

/* 入棧,插入元素e為新的棧頂元素 */
int Push_LinkStack(LinkStack S, DataType e)
{
\tSNode *q;
\tq = (SNode *) malloc(sizeof(SNode));
\tif(q == NULL) {
\t\tprintf("\\n內存分配失敗.\\n");
\t\treturn (0);
\t}
\tq->data = e;
\tq->next = S->next;
\tS->next = q;
\treturn (1);
}

void Push(LinkStack S)
{
\tDataType x;
\tint flag = 1, push_flag;
\twhile(flag) {
\t\tprintf("\\n請插入要入棧元素:");
\t\tscanf("%d", &x);
\t\tpush_flag = Push_LinkStack(S, x);
\t\tif(push_flag == 1)
\t\t\tprintf("\\n入棧成功.\\n");
\t\telse
\t\t\tprintf("\\n入棧失敗.\\n");
\t\tflag = go_on();
\t}
}

/* 出棧,刪除棧頂元素,並由*e返回其值 */
int Pop_LinkStack(LinkStack S, DataType *e)
{
\tSNode *p = S->next;
\tif(Empty_LinkStack(S)) { /* 棧空,不能出棧 */
\t\tprintf("\\n棧空,不能出棧.\\n");

\t\treturn (0);
\t}
\telse {
\t\t*e = p->data; /* 棧頂元素存入*e */
\t\tS->next = p->next;
\t\tfree(p);
\t\treturn (1);
\t}
}

void Pop(LinkStack S)
{
\tDataType x;
\tint flag = 1, pop_flag;
\twhile(flag) {
\t\tpop_flag = Pop_LinkStack(S, &x);
\t\tif(pop_flag == 1)
\t\t\tprintf("\\n出棧成功,出棧元素為:%d\\n", x);
\t\telse
\t\t\tprintf("\\n出棧失敗.\\n");
\t\tflag = go_on();
\t}
}

/* 取棧頂元素,由*e返回其值 */
int GetTop_LinkStack(LinkStack S, DataType *e)
{
\tif(Empty_LinkStack(S)) { /* 棧空,不能取棧頂元素 */
\t\tprintf("\\n棧空,不能取棧頂元素.\\n");
\t\treturn (0);
\t}
\telse {
\t\t*e = S->next->data;
\t\treturn (1);
\t}
}

/* 輸出棧頂元素 */
void Display_Top(LinkStack S)
{
\tDataType e;
\tif(Empty_LinkStack(S) == 1)
\t\tprintf("\\n棧空,沒有元素.\\n");
\telse {

\t\tGetTop_LinkStack(S, &e);
\t\tprintf("\\n棧頂元素:");
\t\tprintf("%4d\\n", e);
\t}
}

/* 輸出棧全部元素 */
void Display_LinkStack(LinkStack S)
{
\tSNode *p = S->next;
\tif(Empty_LinkStack(S) == 1)
\t\tprintf("棧空,沒有元素.\\n");
\telse {
\t\tprintf("\\n棧全部元素");
\t\tprintf("\\n棧頂棧底\\n");
\t\twhile(p != NULL) {
\t\t\tprintf("%4d", p->data);
\t\t\tp = p->next;
\t\t}
\t}
}

int main(void)
{
\tLinkStack S;
\tchar choice;
\tint flag = 1;
\tInit_LinkStack(&S);
\tdo {
\t\tprintf("\\n");
\t\tprintf("------------鏈棧(帶頭結點)------------\\n");
\t\tprintf(" 1.......入棧\\n");
\t\tprintf(" 2.......出棧\\n");
\t\tprintf(" 3.......輸出棧頂元素\\n");
\t\tprintf(" 4.......輸出全部元素\\n");
\t\tprintf(" 0.......退出\\n");
\t\tprintf("----------------------------------------\\n");
\t\tprintf("請輸入[1/2/3/4/0]: ");
\t\tchoice = getche();
\t\tswitch(choice) {
\t\t\tcase '1':
\t\t\t\tPush(S);
\t\t\t\tbreak;
\t\t\tcase '2':
\t\t\t\tPop(S);

\t\t\t\tbreak;
\t\t\tcase '3':
\t\t\t\tDisplay_Top(S);
\t\t\t\tbreak;
\t\t\tcase '4':
\t\t\t\tDisplay_LinkStack(S);
\t\t\t\tbreak;
\t\t\tcase '0':
\t\t\t\tflag = 0;
\t\t\t\tbreak;
\t\t}
\t\twait();
\t} while(flag == 1);
\t
\treturn (0);
}
/<conio.h>/<stdlib.h>/<stdio.h>/<code>


分享到:


相關文章: