简短的代码(反序输出字符),有没有更短的?(堆栈实现的反序终于写完了,请大家指点!)
哈哈,练习一个堆栈反序输出字符的问题,用链表存储,搞了半天还报错,烦了,写个最短的。哈哈,虽然会有个 warning,不过总算运行正常。 #include <stdio.h>
main()
{
char charr[1000];
int i=0;
for(i=0;(i<1000)?((charr[i]=getchar())!='\n'):0;i++);
for(;(i>=0)?(putchar(charr[i])):0;i--);
}
命令用:gcc mainc.c -o mainc.out -std=c99
./mainc.out
当然可以小变态一下:
#include <stdio.h>
main()
{
char charr[1000];
int i=-1;
for(;i++,(i<1000)?((charr[i]=getchar())!='\n'):0;);
for(;i--,(i>=0)?(putchar(charr[i])):0;);
}
堆栈实现反序的代码终于写完了,感觉还好啦!写的冲忙,请大家指点! 编译:teststack.c stack.c可以测试下;
//stack.h
#ifndef _stack_h_
#define _stack_h_
#include <stdbool.h>
//==============================================
#define MAXSTACKITEMS 10000
//stuct stack
typedef struct stackitem{
//TreeNode * pnode;
//int visted;
char ch; //for tstack.c to test
}StackItem;
typedef struct stacknode{
StackItem item;
struct stacknode * next;
}StackNode;
typedef struct stack{
StackNode * head;
int total;
}Stack;
//function define
bool Initialize(Stack * * pstack);
bool IsFullStack(const Stack * pstack);
bool IsEmptyStack(const Stack * pstack);
bool PushIn(StackItem * pitem,Stack * pstack);
StackItem PushOut(Stack * pstack);
bool EmptyStack(Stack * pstack);
#endif
//***************************************************************************************
//stack.c
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
//=================================================
//local declear
static bool AddNode(Stack * pstack,StackNode * pnode);
static StackNode * MakeNode(StackItem * pitem);
//local function
static bool AddNode(Stack * pstack,StackNode * pnode)
{
if(pstack!=NULL && pnode!=NULL)
{
pnode->next=pstack->head;
pstack->head=pnode;
(pstack->total)++;
return true;
}
else
return false;
}
static StackNode * MakeNode(StackItem * pitem)
{
StackNode * temp=NULL;
temp=(StackNode *)malloc(sizeof(StackNode));
if(temp!=NULL)
{
temp->item=*pitem;
temp->next=NULL;
return temp;
}
else
fprintf(stderr,"there is no enough mem!");
}
//=====================================================
//function by stack.h
bool Initialize(Stack * * pstack)
{
*pstack=NULL;
*pstack=(Stack *)malloc(sizeof(Stack));
if((*pstack)!=NULL)
{
(*pstack)->head=NULL;
(*pstack)->total=0;
return true;
}
else
return false;
}
bool IsFullStack(const Stack * pstack)
{
return (pstack->total<MAXSTACKITEMS)?false:true;
}
bool IsEmptyStack(const Stack * pstack)
{
return (pstack->total==0)?true:false;
}
bool PushIn(StackItem * pitem,Stack * pstack)
{
return (AddNode(pstack,MakeNode(pitem)))?true:false;
}
StackItem PushOut(Stack * pstack)
{
if(IsEmptyStack(pstack))
fprintf(stderr,"Error! PushOut failed! no StackItem in Stack now!");
else
{
StackItem titem=(pstack->head)->item;
StackNode * tpnode=pstack->head;
pstack->head=(pstack->head)->next;
pstack->total--;
free(tpnode);
return titem;
}
}
bool EmptyStack(Stack * pstack)
{
int i;
for(i=0;i < pstack->total;i++)
PushOut(pstack);
return true;
}
//*****************************************************************
//teststack.c
//print the input characters order by desc;
#include <stdio.h>
#include "stack.h"
int main()
{
Stack * pstack=NULL;
Initialize(&pstack);
char ch;
while((ch=getchar())!='\n')
{
StackItem pitem;
pitem.ch=ch;
PushIn(&pitem,pstack);
}
while(pstack->total > 0)
putchar(PushOut(pstack).ch);
return 0;
}
[ 本帖最后由 wsj3000 于 2009-8-20 02:34 编辑 ]
mainc.zip
(610 Bytes)
stack.zip
(1.74 KB)