【发布时间】:2017-10-24 05:44:58
【问题描述】:
我创建了一个链表数组,该数组分为四个级别(在 case 1 switch 语句中定义的参数),分别与数组 [0]-数组 [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;
}
}
【问题讨论】:
-
#define NODE struct什么? -
修复了它。打错了
-
不,完全删除它。不要为此使用#define。如果需要 typedef,则使用
typedef关键字拼写。
标签: c arrays linked-list