链栈的基本操作及回文
2014-03-11 20:32
323 查看
头文件linkStack.h
linkstack.cpp
主函数:
#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; }