gpt4 book ai didi

c - 如何将 scanf() 数据插入到链表数组中?

转载 作者:行者123 更新时间:2023-11-30 16:39:52 25 4
gpt4 key购买 nike

我创建了一个链表数组,该数组分为四个级别(在 case 1 switch 语句中定义的参与方大小的参数),分别与 array[0]-array[3] 相关。

我试图找出如何获取用户输入(来自 scanf 语句)并将其插入数组中各自的索引中。

我可以获得一些帮助来了解如何在 else 逻辑之后实现此策略吗?

这是代码:

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

#define NODE struct node

struct node
{
char partyname[20];
int partysize;
NODE *next;
};

struct node* array[4]; // array to be inserted into

//
// proto
//

void add_party(char name[], int age);
void delete_party(char name[]);
void list_parties(void);
void change_p_size(void);


//
// globals
//

NODE *head=NULL;
NODE *tail=NULL;

//
// main function
//

int main()
{
int x;
while (1)
{
printf("Enter 1 to add a party\nEnter 2 to remove a party\nEnter 3 for the list of the party\nEnter 4 to quit\n");
// user interface
scanf("%d",&x);
switch(x)
{
char name[20]; //local variables
int size;

case 1:
printf("Party Name: ");
scanf("%s", name);
printf("\nParty Size: ");
scanf("%d", &size);
if(size >= 1 && size <= 2)
{
//put into array[0]
}
else if(size >= 3 && size <= 4)
{
//put into array[1]
}
else if(size >= 5 && size <= 6)
{
//put into array[2]
}
else(size >= 7)
{
//put into array[3]
}
add_party(name, &size)
break;

case 2:
printf("\nSize of party to delete: ");
scanf("%i", &size);
if(size >= 1 && size <= 2)
{
//traverse array[0] and delete
}
else if(size >= 3 && size <= 4)
{
//traverse array[0] and delete
}
else if(size >= 5 && size <= 6)
{
//traverse array[0] and delete
}
else(size >= 7)
{
//traverse array[0] and delete
}
delete_party(size)
break;

case 3:
list_parties();
break;

case 4:
change_partysize();
break;

case 5:
return 0;
default:
continue;
}
}
}

//
//add function
//

void add_party(char *name, int size)
{

//create a new node
NODE *p;
NODE *q;

int i=0;

q = (NODE *)malloc(sizeof(NODE)); // allocate memory the size of the struct

strcpy(q->name,partyname); // (source,destination)
q->size = partysize;

if(head == NULL) // if an empty list, create a head and tail
{
head = q;
tail = head;
q->next = NULL;
return;
}

//traversal
p = head;
while(p != NULL)
{
//check that no repeating names. delete nodes that do have repeating names
if(strcmp(p->name,name) == 0)
{
printf("\nSorry, that name is already taken\n");
free(q);
return;
}
p = p->next; //go to the next node in the list
}

tail->next = q;
q->next = NULL;
tail = q;
}

//
//delete function
//

void delete_party(int size)
{
NODE *p = head;
if(p == NULL)
return;
if(head == tail) // case 1
{
if(head->size <= size)
{
head=NULL;
tail=NULL;
free(p);
}
return;
}
while(p->next->next != NULL)
{
if(p->next->size <= size) // check that its not going too far?
{
p->next=p->next->next;
return;
}
}
if(p->size <= size) // case 2, multiple elements
{
head=p->next;
free(p);
return;
}
if(p->next->size <= size) // case 3, one element
{
node *q=p->next;
p->next=NULL;
free(q);
tail=p;
}

}

//
// list function
//

void list_parties(void)
{
node *p=head;
while(p!=NULL)
{
printf("%s, %d\n", p->name, p->size);
p=p->next;
}
}

最佳答案

只需向 add_party 函数添加一个目标参数即可指定数组位置。

类似的东西add_party(node *dest, char *name, intage) 然后从 switch 语句中传递位置,然后调用 if like so

if(size >= 1 && size <= 2)
{
add_party(&array[0], name, age);
}

与删除函数相同,传递数组位置作为参数。

关于c - 如何将 scanf() 数据插入到链表数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46902988/

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