有人问用C言语用栈实现倒序输出字符串。
示例中用空格作为字符串的分隔符。
- #include <stdio.h>
- #include <stdlib.h>
-
-
- #define ERR_VALUE -1 //参数错误
- #define ERR_MEMORY -2//内存错误
- #define OK 0
- #define TRUE 1
- #define FALSE 0
-
-
-
-
- #define STRLEN 88
- typedef struct _MyStack
- {
- char *str;
- struct _MyStack *next;
- }MyStack;
- typedef MyStack * pMyStack;
-
-
-
-
- //创建空栈
- int create_stack(pMyStack *stack)
- {
- (*stack)=(pMyStack)malloc(sizeof(MyStack));
- if (stack==NULL)
- {
- return ERR_MEMORY;
- }
- (*stack)->next=NULL;
- (*stack)->str=NULL;
- //memset(stack->str,0,STRLEN);
- return OK;
- }
-
-
- //压栈
- int push_stack(pMyStack *stack,const char *data,int dataLen)
- {
-
- if (*stack==NULL||data==NULL)
- {
- return ERR_VALUE;
- }
- if ((*stack)->str==NULL) //空栈,不用建节点
- {
-
- (*stack)->str=(char*)malloc(dataLen*sizeof(char)+1);
- memset((*stack)->str,0,dataLen+1);
- //赋值
- strncpy((*stack)->str,data,dataLen);
- (*stack)->next=NULL;
- return OK;
- }
- else
- {
- pMyStack node=NULL;
- //新建节点
- node=(pMyStack)malloc(sizeof(MyStack));
- if (node==NULL)
- {
- return ERR_MEMORY;
- }
- node->str=NULL;
- node->next=NULL;
- node->str=(char*)malloc(dataLen*sizeof(char)+1);
- memset(node->str,0,dataLen+1);
- //赋值
- strncpy(node->str,data,dataLen);
- node->next=(*stack);
- (*stack)=node;
- return OK;
- }
- }
- //栈顶是否为空
- int stack_isEmpty(pMyStack stack)
- {
- if (stack==NULL)
- {
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- //删除栈顶元素
- int pop_stack(pMyStack *stack)
- {
- pMyStack tmp=NULL;
- if (stack_isEmpty(*stack)==TRUE)
- {
- return ERR_VALUE;
- }
- tmp=(*stack);
- (*stack)=(*stack)->next;
- if (tmp->str!=NULL)
- {
- free(tmp->str);tmp->str=NULL;
- }
- return OK;
- }
- //获取栈顶元素
- const char* top_stack(pMyStack stack)
- {
- if (stack_isEmpty(stack)==TRUE)
- {
- return NULL;
- }
- return stack->str;
- }
- //遍历栈
- void show_stack(pMyStack stack)
- {
- pMyStack tmp=stack;
- if (tmp==NULL)
- {
- return ;
- }
- while (tmp->str!=NULL)
- {
- printf("%s ",tmp->str);
- if (tmp->next!=NULL)
- {
- tmp=tmp->next;
- }
- else
- {
- break;
- }
- }
- printf("\n");
- }
- //清空栈
- void clear_stack(pMyStack *stack)
- {
- if (*stack==NULL)
- {
- return ;
- }
- while ((*stack)!=NULL&&(*stack)->str!=NULL)
- {
- free((*stack)->str);(*stack)->str=NULL;
- *stack=(*stack)->next;
- //释放节点
- free(tmp);tmp=NULL;
- }
- }
-
-
- int main()
- {
- char data[]={"c hello world ! shen xue bing"};
- char *de=" ";
- int deLen=strlen(de);
- pMyStack stack=NULL;
- create_stack(&stack);
- char *head=data;
- char *pdata=NULL;
- while(1)
- {
- char tmp[20]={0};
- pdata=strstr(head,de);
- if (pdata!=NULL)
- {
- int len=pdata-head;
- strncpy(tmp,head,len);
- head+=len+deLen;
- push_stack(&stack,tmp,len);
- //printf("%s\n",tmp);
- }
- else //
- {
- int len=strlen(head);
- strncpy(tmp,head,len);
- push_stack(&stack,tmp,len);
- //printf("%s\n",tmp);
- break; //最后没有空格 退出
- }
-
- }
- printf("遍历整个栈:\n");
- show_stack(stack);
- printf("栈顶元素:\n");
- printf("%s\n",top_stack(stack));
- printf("删除栈顶元素\n");
- pop_stack(&stack); //删除栈顶元素
- printf("遍历整个栈:\n");
- show_stack(stack);
- printf("栈顶元素:\n");
- printf("%s\n",top_stack(stack));
- //清空栈
- clear_stack(&stack);
- system("pause");
- }
-
-
运行结果: