Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在链表中实现push/pop(C+;+;)_C++_Stack_Linked List_Implementation - Fatal编程技术网

C++ 在链表中实现push/pop(C+;+;)

C++ 在链表中实现push/pop(C+;+;),c++,stack,linked-list,implementation,C++,Stack,Linked List,Implementation,我试图将推送/弹出合并到一个链接列表中,但我似乎无法让它工作。当我运行我的测试函数时,我将我的链表设置为零,并且我尝试推送值,但是列表总是返回,其中没有值。谁能告诉我我做错了什么 if (top == NULL){ current = top; current->next = NULL; //NULL->next : will cause segfault } 如果top为NULL,则设置current=top[这是NULL],然后访问current->

我试图将推送/弹出合并到一个链接列表中,但我似乎无法让它工作。当我运行我的测试函数时,我将我的链表设置为零,并且我尝试推送值,但是列表总是返回,其中没有值。谁能告诉我我做错了什么

if (top == NULL){
      current = top;
      current->next = NULL; //NULL->next : will cause segfault
  }
如果top为NULL,则设置
current=top
[这是NULL],然后访问
current->next
,这将导致segfault,即您正在尝试访问NULL

编辑:评论的后续行动:

如果语句看起来多余,您可能只需要设置:
current->next=head和<代码>头部=电流[除当前分配之外]

而不是

 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;
}

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
    }
}

您能指定链表类的属性吗?[你有没有可能做错什么事]

如果不是你,我会:

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 ; 
   } 
}

在链表中,你必须保持头指针指向链表的头,保持尾指针指向链表的尾, 你必须注意扩大名单的两个案例。 最好的学习方法是在空白链表上演示插入

保重 S

请尝试此代码

void push(data * newpushdata){
    if(head !=null){
        linkednode current = new linkednode(newpushdata);
        current->next = head;
        head = current;
    }
    else {
       head = new linkednode(newpushdata);
    }
}

这是我对包含int元素的堆栈的工作解决方案,但是使用Stack**s而不是Stack*s创建一个空的pushStack可能更好

在pop(堆栈**S)中,我创建了一个sentinel,因此如果堆栈为空,则返回-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;
}
您可以轻松调用pop和stack:

    Stack *S = NULL;
    int x = somevalue;
    int y;
    S = pushStack(S, x);
    y = pop(&S);

我从top=current更改了它,但仍然没有返回任何内容。@Bleuchese:问题是,当需要更改全局变量头时,您只更改了局部变量
top
——否则,您的更改将无法“生存”超出函数的结尾。您的if语句似乎多余,您可能只需要设置:
current->next=head和<代码>头部=电流    Stack *S = NULL;
    int x = somevalue;
    int y;
    S = pushStack(S, x);
    y = pop(&S);