【问题标题】:Double linked list - C双链表-C
【发布时间】:2017-05-10 23:46:58
【问题描述】:

我正在尝试制作一个简单的双链表,我首先使用了(开关):

int choice, data;
    switch(choice)
    {
        case 1:
            printf("Enter Your Data\n");
            scanf("%d",&data);
            InsetFirst(data);
            data =0;
            break;

        case 2:
            printf("Enter Your Data\n");
            scanf("%d",&data);
            InsertLast(data);
            data =0;
            break;

        case 3:
            printf("The list from the beginning to the End :\n");
            PrintForward();
            break;

        case 4:
            printf("The list from the end to the beginning\n");
            PrintBackward();
            break;

        case 5:
            printf("Enter the data you want search\n");
            scanf("%d",data);
            Search(data);
            if(Search(data))
            {
             printf("%d\n",*(Search(data)));
            }
            else
            {}
            data =0;
            break;

        case 6:
            printf("Enter The data you want to delete\n");
            scanf("%d",&data);
            DeleteNode(data);
            break;

        default :
            printf("Not Valid Entry\n");

但它一直在其中一个函数中向我显示此错误

"expected declaration or statement at end of input"

知道我单独测试了这些功能并且它工作正常,

之后我使用了 (if,if-else) 然后它就起作用了`

int main()
{
    int choice=1 , data;
    while (1)
    {
        printf("Choose from the following options\n\n");
        printf("1-Insert at the beginning\n2-Append\n3-Print Forward\n4-Print Backward\n5-Search\n6-Delete\n");
        scanf("%d",&choice);
        if(choice==1)
         {
            printf("Enter Your Data\n");
            scanf("%d",&data);
            InsetFirst(data);
            data =0;
         }
         else if (choice==2)
         {
            printf("Enter Your Data\n");
            scanf("%d",&data);
            InsertLast(data);
            data =0;
         }
         else if(choice==3)
         {
             printf("The list from the beginning to the End :\n");
             PrintForward();
         }
         else if(choice==4)
         {
             printf("The list from the end to the beginning\n");
             PrintBackward();
         }
         else if(choice==5)
         {
             printf("Enter the data you want search\n");
             scanf("%d",data);
             Search(data);
             data =0;
         }
         else if(choice==6)
         {
            printf("Enter The data you want to delete\n");
            scanf("%d",&data);
            DeleteNode(data);
         }
         else
         {
             printf("Enter a Valid Choice\n");
         }
    }`, 

但如果该项目不存在,搜索功能会出错。

希望任何人都可以帮助我,在此先感谢,和平:) 这是完整的代码,其中包含不起作用的注释部分:

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

struct Node
{
    int data;
    struct Node* pnext;
    struct Node* pprev;
};
struct Node* pstart = NULL;
struct Node* plast = NULL;

/** Functions Prototype **/

struct Node* CreatNode (void);
void InsetFirst (int data);
void InsertLast (int data);
void PrintForward (void);
void PrintBackward (void);
struct Node* Search (int data);
void DeleteNode (int Node );


int main()
{
    int choice=1 , data;
    while (1)
    {
        printf("Choose from the following options\n\n");
        printf("1-Insert at the beginning\n2-Append\n3-Print Forward\n4-Print Backward\n5-Search\n6-Delete\n");
        scanf("%d",&choice);
        if(choice==1)
         {
            printf("Enter Your Data\n");
            scanf("%d",&data);
            InsetFirst(data);
            data =0;
         }
         else if (choice==2)
         {
            printf("Enter Your Data\n");
            scanf("%d",&data);
            InsertLast(data);
            data =0;
         }
         else if(choice==3)
         {
             printf("The list from the beginning to the End :\n");
             PrintForward();
         }
         else if(choice==4)
         {
             printf("The list from the end to the beginning\n");
             PrintBackward();
         }
         else if(choice==5)
         {
             printf("Enter the data you want search\n");
             scanf("%d",data);
             Search(data);
             data =0;
         }
         else if(choice==6)
         {
            printf("Enter The data you want to delete\n");
            scanf("%d",&data);
            DeleteNode(data);
         }
         else
         {
             printf("Enter a Valid Choice\n");
         }
    }

/*
    int choice,data;
    switch(choice)
    {
        case 1:
            printf("Enter Your Data\n");
            scanf("%d",&data);
            InsetFirst(data);
            data =0;
            break;

        case 2:
            printf("Enter Your Data\n");
            scanf("%d",&data);
            InsertLast(data);
            data =0;
            break;

        case 3:
            printf("The list from the beginning to the End :\n");
            PrintForward();
            break;

        case 4:
            printf("The list from the end to the beginning\n");
            PrintBackward();
            break;

        case 5:
            printf("Enter the data you want search\n");
            scanf("%d",data);
            Search(data);
            if(Search(data))
            {
             printf("%d\n",*(Search(data)));
            }
            else
            {}
            data =0;
            break;

        case 6:
            printf("Enter The data you want to delete\n");
            scanf("%d",&data);
            DeleteNode(data);
            break;

        default :
            printf("Not Valid Entry\n");
            */

    return 0;
}

/** Function to create Node in the list **/

struct Node* CreatNode (void)
{
    struct Node* temp;
    temp = (struct Node*) malloc(sizeof(struct Node));
    if (!temp)
    {
        printf("\nNot Enough Memory");
    }
    else
    {
        return temp;
    }
}
/**************************************************************************************/


/** Function to Insert Node at the Beginning of the list **/

void InsetFirst (int data)
{
    struct Node* temp;
    temp = CreatNode();
    temp ->data = data;
    temp ->pnext = NULL;
    temp ->pprev = NULL;
    if (pstart == NULL)
    {
        pstart = temp;
        plast = temp;
    }
    else
    {
        temp ->pnext = pstart;
        pstart ->pprev =temp;
        pstart = temp;
    }
}
/***********************************************************************************/



/** Function to Insert Node at the End of the List **/

void InsertLast (int data)
{
    struct Node* temp;
    temp = CreatNode();
    temp ->data = data;
    temp ->pnext = NULL;
    temp ->pprev = NULL;
    if (pstart == NULL)
    {
        pstart = temp;
        plast = temp;
    }
    else
    {
        temp ->pprev = plast;
        plast ->pnext = temp;
        plast = temp;
    }
}
/**********************************************************************************************/

/** Function to Print the list From the beginning to the End **/

void PrintForward (void)
{
    struct Node* current;
    current = pstart;
    printf("\nThe list From the Beginning to the End :\n");
    while (current)
    {
        printf("\n%d",current->data);
        current = current->pnext;
    }
    printf("\n");
}
/*********************************************************************************************/

void PrintBackward (void)
{
    struct Node* current;
    current = plast;
    printf("\nThe list From End to the Beginning :\n");
    while (current)
    {
        printf("\n%d",current->data);
        current = current->pprev;
    }
    printf("\n");
}
/*********************************************************************************************/

/** Function To Find a Given Data **/

struct Node* Search (int data)
{
    struct Node* current;
    current = pstart;
    if (current)
    {
        while(current)
        {
            if (current->data == data)
            {
                return current;
            }
            current = current->pnext;
        }
        printf("\nIt's not found\n");
        return NULL;
    }

}
/**************************************************************************************/

/** Function to Delete a Given Node **/

void DeleteNode (int Node )
{
    struct Node* state;
    state = Search(Node);

    if (state)
    {
        if ((state == pstart) && (state == plast))
        {
            pstart = NULL;
            plast = NULL;
        }
        else if (pstart == state)
        {
            pstart = state->pnext;
            state->pnext->pprev = NULL;
        }
        else if (plast == state)
        {
            plast = state->pprev;
            state->pprev->pnext = NULL;
        }
        else
        {
            state->pprev->pnext = state->pnext;
            state->pnext->pprev = state->pprev;
        }
        free(state);
    }
    else
    {
        printf("NOT Found\n");
    }
}

【问题讨论】:

  • 显示错误信息。
  • 您是否忘记关闭您的 swithc 的 ' } ' 以及之前的初始化选项?
  • 当然,您忘记使用} 关闭switch 代码。您可以在第一次显示它时看到它,也可以在第二次评论它时看到它。

标签: c switch-statement doubly-linked-list


【解决方案1】:

您的代码中存在一些问题。我用的是switch版本,if else版本也有问题。

case 5:
    printf("Enter the data you want search\n");
    scanf("%d",data);
    Search(data);
    if(Search(data))
    {
     printf("%d\n",*(Search(data)));
    }
    else
    {}
    data =0;
    break;

当你使用 scanf 时,你需要发送一个指向你想要存储东西的位置的指针,所以它将是scanf("%d", &amp;data)。 printf 的 %d 也需要 int 值作为参数,但在这里:

printf("%d\n",*(Search(data)));

您正在向它发送一个节点,因此它不会接受它。您需要发送节点内的数据,以便执行以下操作:

printf("%d\n",(*(Search(data))).data);

而且你不需要 else 有一个空块,如果你删除它不会影响程序。

现在我们在 CreatNode 函数中遇到了一个问题,它在 temp 为 null 的情况下不返回任何内容。所以你需要在 if 块中返回 null 以防内存不足:

struct Node* CreatNode (void)
{
    struct Node* temp;
    temp = (struct Node*) malloc(sizeof(struct Node));
    if (!temp)
    {
        printf("\nNot Enough Memory");
        return NULL; //YOU NEED TO RETURN NULL HERE
    }
    else
    {
        return temp;
    }
}

如果例如 first current 等于 NULL,则函数搜索不会返回任何内容,因此您需要将 return 和 printf 行移出 if 块,如下所示:

struct Node* Search (int data)
{
    struct Node* current;
    current = pstart;
    if (current)
    {
        while(current)
        {
            if (current->data == data)
            {
                return current;
            }
            current = current->pnext;
        }
    }
    printf("\nIt's not found\n");
    return NULL;
}

最后一件事以及您的开关不起作用的原因是因为它不在循环中。 Switch 本身不是一个循环,因此您需要将它放在一个 while 循环中,该循环一直有效,直到例如用户输入 0。所以这将是一个解决方案:

int main()
{
    int choice=-1,data;
    while(choice != 0)
    {
        printf("\n\nChoose from the following options\n\n");
        printf("1-Insert at the beginning\n2-Append\n3-Print Forward\n4-Print Backward\n5-Search\n6-Delete\n");
        scanf("%d", &choice);
        switch(choice)
        {
            case 1:
                /*...*/

            case 2:
                /*...*/

            case 3:
                /*...*/

            case 4:
                /*...*/

            case 5:
                /*...*/

            case 6:
                /*...*/

            case 0:
                break;

            default :
                printf("Not Valid Entry\n");

        }
    }
    return 0;
}

【讨论】:

  • 感谢您的帮助,非常感谢 :) 但是在应用您的更正(很有价值)之后,此错误仍然存​​在“ |错误:输入末尾的预期声明或声明| |290|警告: 控制到达非空函数 [-Wreturn-type]|||=== 构建失败:1 个错误,1 个警告(0 分钟,2 秒)== =| " ....如果你能帮上忙,我会很感激的
  • 我用 (if-else) 让它功能齐全,但我还需要让它与 (switch) 一起工作
  • 改正后,您的代码对我来说运行良好,但请告诉我错误出现在哪个函数中?
  • 删除功能结束
  • 你是否改变了delete的定义,因为在你的代码中,定义是void DeleteNode (int Node ),因为它是无效的,它不需要返回任何东西。自从您在此处发布代码以来,您可能已经更改了一些内容。如果您没有更改定义,则会出现错误expected declaration or statement at end of input,因为函数末尾缺少}。检查你的括号。
猜你喜欢
  • 2010-12-25
  • 1970-01-01
  • 2016-01-01
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多