实现双端队列时C中的分段错误

时间:2018-07-13 07:17:29

标签: c pointers

我正在尝试在C中实现双端队列。我正在学习C,因此该错误似乎非常琐碎。这是整个程序

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
    int data;
    struct node *prev;
    struct node *next;
}Node;

Node *getNode(int data){
    Node *temp = (Node *)malloc(sizeof(Node));
    temp -> next = temp -> prev = NULL;
    temp -> data = data;
    return temp;
}

void push_right(Node **head, Node **tail, int data){
    Node *newNode = getNode(data);
    Node *temp = (*head);
    if (temp == NULL){
        *head = newNode;
    }   
    else{
        while(temp -> next != NULL){
            temp = temp -> next;
        }
        temp -> next = newNode;
        newNode -> prev = temp;
        *tail = newNode;
    }
}

void push_left(Node **head, Node **tail, int data){
    Node *newNode = getNode(data);
    Node *temp = (*head);
    if (temp == NULL){
        *head = newNode;
    }   
    else{
        newNode -> next = temp;
        newNode -> prev = NULL;
        (*head) = newNode;
    }
}

void remove_right(Node **head, Node **tail, int *val){
    Node *temp = (*tail);
    if (temp == NULL)
    {
        printf("Cannot be removed doesn't point to anything\n");
        return;
    }
    else{
        *val = temp -> data;
        temp = temp -> prev;
        (*tail) = temp;
    }
    free(temp);
}

void remove_left(Node **head, Node **tail, int *val){
    Node *temp = (*head);
    if (temp == NULL)
    {
        printf("Cannot be removed doesn't point to anything\n");
        return;
    }
    else{
        *val = temp -> data;
        temp = temp -> next;
        (*tail) = temp;
    }
    free(temp);
}

void print_all(Node *head){
    Node *temp = head;
    printf("\n");
    while(temp != NULL){
        printf("%d\n",temp->data);
        temp = temp -> next;
    }
}

int main(int argc, char const *argv[])
{
    int *val = NULL;
    Node *head = NULL;
    Node *tail = NULL;
    for (int i = 0; i < 10; ++i){
        push_right(&head, &tail,i);
        push_left(&head, &tail,i);
    }
    remove_left(&head, &tail, val);
    print_all(head);
    return 0;
}

调用remove_left()时似乎出现了问题。我花费了大量时间来了解问题的根源,但似乎没有任何效果。请帮忙。

这是“终端”屏幕上的输出。

lib2s-iMac:queue admin$ ./a.out 
Segmentation fault: 11

谢谢。

2 个答案:

答案 0 :(得分:1)

问题在这里:

*val = temp -> data;

Val为NULL,因此尝试对其进行取消引用将导致分段错误。

如果将val类型更改为int而不是指向int的指针。然后像这样调用remove_left:

int main(int argc, char const *argv[])
{   int val = 0;
    Node *head = NULL;
    Node *tail = NULL;
    for (int i = 0; i < 10; ++i){
        push_right(&head, &tail,i);
        push_left(&head, &tail,i);
    }

    remove_left(&head, &tail, &val);
    print_all(head);
    return 0;
}

这应该有效。

答案 1 :(得分:1)

您的程序中有 3 个主要错误。

  1. [TestFixture] public class TestClass { [Test] public void TestStuff3() { DesiredCapabilities caps = new DesiredCapabilities(); caps.SetCapability("browserstack.user", <username>); caps.SetCapability("browserstack.key", <key>); caps.SetCapability("browserstack.debug", "true"); caps.SetCapability("real_mobile", "true"); caps.SetCapability("device", "iPhone X"); caps.SetCapability("os_version", "11.0"); caps.SetCapability("os", "ios"); caps.SetCapability("browser", "iphone"); IWebDriver driver = new CnxDriver(new Uri("http://hub-cloud.browserstack.com/wd/hub/"), caps); WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60)); driver.Navigate().GoToUrl("https://www.w3schools.com/Jsref/tryit.asp?filename=tryjsref_touchstart"); driver.SwitchTo().Frame("iframeResult"); var element = wait.Until(d => d.FindElement(By.XPath(".//body/p[1]"))); Thread.Sleep(5000); var touchAction = new TouchActions(driver); touchAction.SingleTap(element).Perform(); Thread.Sleep(5000); driver.Quit(); } } ,这里应该是int *val = NULL;,而不是int val
  2. 在功能int *val上。在这里,您应该编辑remove_left()而不是(*head)。此外,指针(next,perv)的管理不正确。
  3. (*tail)相同。错误管理指针。

修改功能:-

remove_right()

修改过的main():-

void remove_right(Node **head, Node **tail, int *val)   // modified This function.
{
    Node *temp = (*tail);
    if (temp == NULL)
    {
        printf("Cannot be removed doesn't point to anything\n");
        return;
    }

    else
    {
        *val = temp->data;
        temp->prev->next = NULL;
        (*tail) = temp->prev;
    }
    free(temp);
}

void remove_left(Node **head, Node **tail, int *val)   // modified This function.
{
    Node *temp = (*head);
    if (temp == NULL)
    {
        printf("Cannot be removed doesn't point to anything\n");
        return;
    }

    else
    {
        *val = temp->data;
        temp->next->prev = NULL;
        (*head) = temp->next;
    }
    free(temp);
}

Complete code Online.

输出:-

int main(int argc, char const *argv[])
{
    int val = -1;                         // Modified here
    Node *head = NULL;
    Node *tail = NULL;
    for (int i = 0; i < 10; ++i)
    {
        push_right(&head, &tail, i);
        push_left(&head, &tail, i);
    }
    remove_left(&head, &tail, &val);
    print_all(head);
    return 0;
}
相关问题