c语言指针选民投票,C语言问题 链表投票

【问题描述】用链表实现对候选人的得票进行统计。函数Statistic的输入参数:head指向链首,name存放候选人的姓名。该函数的功能为:若在链表的结点上找到name,则将姓名为name的结点上得得票数加1;否则新建一个结点,初始化其姓名和的票数,并将新结点插入链尾。最后返回链表的首指针。

【输入形式】依次输入候选人姓名,姓名之间以空格为间隔符。输入0表示结束输入。

【输出形式】姓名:票数

【样例输入】q w e r q e e 0

【样例输出】

q:2

w:1

e:3

r:1

我的代码

include

include

include

struct Node

{

char name[12];

int count;

Node *next;

};

Node Statistic(Nodehead, char *name)

{

Node *p1 = head;

Node *p2=head;

if (head == 0)

{

head = new Node;

strcpy(head->name, name);

head->count = 1;

head->next = 0;

}

else

{

while (p1)

{

if (strcmp(p1->name, name) == 0)

{

p1->count++;

break;

}

else

{

p2 = p1;

p1 = p1->next;

}

}

if (p1 = NULL)

{

p1 = new Node;

strcpy(p1->name, name);

p1->count = 1;

p1->next = 0;

p2->next = p1;

}

}

return head;

}

void List(Node *head)

{

while (head)

{

printf("%s:%d\n", head->name, head->count);

head = head->next;

}

}

void Free(Node *head)

{

Node *p;

while (head) {

p = head;

head = head->next;

delete p;

}

}

void main()

{

Node *head = 0;

char name[12];

scanf("%s", &name);

while (strcmp(name, "0") != 0)

{

head = Statistic(head, name);

scanf("%s", &name);

}

List(head);

Free(head);

system("pause");

}

各位大大 为什么我的代码只能输入第一个人的票数呢 哪里错了?请帮我看一下 谢谢各位!!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,那我可以为您解释一下如何用 C 语言的指针链表来实现投票选人的功能。 首先,我们需要定义一个结构体来表示每个候选人的信息,包括姓名和得票数。定义如下: ```c struct candidate { char name[20]; int votes; }; ``` 然后,我们可以定义一个指针链表来存储所有候选人的信息。每个节点包含一个指向候选人结构体的指针和一个指向下一个节点的指针。定义如下: ```c struct node { struct candidate *data; struct node *next; }; ``` 接下来,我们可以实现一些基本的操作,例如创建一个新节点、添加节点到链表中、从链表中删除节点等等。这些操作可以通过指针操作来完成。示例代码如下: ```c // 创建一个新节点 struct node *new_node(struct candidate *data) { struct node *new = malloc(sizeof(struct node)); new->data = data; new->next = NULL; return new; } // 添加节点到链表中 void add_node(struct node **head, struct node *new) { if (*head == NULL) { *head = new; } else { struct node *current = *head; while (current->next != NULL) { current = current->next; } current->next = new; } } // 从链表中删除节点 void remove_node(struct node **head, struct candidate *data) { struct node *current = *head; struct node *prev = NULL; while (current != NULL && current->data != data) { prev = current; current = current->next; } if (current != NULL) { if (prev != NULL) { prev->next = current->next; } else { *head = current->next; } free(current); } } ``` 最后,我们可以编写一个简单的程序来实现投票选人的功能。程序可以读取用户输入的候选人姓名,然后在链表中查找该候选人并将其得票数加一。如果该候选人不存在,则创建一个新的节点并添加到链表中。示例代码如下: ```c int main() { struct node *head = NULL; char name[20]; while (1) { printf("请输入候选人姓名(输入 q 结束):"); scanf("%s", name); if (strcmp(name, "q") == 0) { break; } struct candidate *data = NULL; struct node *current = head; while (current != NULL && strcmp(current->data->name, name) != 0) { current = current->next; } if (current != NULL) { current->data->votes++; } else { data = malloc(sizeof(struct candidate)); strcpy(data->name, name); data->votes = 1; add_node(&head, new_node(data)); } } printf("投票结果:\n"); struct node *current = head; while (current != NULL) { printf("%s: %d 票\n", current->data->name, current->data->votes); current = current->next; } return 0; } ``` 以上就是一个简单的用 C 语言指针链表实现投票选人的例子。希望对您有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值