C、双链表问题



我正在尝试读取用户关于客户的输入。

输入:mike、404 forbidden st、raleigh、nc、27607、123.78

然后将该客户添加到按字母排序的双链表中。用户可以选择插入条目、删除条目或查看列表。添加用户控件后…我的扫描仪不能正常工作了。我不明白为什么。我不明白为什么我不能在用户控制版本中打印客户值。

此外,任何关于更新节点先前字段的语法的建议/链接将非常有帮助:p

提前谢谢你

工作(无用户控制):

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXNAMELEN 19
#define MAXADDRLEN 49
#define MAXCITYLEN 19
#define MAXSTATELEN 2
struct aNode {
    char name[MAXNAMELEN];
    char address[MAXADDRLEN];
    char city[MAXCITYLEN];
    char state[MAXSTATELEN];
    int zip;
    float balance;
    struct aNode *next;
    struct aNode *prev; 
};
typedef struct aNode node;
struct aNode *dLList;
void read();
void input();
void print();
void delete();
int insertSort(node*);
int main() {
    //from text file
    read();
    input();  
        /*
    int choice;
    while(1) {
        printf("n1.INSERTn2.DELETEn3.DISPLAYn4.ENDn");
        printf("Enter choice:");
        scanf("%d",&choice);
        switch(choice) {
        case 1:
            input();
            break;
        case 2:
            delete();
            break;
        case 3:
            print();
            break;
        case 4:
            exit(0);
        }
    }
    */
    return(0);
}
//TODO
void read() {
}
void input() {
    struct aNode *current;
    current = (struct aNode *)malloc(sizeof(struct aNode));
    int buff = 120;
    char temp[buff];
    printf("Enter data: ");
    fgets(temp, buff, stdin);
    sscanf(temp, "%[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %d%*c %f", 
           current->name, current->address, current->city, current->state,   
           &current->zip, &current->balance);                   
    insertSort(current);
    //only for testing
    printf("%s, %s, %s, %s, %5d, $%9.2fn", 
            current->name, current->address, current->city, 
            current->state, current->zip, current->balance);
}
void print() {
    struct aNode *p;
    for(p = dLList; p != NULL; p = p->next) {
        printf("%s, %s, %s, %s, %5d, $%9.2fn", 
               p->name, p->address, p->city, 
               p->state, p->zip, p->balance);
    }
}
//TODO
void delete() {
}
int insertSort(node * current) {
     struct aNode *p;
    struct aNode *q;
    p = (struct aNode *)malloc(sizeof(struct aNode));
    p = current;
    //need to link to previous node   
    if(dLList == NULL || strcmp(dLList->name, p->name) > 0) {
        p->next = dLList;
        p->prev = NULL;
        return(0);
    }else {
        q = dLList;
        while(q->next != NULL && strcmp(q->next->name, q->name) < 0) {
            q = q->next;
        }   
        p->next = q->next;
        q->next = p;
        return(0);
    }   
}

输出:mike, 404 forbidden st, raleigh, nc, 27607, $ 123.78

不工作(与用户控制):

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXNAMELEN 19
#define MAXADDRLEN 49
#define MAXCITYLEN 19
#define MAXSTATELEN 2
struct aNode {
    char name[MAXNAMELEN];
    char address[MAXADDRLEN];
    char city[MAXCITYLEN];
    char state[MAXSTATELEN];
    int zip;
    float balance;
    struct aNode *next;
    struct aNode *prev; 
};
typedef struct aNode node;
struct aNode *dLList;
void read();
void input();
void print();
void delete();
int insertSort(node*);
int main() {
    //from text file
    read();
    //input();
    int choice;
    while(1) {
        printf("n1.INSERTn2.DELETEn3.DISPLAYn4.ENDn");
        printf("Enter choice:");
        scanf("%d",&choice);
        switch(choice) {
        case 1:
            input();
            break;
        case 2:
            delete();
            break;
        case 3:
            print();
            break;
        case 4:
            exit(0);
        }
    }
    return(0);
}
//TODO
void read() {
}
void input() {
    struct aNode *current;
    current = (struct aNode *)malloc(sizeof(struct aNode));
    int buff = 120;
    char temp[buff];
    printf("Enter data: ");
    fgets(temp, buff, stdin);
    sscanf(temp, "%[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %d%*c %f", 
           current->name, current->address, current->city, current->state,   
           &current->zip, &current->balance);                   
    insertSort(current);
    //only for testing
    printf("%s, %s, %s, %s, %5d, $%9.2fn", 
               current->name, current->address, current->city, 
               current->state, current->zip, current->balance);
}
void print() {
    struct aNode *p;
    for(p = dLList; p != NULL; p = p->next) {
        printf("%s, %s, %s, %s, %5d, $%9.2fn", 
              p->name, p->address, p->city, 
              p->state, p->zip, p->balance);
    }
}
//TODO
void delete() {
}
int insertSort(node * current) {
     struct aNode *p;
    struct aNode *q;
    p = (struct aNode *)malloc(sizeof(struct aNode));
    p = current;
    //need to link to previous node   
    if(dLList == NULL || strcmp(dLList->name, p->name) > 0) {
        p->next = dLList;
        p->prev = NULL;
        return(0);
    }else {
        q = dLList;
        while(q->next != NULL && strcmp(q->next->name, q->name) < 0) {
            q = q->next;
        }   
        p->next = q->next;
        q->next = p;
        return(0);
    }   
}

输出:,,,,0,$ 0.00

问题是scanf不使用选择字符串末尾的换行符。然后由input()中的fgets调用读取,因此输入字符串为空(除了n字符)。

这个为我工作:

fgets(temp, buff, stdin);
sscanf(temp, "%d",&choice);

dLList在任何地方初始化为NULL吗?
即使它是,当您将新节点插入空列表时,它也不会被设置为第一个节点。

相关内容

  • 没有找到相关文章

最新更新