Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.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_C_Switch Statement_Doubly Linked List - Fatal编程技术网

双链表-C

双链表-C,c,switch-statement,doubly-linked-list,C,Switch Statement,Doubly Linked List,我试图制作一个简单的双链接列表,我首先使用了(switch): int choice, data; switch(choice) { case 1: printf("Enter Your Data\n"); scanf("%d",&data); InsetFirst(data); data =0; break; case

我试图制作一个简单的双链接列表,我首先使用了(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");
但它不断地向我显示其中一个函数中的错误

“输入结束时预期的声明或语句”

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

在那之后,我使用了(如果,如果其他),然后它工作了`

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");
    }
}
#包括
#包括
#包括
结构体类型
{
int数据;
结构节点*pnext;
结构节点*pprev;
};
结构节点*pstart=NULL;
结构节点*plast=NULL;
/**功能原型**/
结构节点*创建节点(空);
void InsetFirst(int数据);
void InsertLast(int数据);
作废打印转发(作废);
void向后打印(void);
结构节点*搜索(整型数据);
void DeleteNode(int节点);
int main()
{
int-choice=1,数据;
而(1)
{
printf(“从以下选项中选择\n\n”);
printf(“1-开头插入\n2追加\n3向前打印\n4向后打印\n5搜索\n6删除\n”);
scanf(“%d”,选择(&C);
如果(选项==1)
{
printf(“输入数据”);
scanf(“%d”和数据);
插图一(数据);
数据=0;
}
else if(选项==2)
{
printf(“输入数据”);
scanf(“%d”和数据);
InsertLast(数据);
数据=0;
}
else if(选项==3)
{
printf(“从开始到结束的列表:\n”);
打印转发();
}
else if(选项==4)
{
printf(“从末尾到开头的列表\n”);
PrintBackward();
}
else if(选项==5)
{
printf(“输入要搜索的数据\n”);
扫描频率(“%d”,数据);
搜索(数据);
数据=0;
}
else if(选项==6)
{
printf(“输入要删除的数据\n”);
scanf(“%d”和数据);
删除节点(数据);
}
其他的
{
printf(“输入有效选项\n”);
}
}
/*
int选择,数据;
开关(选择)
{
案例1:
printf(“输入数据”);
scanf(“%d”和数据);
插图一(数据);
数据=0;
打破
案例2:
printf(“输入数据”);
scanf(“%d”和数据);
InsertLast(数据);
数据=0;
打破
案例3:
printf(“从开始到结束的列表:\n”);
打印转发();
打破
案例4:
printf(“从末尾到开头的列表\n”);
PrintBackward();
打破
案例5:
printf(“输入要搜索的数据\n”);
扫描频率(“%d”,数据);
搜索(数据);
if(搜索(数据))
{
printf(“%d\n”,*(搜索(数据));
}
其他的
{}
数据=0;
打破
案例6:
printf(“输入要删除的数据\n”);
scanf(“%d”和数据);
删除节点(数据);
打破
违约:
printf(“无效输入\n”);
*/
返回0;
}
/**函数在列表中创建节点**/
结构节点*创建节点(无效)
{
结构节点*temp;
temp=(结构节点*)malloc(sizeof(结构节点));
如果(!temp)
{
printf(“\n内存不足”);
}
其他的
{
返回温度;
}
}
/**************************************************************************************/
/**函数在列表的开头插入节点**/
void InsetFirst(整型数据)
{
结构节点*temp;
temp=CreatNode();
温度->数据=数据;
temp->pnext=NULL;
temp->pprev=NULL;
if(pstart==NULL)
{
pstart=温度;
plast=温度;
}
其他的
{
temp->pnext=pstart;
pstart->pprev=温度;
pstart=温度;
}
}
/***********************************************************************************/
/**函数在列表末尾插入节点**/
void InsertLast(int数据)
{
结构节点*temp;
temp=CreatNode();
温度->数据=数据;
temp->pnext=NULL;
temp->pprev=NULL;
if(pstart==NULL)
{
pstart=温度;
plast=温度;
}
其他的
{
温度->pprev=塑料;
plast->pnext=温度;
plast=温度;
}
}
/**********************************************************************************************/
/**函数从开始到结束打印列表**/
作废打印转发(作废)
{
结构节点*当前;
电流=pstart;
printf(“\n从开始到结束的列表:\n”);
while(当前)
{
printf(“\n%d”,当前->数据);
当前=当前->pnext;
}
printf(“\n”);
}
/*********************************************************************************************/
void向后打印(void)
{
结构节点*当前;
电流=塑料;
printf(“\n从末尾到开头的列表:\n”);
while(当前)
{
printf(“\n%d”,当前->数据);
电流=电流->pprev;
}
printf(“\n”);
}
/*********************************************************************************************/
/**函数来查找给定的数据**/
结构节点*搜索(整型数据)
{
结构节点*当前;
电流=pstart;
如果(当前)
{
while(当前)
{
如果(当前->数据==数据)
{
回流;
}
当前=当前->pnext;
}
printf(“\n未找到\n”);
返回NULL;
}
}
/*****************
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;
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;
    }
}
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;
}
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;
}