作业三 链栈

栈链,就是栈和链表的集合体咯,理解上不难,只是因为链表指针的存在容易做错

typedef int ElemType;
struct Node;
typedef struct Node * PtrToNode;
typedef PtrToNode Stack;
struct Node
{
    ElemType data;
    PtrToNode next;
};

2288 栈链进栈

void Push(Stack S, ElemType x)
{//有头指针的
	Stack p = (Stack)malloc(sizeof(Node));
	p->data = x;
	p->next = S->next;
	S->next = p;
}
  因为是链表,所以插入数据前要先malloc分配一个空间,赋值完后next连接

   这里p->next=S->next其实就是p->next=NULL

2289 栈链出栈

void Pop(Stack S)
{
	if (S->next == NULL) return;
	else {
		S->next = S->next->next;
	}
}

 if 判断是不是空栈

  其实这里写的有问题,虽然是AC但是这样删除以后,原S->next会造成内存残留,应该把它free()掉。

  

  中缀的那两道题我代码写的太丑了,实在没脸放上来,大家自行百度吧.......




posted @ 2017-11-14 21:25  九大于七  阅读(68)  评论(0编辑  收藏  举报