我已经创建了一个链表的数组,它分别在四个级别(在第一个案例中为参与方大小定义的参数)中与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;
}
}
答案 0 :(得分:0)
只需向add_party
函数添加目标参数即可指定阵列位置。
喜欢的东西
add_party(node *dest, char *name, int age)
然后从switch语句中传递位置,然后调用if if
if(size >= 1 && size <= 2)
{
add_party(&array[0], name, age);
}
与delete函数相同,将数组位置作为参数传递。