双链表-C

Double linked list - C

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

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

您的代码中存在一些问题。我用的是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", &data)。 printf 的 %d 也需要 int 值作为参数,但这里:

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

你正在向它发送一个节点,所以它不会接受它。您需要发送节点内的数据,因此您可以这样做:

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

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

现在我们在 CreatNode 函数中遇到问题,在 temp 为 null 的情况下 return 没有任何内容。所以你需要在 if 块中 return 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;
    }
}

如果第一个当前值等于 NULL,函数搜索将不会 return 任何内容,因此您需要将 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;
}