以下是引用Buger在2013-4-9 08:46:08的发言:
#include
#include
//链式栈定义
typedef struct stackNode {
int data;
struct stackNode *next;
}*LinkStack;
//链式栈初始化
void InitLinkStack(LinkStack *S) {
S = NULL;
}
//链式栈入栈
int push(LinkStack &S, int e) {
LinkStack p;
p = (LinkStack)malloc(sizeof(struct stackNode));
if(!p) return -1;
p -> data = e;
p -> next = S;
S = p;
return 1;
}
//链式栈出栈
int Pop(LinkStack *S, int *e) {
LinkStack p;
if(!S) return -1;
p = S;
S = S -> next;
e = p -> data;
free(p);
return 1;
}
//链式栈取栈顶
int getTop(LinkStack S, int *e) {
if(!S) return -1;
*e = S -> data;
return 1;
}
int main() {
LinkStack S;
return 0;
}
是的那个我自己也写了,但是链式队列可以,但单链表还是不行...能帮忙改下?
这个链式队列是我解决的,还有单链表不行...
#include <stdio.h>
#include <malloc.h>
//链式队列定义
struct queueNode {
int data;
struct queueNode * next;
};
typedef struct {
struct queueNode *front;
struct queueNode *rear;
}LinkQueue;
//链式队列初始化
int InitLinkQueue(LinkQueue *Q) {
Q -> front = Q -> rear = (struct queueNode *)malloc(sizeof(struct queueNode));
if(!Q -> front) return -1;
Q -> front -> next = NULL;
return 1;
}
//链式队列判空
int queueIsEmpty(LinkQueue Q) {
if(Q.front == Q.rear) return -1;
else return -1;
return 1;
}
//链式队列入队
int insertQueue(LinkQueue *Q, int e) {
struct queueNode *p;
p = (struct queueNode *)malloc(sizeof(struct queueNode));
if(!p) return -1;
p -> data = e;
p -> next = NULL;
Q -> rear -> next = p;
Q -> rear = p;
return 1;
}
//链式队列出队
int deleteQueue(LinkQueue *Q, int *e) {
struct queueNode *p;
if(Q -> front == Q -> rear) return -1;
p = Q -> front -> next;
*e = p -> data;
Q -> front -> next = p -> next;
if(p == Q -> rear) Q -> rear = Q -> front;
free(p);
return 1;
}
//链式队列取队头
int getFront(LinkQueue Q, int *e) {
if(Q.front == Q.rear) return -1;
*e = Q.front -> next -> data;
return 1;
}
int main() {
LinkQueue Q;
return 0;
}