C、双向链表问题
我正在尝试读取用户关于客户的输入。
输入: 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,
¤t->zip, ¤t->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禁止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.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,
¤t->zip, ¤t->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
I'm trying to read input from the user about a customer.
Input: mike,404 forbidden st,raleigh,nc,27607,123.78
Then add that customer to a alphabetically sorted, doubly linked list. The user can choose to insert an entry, delete and entry, or view the list. After adding the user control...my sscanf doesn't work properly anymore. I can not figure out why. I don't understand why I'm not able to print customer values in the user control version.
Also, any advice/links about the syntax of updating a nodes previous field would be very helpful :P
Thank you in advance
Working (without user control):
#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,
¤t->zip, ¤t->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: mike, 404 forbidden st, raleigh, nc, 27607, $ 123.78
Not Working (with user control):
#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,
¤t->zip, ¤t->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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是
scanf
不消耗选择字符串末尾的换行符。然后由input()
中的fgets
调用读取它,因此输入字符串为空(除了\n
字符)。这对我有用:
The problem is that
scanf
doesn't consume the newline at the end of the choice string. It is then read by thefgets
call ininput()
, so the input string is empty (apart from the\n
character).This worked for me:
dLList
是否在任何地方被初始化为 NULL?即使是,当您将新节点插入空列表时,它似乎也不会设置为第一个节点。
Is
dLList
being initialized to NULL anywhere?And if even if it is, is does not appear to get set to the first node when you insert a new node into an empty list.