数据结构3.0栈的链表操作

#include
#include
#include

typedef struct SLNode
{
int data;
struct SLNode * next;
}*Lstack, Node;

/*
//为方便操作而生成一个头,一个尾指针
//其实没有必要
struct stack
{
Lstack top;
Lstack bottom;
}
*/

Lstack create_stack (void);
void input(Lstack stack);
bool pop(Lstack stack, int *val);
bool push(Lstack stack, int val);
bool is_empty(Lstack stack);

int main(void)
{
int kettle;
Lstack lane = create_stack();
input( lane);

Lstack work = lane->next;
while (work != NULL)
{
	printf("%d ", work->data);
	work= work->next;
}
if (pop(lane, &kettle))
{
	printf("\n%d\n" , kettle);
}
while (lane != NULL)
{
	Lstack p = lane;
	lane = lane->next;
	free(p);
}

return 0;

}

Lstack create_stack (void)
{
Lstack phead = (Lstack)malloc(sizeof(Node));
if (NULL == phead)
{
printf(“船长,动态内存分配失败了\n”);
exit(-1);
}

phead->next = NULL;
return phead;

}

void input(Lstack stack)
{
int numb;
int ship;
int i;
printf(“船长,请问要生成几个节点:”);
scanf("%d", &numb);
for ( i = 0; i < numb; i++)
{
scanf("%d", &ship);
push(stack , ship);
}

}

bool push(Lstack stack, int val)
{
//stack一直是头指针没有动;
Lstack labor = create_stack ();
labor->data = val;
labor->next = stack->next;
stack->next = labor;

return true;

}

bool is_empty(Lstack stack)
{
if (stack->next == NULL)
{
return true;
}
else
{
return false;
}

}

bool pop(Lstack stack, int *val)
{
if (is_empty(stack))
{
return false;
}

Lstack tmp = stack->next;
*val = tmp->data;
stack->next = tmp->next;
tmp->next = NULL;
free(tmp);
tmp = NULL;

return true;

}

你可能感兴趣的:(数据结构基础)