链表数组c

时间:2013-03-30 17:39:38

标签: c arrays linked-list

我正在尝试将节点添加到链接列表数组中,但我不确定该数组的索引。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Max_Students 10

 struct node {
    char courseID[7];
    int section;
    int credits;
    struct node *link;
};


int main(void)
{
    int i=0;
    struct node* Students[Max_Students];
    int studentnum=0; //student index
    int run=1;
    char coursetemp[7];
    int option, num, num2;
    struct node *ptr;
    void add(struct node **, int, int, char[], int);
    ptr = NULL;
    while (run==1)
    {
        printf("What Student's info would you like to view?\n");
        scanf("%i", &studentnum);
        printf("Main Menu\n 1. Add Course\n");
        scanf("%d", &option);
        if (option == 1)
        {
            printf("enter string\n");
            scanf("%s", coursetemp);
            printf("enter two ints\n");
            scanf("%d %d", &num, &num2);
            add(&ptr, num, num2, coursetemp, studentnum);
        }   
        else
        {
        }   
    }
    return 0;
}
void add(struct node **q, int num, int num2, char coursetemp[7], int stdnum)
{
    struct node *temp;
    temp = *q;
    if (*q == NULL)
    {
        *q = malloc(sizeof(struct node));
        temp = *q;
    }
    else
    {
        while ((temp->link) != NULL)
        {
            temp = temp->link;
        }
        temp->link = malloc(sizeof(struct node));
        temp = temp->link;
    }
    temp->section = num;
    temp->credits = num2;
    strcpy(temp->courseID, coursetemp);

    temp->link = NULL;
}

我要做的是scanf for stdnum,然后为学生[stdnum]调用addnode。 我尝试过像

这样的事情
temp->Students[stdnum].section = num;
temp->Students[stdnum].credits = num2;
strcpy(temp->Students[stdnum].courseID, coursetemp);

但我似乎无法让他们正常工作。如何正确调用addnode()才能使其正常工作?

0 个答案:

没有答案