我试图将push/pop合并到链表中,我似乎无法使其工作。当我运行我的测试函数时,我将链接列表设置为零,并尝试推送值,但列表一直返回而没有任何值。谁能告诉我我做错了什么?在链表中实现push/pop(C++)
0
A
回答
1
if (top == NULL){
current = top;
current->next = NULL; //NULL->next : will cause segfault
}
如果顶部为NULL,则设置current = top
[这是NULL],然后访问current->next
,这将导致一个段错误,您试图访问NULL ..
编辑:跟随高达评论:
你的if语句似乎是多余的,你应该只需要设置:current->next = head;
和head = current;
[除了当前分配]
0
而不是
if (top == NULL){
current = top;
current->next = NULL;
}
你想
if (top == NULL){
top = current;
current->next = NULL;
}
,当然还有,在此之后,你必须确保你实际设置head
到top
一次。
现在你已经做了这个改变,应该清楚的是,两种情况都做同样的事情 - 所以不需要区分事实。所以功能可以简化为
void push(Data * newPushData){
LinkNode * current = new LinkNode(newPushData);
current->next = head;
head = current;
}
0
的top
变量是push(...)
函数的局部变量。您可以改用head
,我宁愿修改if
声明。
我认为功能应该是这样的:
void push(Data * newPushData){
LinkNode * current = new LinkNode(newPushData);
if (head != NULL){
current->next = head;
head = current;
}
else{
head = current;
current->next = NULL; // if you haven't done it in LinkNode constructor
}
}
0
void push(Data * newPushData)
{
if(head != NULL)
{
LinkNode current = new LinkNode(newPushData);
current->next = head;
head = current;
}
else
{
head = new LinkNode(newPushData);
}
}
0
你能请注明链接列表类的属性? [有轻微的机会,你正在做的事情错]
你相反,我会怎么做:
void push(Data * newPushData){
if (head == NULL)
head->data = newPushData
tail = head ;
else // regular situation
{
Node * node = new Node() ;
tail->next = node;
node->data = newPushData;
node->next = NULL ;
tail = node ;
}
}
在你不得不在维持头指针指向一个链表该列表的头部,保持尾部指针位于列表的尾部, 您必须注意扩大列表的两种情况。 学习的最佳方式是说明在空白链表上的插入。
照顾 小号
0
试试这个代码...
void push(data * newpushdata){
if(head !=null){
linkednode current = new linkednode(newpushdata);
current->next = head;
head = current;
}
else {
head = new linkednode(newpushdata);
}
}
+0
这只是一堆代码。请至少发表评论 – kolossus
0
是含有INT元素堆栈我工作的解决方案,但也许这是更好地创建使用堆栈的空隙pushStack ** S而不是Stack * S。
在弹出(栈** S)我创建了一个哨兵,因此,如果堆栈是空的,返回-1:
typedef struct StackT {
int val;
struct StackT *next;
} Stack;
int isStackEmpty (Stack *S) {
if (S == NULL)
return 1;
else
return 0;
}
int *pop(Stack **S) {
Stack *tmp = *S;
int i = -1;
if (isStackEmpty(tmp) == 0) {
i = tmp->val;
*S = tmp->next;
}
return i;
}
Stack *pushStack (Stack *S, int x) {
Stack *node = (Stack *) malloc (sizeof (Stack));
node->val = x;
node->next = S;
return node;
}
你可以叫流行音乐和伊斯利堆栈:
Stack *S = NULL;
int x = somevalue;
int y;
S = pushStack(S, x);
y = pop(&S);
相关问题
- 1. 链表实现在C#()
- 2. C++链表实现
- 3. 链接列表在C中的实现
- 4. C中的链表实现C
- 5. C中的链接列表实现
- 6. 使用C++中的链接列表实现队列实现
- 7. 在c#中实现链表的排序时出现问题
- 8. 在c#中实现代表#
- 9. 在C++中的表实现
- 10. 在C#中实现“表”
- 11. 链表实现中的NumberFormatException
- 12. 在链表图实现
- 13. 在java中实现一个链表在
- 14. 正确地实现单链表C++
- 15. C++:关于链表的实现
- 16. 链接列表实现帮助 - Visual C++
- 17. C++链接列表实现崩溃
- 18. Array-style链接列表实现C++
- 19. 如何在c中的链表中实现一个队列?
- 20. 单链表实现
- 21. golang实现链表
- 22. XOR链表实现
- 23. 链表实现java
- 24. NullPointerException在双链表实现中
- 25. 在php中实现链表PHP
- 26. 在java中实现链接列表
- 27. 在链表类中实现Iterator接口
- 28. 在Kotlin中实现链接列表
- 29. 在C++中使用链接列表实现数学程序
- 30. 在C++中实现链表时遇到困难
我从top = current改变了它,但是我仍然没有得到任何回报。 – BleuCheese
@BleuCheese:问题在于当你需要改变全局变量'head'时,你只改变局部变量'top' - 否则,你的改变不会在函数结束后“生存” 。 –
你的if语句似乎是多余的,你应该只需要设置:'current-> next = head;'和'head = current;' – amit