我有三個文件:stack2.h,stack2.c和main.c.代碼中的兩個警告意味着使用鏈表來實現堆棧
stack2.h包含此:
/* Define linked list structure */
typedef struct node {
int val;
struct Node *next;
} Node, *pNode;
/* Define stack structure */
typedef struct StackType {
pNode top;
} Stack, *pStack;
/* Declare functions */
pStack InitStack();
int IsEmpty(pStack pS);
int Pop(pStack pS);
void Push(pStack pS, int val);
void KillStack(pStack pS);
stack2.c包含
pStack InitStack() {
/* Declare variables */
pStack pS = (pStack)malloc(sizeof(Stack));
/* Set first node to NULL */
pS -> top = NULL;
/* Return pointer to stack */
return pS;
}
int IsEmpty(pStack pS) {
return (pS->top == NULL);
}
int Pop(pStack pS) {
/* Declare variables */
int ret = 0;
pNode temp = NULL;
/* Check if stack is empty */
if(IsEmpty(pS)) {
printf("[ERROR] Pop operation on an empty stack.\n");
exit(1);
}
/* Find return value (last in) */
ret = pS->top->val;
temp = pS->top;
/* Delete and kill node */
pS->top = pS->top->next;
free(temp);
/* Return */
return ret;
}
void Push(pStack pS, int val) {
/* Allocate memory for new node */
pNode nnew = (pNode)malloc(sizeof(Node));
/* Initiate node */
nnew->next = pS->top;
nnew->val = val;
/* Set structure's top to new node */
pS -> top = nnew;
}
我不會什麼main.c中包含的負擔你。實質上,它包含正確的庫和文件,並且簡單地推送和彈出一些值。我得到這些警告:
assignment from incompatible pointer types
這兩行:
nnew->next = pS->top;
pS->top = pS->top->next;
我有點困惑。 nnew是指向節點的指針,因此nnew-> next也是指向節點的指針。 pS是一個指向堆棧的指針,所以pS-> top也是指向一個節點的指針。我看不出這些是不相容的!
這是怎麼回事?謝謝!
您應該指定生成錯誤的FILE和LINE NUMBER。 – abelenky 2012-04-25 23:12:30
'struct node'和'struct Node'不是一回事。 – geekosaur 2012-04-25 23:12:37
@geekosaur:兩個副本和粘貼,並說明,你有一個相當不錯的答案... – sarnold 2012-04-25 23:19:36