gpt4 book ai didi

c - 双链表-C

转载 作者:太空宇宙 更新时间:2023-11-04 03:22:48 25 4
gpt4 key购买 nike

我正在尝试制作一个简单的双链表,我首先使用了 (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");

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

“输入末尾的预期声明或语句”

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

之后我使用了 (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",*(搜索(数据)));

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

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

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

现在我们在 CreatNode 函数中遇到了一个问题,它在 temp 为 null 的情况下不返回任何内容。所以你需要在 if block 中返回 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 和 printf 行移出 if block :

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

关于c - 双链表-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43904322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com