C语言单链表实现employee栈:栈顶薪资比较失效与打印功能完善
代码问题定位与修正
入栈薪资对比失效的根因
- 现有
push函数实现正常:新节点申请内存后,next指针指向入栈前的旧栈顶,再将head指针移到新节点位置完成入栈。入栈成功后,新员工已经是新的栈顶,被head直接指向。 - 原判断逻辑完全错位:
if (data.salary > head->salary)语句中,data是刚读入的待入栈员工,head此时已经指向刚入栈的新员工节点,本质是员工自己和自己比薪资,永远拿不到正确对比结果。 - 存在隐藏崩溃点:原代码中
printf("%f\n", data.next->salary)操作的是main函数局部变量data,这个变量从未给next成员赋值,是未初始化的野指针,运行到该行会直接触发内存访问错误。 - 正确的对比对象:入栈完成后,入栈前的旧栈顶就是新栈顶节点
next指针指向的位置。如果旧栈顶为NULL(即新节点的next是空),说明这是栈内第一位员工,没有可对比的旧栈顶,直接提示即可;如果存在旧栈顶,再做薪资高低判断。
栈打印的去重实现
- 原有
show函数直接从栈顶到栈底遍历打印,没有做重复判断。每个员工的增值税编号am是唯一身份标识,只要两个节点的am值相同,就属于同一位员工的重复存储,需要跳过。 - 按照栈的后进先出特性,离栈顶越近的节点是越晚入栈的最新数据,去重时需要保留离栈顶最近的重复节点,跳过更靠近栈底的旧重复项。遍历从栈顶往栈底推进,每遇到一个节点,就回头检查已经遍历过的、更靠近栈顶的节点是否存在相同
am的记录,存在就跳过当前节点,完成去重。
修正后的完整可运行代码
#include <stdio.h> #include <stdlib.h> struct employee { int am; char name[50]; float salary; struct employee *next; }; void init(struct employee **head); void show(struct employee *head); int push(struct employee s, struct employee **head); int pop(struct employee *s, struct employee **head); int main(){ struct employee data; struct employee *head; int ch; init(&head); do { printf("\nMenu "); printf("\n1.Push "); printf("\n2.Pop "); printf("\n3.Show contents "); printf("\n0.Exit "); printf("\nChoice: "); scanf("%d",&ch); switch (ch) { case 1: printf("Enter employee data (am name salary):"); scanf("%d %s %f", &data.am, data.name, &data.salary); if (push(data,&head)){ printf("Successfully pushed to stack.\n"); // 入栈后新节点是新栈顶,head->next就是入栈前的旧栈顶 struct employee *old_top = head->next; if (old_top == NULL) { printf("这是栈内第一位员工,无历史栈顶可做薪资对比\n"); } else { if (data.salary > old_top->salary) { printf("新员工薪资(%.2f)高于原栈顶员工(%.2f)薪资\n", data.salary, old_top->salary); } else if (data.salary < old_top->salary) { printf("新员工薪资(%.2f)低于原栈顶员工(%.2f)薪资\n", data.salary, old_top->salary); } else { printf("新员工薪资(%.2f)与原栈顶员工(%.2f)薪资相等\n", data.salary, old_top->salary); } } } else printf("No memory available, push failed.\n"); break; case 2: if (pop(&data,&head)) printf("Popped: %d %s %.1f\n",data.am, data.name, data.salary); else printf("Empty stack, no data retrieved.\n"); break; case 3: show(head); break; case 0: printf("Bye!\n"); break; default: printf("Try again.\n"); break; } } while (ch!=0); exit(0); } void init(struct employee **head){ *head = NULL; } int push(struct employee d, struct employee **head) { int done=0; struct employee *s; s = (struct employee *)malloc(sizeof(struct employee)); if (s!=NULL) { *s = d; s->next = *head; *head = s; done = 1; } return done; } int pop(struct employee *s, struct employee **head) { int done=0; struct employee *temp; if (*head != NULL) { temp = *head; *s = *(*head); // 优化可读性,等价于原代码的多重重写引用 *head = (*head)->next; free(temp); done = 1; } return done; } void show(struct employee *head) { struct employee *i, *check; int is_duplicate; printf("--- Top of stack ---\n"); for (i = head; i != NULL; i = i->next) { is_duplicate = 0; // 从栈顶遍历到当前节点之前的位置,检查am是否重复 for (check = head; check != i; check = check->next) { if (check->am == i->am) { is_duplicate = 1; break; } } if (!is_duplicate) { printf("%d %s %.2f\n", i->am, i->name, i->salary); } } printf("---Bottom of stack---\n"); }
内容的提问来源于stack exchange,提问作者user15221060
相关产品推荐
相关产品推荐

