【问题标题】:C, doubly linked list problemC、双向链表问题
【发布时间】:2011-04-27 18:36:06
【问题描述】:

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

输入:mike,404 禁止 st,raleigh,nc,27607,123.78

然后将该客户添加到按字母顺序排序的双向链表中。用户可以选择插入条目、删除条目或查看列表。添加用户控件后...我的 sscanf 不再正常工作。我不知道为什么。我不明白为什么我无法在用户控制版本中打印客户值。

此外,任何有关更新节点先前字段的语法的建议/链接都会非常有帮助: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.INSERT\n2.DELETE\n3.DISPLAY\n4.END\n");
        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.2f\n", 
            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.2f\n", 
               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 禁止街,罗利,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.INSERT\n2.DELETE\n3.DISPLAY\n4.END\n");
        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.2f\n", 
               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.2f\n", 
              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

【问题讨论】:

    标签: c linked-list


    【解决方案1】:

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

    这对我有用:

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

    【讨论】:

    • 工作,谢谢 :) 你说“为你工作”?你在做和我一样的事情吗? :P 关于 AShelly 在下面写下的任何建议都会有所帮助。我不太明白他/她在暗示什么。
    • @beowulf3:第一次插入时,dLList 仍然为 NULL。您需要它指向列表的第一个元素,即被插入的元素。这就是为什么在insertSort()if 语句的主体中添加dLList = p; 可以使您的代码正常工作。
    • 哦,噗。我傻。但是第二次插入的时候怎么处理,即insertSort()中的if语句为假?
    【解决方案2】:

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

    【讨论】:

    • 不,唯一的“= NULL”出现在 insertSort() 中。 "p->prev = NULL"
    • 所以它根本没有被初始化。您需要在开始时将其设置为 NULL,然后在 InsertSort 中,每当您在列表的头部(包括第一个)插入一个节点时执行dLList = current;
    • 从 main() 开始?还是在那之前?我之前说过:错误很多。之后:同样的不打印问题。我放置了“dLList = p;”在 insertSort 的 if 语句中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 2011-08-07
    • 2017-08-18
    • 1970-01-01
    相关资源
    最近更新 更多