簡體   English   中英

C,雙向鏈表問題

[英]C, doubly linked list problem

我正在嘗試讀取用戶關於客戶的輸入。

輸入: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);
    }   
}

Output:麥克,404 禁止街,羅利,北卡羅來納州,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);
    }   
}

Output: , , , , 0, $ 0.00

問題是scanf不使用選擇字符串末尾的換行符。 然后由input()中的fgets調用讀取它,因此輸入字符串為空(除了\n字符)。

這對我有用:

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

dLList是否在任何地方被初始化為 NULL?
即使是這樣,當您將新節點插入空列表時,它似乎也不會設置為第一個節點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM