代码: 全选
// special line structure | stack
// linked storage form
// Last Modified: 2008-06-04
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
char data;
struct node *next;
} stack;
void init(stack *top)
{
stack *tmp;
while (top->next)
{
tmp = top;
top = top->next;
free(tmp);
}
printf("\n\n init stack ");
}
// tO(1)
void push(stack *top, char val)
{
stack *tmp;
tmp->data = val;
tmp->next = top;
printf("\n top->data = %c \n", top->data);
top = tmp;
printf("\n top->data = %c \n", top->data);
}
// tO(1)
/*
void pop(stack *s)
{
void print(stack *s);
if (s->top == -1)
{
printf("\n\n empty stack \n");
return;
}
printf("\n\n +--> ");
printf("\n | ");
printf("\n %c ", s->data[s->top]);
printf("\n | ");
s->top--;
print(s);
}
// tO(1)
void top(stack *s)
{
if (!(s->top + 1))
{
printf("\n\n empty stack ");
return;
}
printf("\n\n +---+---+ ");
printf("\n | %c | %d | ", s->data[s->top], s->top);
printf("\n +---+---+ ");
}
// tO(1)
*/
void print(stack *top)
{
stack *tmp;
printf("\n top->data = %c \n", top->data);
if (top->data)
{
tmp = top;
printf("\n\n +---+ ");
while (tmp->data)
{
printf("\n | %c | ", tmp->data);
printf("\n +---+ ");
tmp = tmp->next;
}
}
else
{
printf("\n");
printf("\n +----+ ");
printf("\n |NULL|");
printf("\n +----+ ");
printf("\n\n empty stack \n");
}
}
int main()
{
void init(stack *top);
void push(stack *s, char val);
/*
void pop(stack *s);
void top(stack *s);
*/
void print(stack *top);
char key = 0;
char value = 0;
stack *top = (stack *)malloc(sizeof(stack));
//top->data = '\0';
//top->next = NULL;
printf("\n\n special line structure | stack - linked storage form ");
init(top);
while (1)
{
printf("\n\n 1. init ");
printf("\n");
printf("\n 2. push ");
/*
printf("\n 3. pop ");
printf("\n 4. top ");
printf("\n");
*/
printf("\n 5. print ");
printf("\n");
printf("\n 0|q quit ");
printf("\n");
printf("\n ");
key = getchar();
switch(key)
{
case '1':
printf("\n");
init(top);
printf("\n\n continue ");
getchar();
getchar();
break;
case '2':
printf("\n\n push value: ");
getchar();
scanf("%c", &value);
push(top, value);
printf("\n\n continue ");
getchar();
getchar();
break;
/*
case '3':
pop(s);
printf("\n\n continue ");
getchar();
getchar();
break;
case '4':
top(s);
printf("\n\n continue ");
getchar();
getchar();
break;
*/
case '5':
print(top);
printf("\n\n continue ");
getchar();
getchar();
break;
case '0':
goto QUIT_FLAG;
}
}
QUIT_FLAG:
return 0;
}