【问题标题】:How do I insert scanf() data into an array of linked list?如何将 scanf() 数据插入到链表数组中?
【发布时间】: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


【解决方案1】:

只需将目标参数添加到指定数组位置的add_party 函数即可。

类似的东西 add_party(node *dest, char *name, int age) 然后从你的 switch 语句中传递位置,然后调用,如果这样的话

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

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

【讨论】:

  • project6.c:63:21: error: too many arguments to function ‘add_party’ add_party(&array[0], name, size);
  • 你改变你的函数原型了吗?也许那会导致错误。
猜你喜欢
  • 2023-03-19
  • 2022-06-16
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多