gpt4 book ai didi

c - 打印链接列表中的字符串

转载 作者:行者123 更新时间:2023-11-30 15:19:42 26 4
gpt4 key购买 nike

所以我无法让我的程序打印我输入的两个字符串,或者无论您想在列表中放入多少个字符串,它总是打印出多次输入的最后一个字符串。我对所有注释掉的代码感到抱歉,其中大部分代码您不需要阅读。

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

struct node{
char *data;
struct node *next;
}*head;

typedef struct node NODE;

// Function prototypes
void append(char myStr[]);
void add( char myStr[] );
//void addafter(char myStr[], int loc);
void insert(char myStr[]);
int delete(char myStr[]);
void display(struct node *r);
int count();
// main function
int main()
{
int i;
struct node *n;
head = NULL;
char myStr[50];

while(1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Size\n");
printf("4.Delete\n");
printf("5.Exit\n");
printf("Enter your choice : ");

if(scanf("%d", &i) <= 0)
{
printf("Enter only an Integer\n");
exit(0);
}
else
{

switch(i)
{

case 1:
printf("Enter the name to insert : ");
scanf("%50s", myStr);
insert(myStr);
break;
case 2:
if(head == NULL)
{
printf("List is Empty\n");
}
else
{
printf("Name(s) in the list are : ");
}
display(n);
break;
case 3:
printf("Size of the list is %d\n",count());
break;
case 4:
if(head == NULL)
printf("List is Empty\n");
else
{
printf("Enter the myStrber to delete : ");
scanf("%50s",myStr);

if(delete(myStr))
printf("%s deleted successfully\n",myStr);
else
printf("%s not found in the list\n",myStr);
}
break;
case 5:
return 0;
default:
printf("Invalid option\n");
}
}
}

return 0;
}
// Function definitions
void append(char myStr[])
{
struct node *temp,*right;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = myStr;
right=(struct node *)head;

while(right->next != NULL)
{
right = right->next;
}
right->next = temp;
right = temp;
right->next = NULL;
}
// adding a node to the beginning of the linked list
void add( char myStr[] )
{
struct node *temp;
temp =(struct node *)malloc(sizeof(struct node));
temp->data = myStr;

// only one node on the linked list
if (head == NULL)
{
head = temp;
head->next = NULL;
}

else
{
temp->next = head;
head = temp;
}
}

void insert(char myStr[])
{
int c = 0;
struct node *temp;
temp = head;
if(temp == NULL)
{
add(myStr);
}
else
{
append(myStr);
}
}
int delete(char myStr[])
{
struct node *temp, *prev;
temp = head;

while(temp != NULL)
{
if(temp->data == myStr)
{
if(temp == head)
{
head = temp->next;
head = (*temp).next;
free(temp);
return 1;
}
else
{
prev->next = temp->next;
free(temp);
return 1;
}
}
else
{
prev = temp;
temp = temp->next;
}
}
return 0;
}
void display(struct node *r)
{
r = head;

if(r == NULL)
{
return;
}

while(r != NULL)
{
printf("%s ", r->data);
r = r->next;
if(r == NULL)
{
printf("\nOur linked list is finished!");

}
}

printf("\n");
}
int count()
{
struct node *n;
int c = 0;
n = head;

while(n != NULL)
{
n = n->next;
c++;
}

return c;
}

最佳答案

问题似乎是 main 函数中的 myStr 是一个 char[],因此每次插入数据时它的内容都会被覆盖。请注意,结构体节点 data 字段是一个 char*,它只是指向 myStr 地址。

希望这有帮助!

关于c - 打印链接列表中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30517825/

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