頭文件linkStack.h
#ifndef LINKSTACK_H_INCLUDED
#define LINKSTACK_H_INCLUDED
typedef int Status;
typedef int SElemType;
#define OK 1
#define ERROR -1
typedef struct Node
{
SElemType data;
Node *next;
}Node,*LinkStack;
Status InitLinkStack(LinkStack top);//構造一個空棧
Status DestroyLinkStack(LinkStack top);//銷燬棧,使棧不再存在
Status ClearLinkStack(LinkStack top);//把棧置爲空棧
Status StackEmpty(LinkStack top);//判斷棧是否爲空棧
int StackLength(LinkStack top);//返回棧的元素個數
SElemType GetTop(LinkStack top);//取棧頂元素
LinkStack Push(LinkStack top,SElemType e);//插入e爲棧頂元素
LinkStack Pop(LinkStack top);//元素出棧
void PrintStack(LinkStack top);//打印棧中元素
int Palindrome(LinkStack LS,char *ch);//迴文判斷
void menu();
#endif // LINKSTACK_H_INCLUDED
linkstack.cpp
#include"linkStack.h"
using namespace std;
#include<cstdlib>
#include<stdio.h>
#include<cstring>
//構造一個空棧
Status InitLinkStack(LinkStack top)
{
top->next = NULL;
return OK;
}
//銷燬棧,使棧不再存在
Status DestroyLinkStack(LinkStack top)
{
Node *p;
p = top->next;
while(p)
{
free(top);
top = p;
p = p->next;
}
free(top);
printf("destroy is success\n");
}
//把棧置爲空棧
Status ClearLinkStack(LinkStack top)
{
if(StackEmpty(top))
{
printf("the stack is already empty,it needn't clear\n");
return ERROR;
}
while(!StackEmpty(top))
{
Pop(top);
}
printf("OK,all the elements are clear\n");
}
//判斷棧是否爲空棧
Status StackEmpty(LinkStack top)
{
if(NULL == top->next)
{
// printf("the stack is empty\n");
return 1;
}
else
{
// printf("the stack is not empty\n");
return 0;
}
}
//返回棧的元素個數
int StackLength(LinkStack top)
{
int len = 0;
while(top->next)
{
len++;
top = top->next;
}
return len;
}
//取棧頂元素
SElemType GetTop(LinkStack top)
{
if(!StackEmpty(top))
return top->next->data;
}
//插入e爲棧頂元素
LinkStack Push(LinkStack top,SElemType e)
{
Node *p;
p = (Node *)malloc(sizeof(Node));
if(!p)
{
printf("allocation is failed\n");
}
p->data = e;
p->next = top->next;
top->next = p;
//printf("push is success\n");
return top->next;
}
//元素出棧
LinkStack Pop(LinkStack top)
{
if(StackEmpty(top) == 1)
{
printf("no elements to pop\n");
return NULL;
}
Node *p;
int x;
p = top->next;
x = p->data;
top->next = p->next;
free(p);
// printf("pop is success,the elements is%d\n",x);
return top->next;
}
//打印棧中元素
void PrintStack(LinkStack top)
{
if(top->next == NULL)
{
printf("the stack is empty,no elements to print\n");
}
while(top->next)
{
printf("%d\n",top->next->data);
top = top->next;
}
}
//迴文判斷
int Palindrome(LinkStack LS,char *ch)
{
int i,m,flag = 0;
m = strlen(ch);//計算字符串長度
LS = (LinkStack)malloc(sizeof(Node));
InitLinkStack(LS);
for(i = 0; i < m / 2; i++)
{
Push(LS,ch[i]);
}
if(m % 2 == 1)//m爲奇數,跳過中間的字符
i++;
while(i < m)
{
if(GetTop(LS)== ch[i])
Pop(LS);
else
return 0;
i++;
}
return 1;
}
void menu()
{
printf("如果您剛運行程序,請您先進行入棧操作\n");
printf("0--------退出程序\n");
printf("1--------元素入棧\n");
printf("2--------輸出棧的長度\n");
printf("3--------取棧頂元素\n");
printf("4--------元素出棧\n");
printf("5--------清空鏈棧\n");
printf("6--------銷燬鏈棧\n");
printf("7--------判斷棧是否爲空\n");
printf("8--------打印棧中元素\n");
printf("9--------判斷是否爲迴文\n");
}
主函數:
#include<iostream>
#include<stdio.h>
#include<cstdlib>
#include"linkStack.h"
using namespace std;
int main()
{
LinkStack test;
test = (LinkStack)malloc(sizeof(Node));
InitLinkStack(test);
menu();
int i,value;
int length;
printf("please choice(0~9):");
scanf("%d",&i);
printf("\n");
while(i)
{
switch(i)
{
case 1:
printf("please input what you what to push(0 to end):");
scanf("%d",&value);
while(value != 0)
{
Push(test,value);
scanf("%d",&value);
}
PrintStack(test);
break;
case 2://輸出棧的長度
if(StackEmpty(test) == 1)
printf("the stack is empty,length is 0\n");
else
{
printf("the length is %d\n",StackLength(test));
}
break;
case 3://取棧頂元素
printf("the top element is %d\n",GetTop(test));
break;
case 4://元素出棧
Pop(test);
break;
case 5://清空鏈棧
ClearLinkStack(test);
break;
case 6://銷燬鏈棧
DestroyLinkStack(test);
break;
case 7://判斷棧是否爲空
if(StackEmpty(test))
printf("the stack is empty\n");
else
printf("the stack is not empty\n");
break;
case 8:
PrintStack(test);
break;
case 9:
LinkStack LS;
char *ch;
int i;
printf("please input the string:");
scanf("%s",ch);
i = Palindrome(LS,ch);
if(i!=0)
printf("是迴文\n");
else
printf("不是迴文\n");
break;
default:
printf("your choice is wrong,please put in(0~9)");
break;
}
menu();
scanf("%d",&i);
}
return 0;
}