无法理解c链表

时间:2014-08-16 15:10:01

标签: c linked-list

我正在尝试在c中学习链接列表,但我不确定我的代码有什么问题。这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>  
#define ADDNEW   1
#define LISTALL  2
#define EXIT     3
#define MAX_ENTRY 20
#define NAME_SIZE 20

typedef struct entry {
    char name [NAME_SIZE];
    int mark;
    struct entry *next;
} Student;
typedef Student *list;
Student slist[MAX_ENTRY]; /* from 0 to (MAX_ENTRY – 1) */

int entry_total=0;  /* keep track of the no of entries */

void menu() {
    /* print the menu */
    printf(" ========== MENU =============\n");
    printf("1. Add new entry\n2. List all entries\n3. Exit\n");
}

int getInput() {
    int input;
    printf("Your command (1 to 3)=>");
    scanf("%d",&input);
    return input;
}

Student * add_new(Student slist[],int total,list getlist,int num) {
    list head=getlist;
    list temp;
    char name [NAME_SIZE];
    int mark;

    printf("Adding new entry\nName\t=>");
    scanf("%s",&name);
    printf("Marks\t=>");
    scanf("%d",&mark);

    if(num==0){
        strcpy(head->name, name);
        head->mark=mark;
        head->next=NULL;
        temp=head;
        return temp;
    }else{
        head->next=getlist;
        strcpy(head->next->name, name);
        head->next->mark=mark;
        head->next->next=NULL;
        head=head->next;
    }
}

void printlist(Student * getlist) {
    list head=getlist;
    while (head!=NULL)
    {
        printf("Name %s Score:%d\n\n", head->name, head->mark);
        head = head->next;
    }
}

int main() {
    list head,temp;
    head = (list)malloc(sizeof(Student));
    int opt,i,avg,max;
    int num=0;
    do {
        menu();
        opt=getInput();
        switch(opt) {
            case ADDNEW:
                if (entry_total < MAX_ENTRY) {
                    if(entry_total==0){
                        temp=add_new(slist,entry_total,head,num);
                    }else{
                        add_new(slist,entry_total,head,num+1);
                    }
                    entry_total++;
                } 
                break;
            case LISTALL:
                printlist(temp);
                break;
        }
    } while (opt != EXIT ) ;
}

让我说我选择选择1

  

名称 - &GT;先辈

     

记分→100

比我再次选择1

  

名称 - &GT;最大

     

记分→40

比我选择2;

输出只有

  

姓名最高分:40

正如您所看到的,该程序已覆盖了之前的列表。

那么我该如何解决这个问题?

提前致谢。

2 个答案:

答案 0 :(得分:1)

这将接受多个条目并打印出来。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ADDNEW   1
#define LISTALL  2
#define EXIT     3
#define MAX_ENTRY 20
#define NAME_SIZE 20

typedef struct entry {
    char name [NAME_SIZE];
    int mark;
    struct entry *next;
} Student;

int entry_total=0;  /* keep track of the no of entries */

void menu() {
    /* print the menu */
    printf(" ========== MENU =============\n");
    printf("1. Add new entry\n2. List all entries\n3. Exit\n");
}

int getInput() {
    int input;
    printf("Your command (1 to 3)=>");
    while ( !(((input = getchar()) >= '1') && ( input <= '3'))) {
        // loop until 1, 2, or 3 entered
        while ( ( input = getchar()) != '\n') {
            ; // input was not 1, 2 or 3 so clear buffer
        }
        printf("Your command (1 to 3)=>");  // reprompt
    }
    return (input - '0'); // input will be '1', '2' or '3' so subtract '0'
}

Student * add_new(Student *slist) {
    Student *ptemp;
    char name [NAME_SIZE];
    int mark;

    printf("Adding new entry\nName\t=>");
    scanf("%s",name);
    printf("Marks\t=>");
    scanf("%d",&mark);

    ptemp = malloc ( sizeof ( *ptemp));
    strcpy(ptemp->name, name);
    ptemp->mark=mark;
    ptemp->next=slist;
    return ptemp;
}

void printlist(Student * getlist) {
    Student *start = getlist;
    while (start!=NULL)
    {
        printf("\nName %s \tScore:%d\n", start->name, start->mark);
        start = start->next;
    }
}

int main() {
    Student *head = NULL;
    int opt;
    do {
        menu();
        opt=getInput();
        switch(opt) {
            case ADDNEW:
                if (entry_total < MAX_ENTRY) {
                    head=add_new(head);
                    entry_total++;
                }
                break;
            case LISTALL:
                printlist(head);
                break;
        }
    } while (opt != EXIT ) ;
    return 0;
}

答案 1 :(得分:1)

像这样修复的例子:

//There is no need to pass the global variable
//The use of variable is wrong. or unused.
Student *add_new(list curr){
    list temp = &slist[entry_total];//new node

    printf("Adding new entry\nName\t=>");
    scanf("%s", temp->name);//remove `&` and Might overflow.
    printf("Marks\t=>");
    scanf("%d", &temp->mark);

    if(curr!=NULL){       //returned as a new node when current node is NULL
        curr->next = temp;//connect a new node to the current node
    }
    return temp;
}

int main() {
    list head, curr;
    head = NULL;
    int opt,i,avg,max;
    //int num=0;//no need
    do {
        menu();
        opt=getInput();
        switch(opt) {
            case ADDNEW:
                if (entry_total < MAX_ENTRY) {
                    if(entry_total==0){
                        head=add_new(head);
                        curr = head;
                    }else{
                        curr = add_new(curr);
                    }
                    entry_total++;
                } 
                break;
            case LISTALL:
                printlist(head);
                break;
        }
    } while (opt != EXIT ) ;
    return 0;
}