我使用链表实现在 C 语言中创建了一个堆栈。当我尝试输入字符串时,我遇到了问题。在我推送到堆栈的每个节点中,所有节点中的字符串都等于顶部节点的字符串值。例如:
推>行:9座位字母:A名称: 美国食品药品监督管理局
推>行:3座信:F名称: JFJFJJF
流行<-行:3座信:F名称: JFJFJJF
啪<-行:9座位字母:A名称: JFJFJJF
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int rowNumber;
char seat;
char* passengerName;
struct passengerNode* nextPtr;
} PassengerStack, *PassengerStackPtr;
// prototypes
void top(PassengerStackPtr* topPtr);
void push(PassengerStackPtr* topPtr, int rowNum, char seatLetter, char passengersName[]);
PassengerStackPtr pop(PassengerStackPtr* topPtr);
void printStack(PassengerStackPtr currentPtr);
int full(PassengerStackPtr topPtr);
int empty(PassengerStackPtr topPtr);
int main(void)
{
PassengerStackPtr passPtr = NULL;
int choice;
int rowNumber;
char seat;
char passengerName[50];
printf("Enter choice: n1 to show the first passenger in the stack. n2 to push a passenger in the stack. n3 to "
"pop off a passenger in the stack. n4 to print the stack. n5 to exit the program.");
scanf("%d", &choice);
while(choice != 5) {
switch(choice) {
case 1:
top(passPtr);
break;
case 2:
printf("nEnter a row number: ");
scanf("%d", &rowNumber);
printf("nEnter a seat letter: ");
scanf("%c", &seat);
scanf("%c", &seat); //skips over if I don't duplicate (don't know why)
printf("nEnter a name for the passenger: ");
scanf("%s", &passengerName);
printf("Entered: n");
printf("%d", rowNumber);
printf("n");
printf("%c", seat);
printf("n");
printf("%s", passengerName);
printf("n");
push(&passPtr, rowNumber, seat, passengerName);
break;
case 3:
if(!empty(passPtr)) {
PassengerStackPtr poppedPtr = pop(&passPtr);
printf("%d", poppedPtr->rowNumber);
printf("n");
printf("%c", poppedPtr->seat);
printf("n");
printf("%s", poppedPtr->passengerName);
printf("n");
}
break;
case 4:
printStack(passPtr);
break;
case 5:
// add exit here !!!!!!!!!!
break;
default:
printf("Invalid choice.nn");
break;
}
printf("Enter a choice: ");
scanf("%d", &choice);
}
printf("End of run.n");
return 0;
}
void push(PassengerStackPtr* topPtr, int rowNum, char seatLetter, char* passengersName)
{
PassengerStackPtr newPtr;
newPtr = malloc(sizeof(PassengerStackPtr));
if(newPtr != NULL) {
newPtr->rowNumber = rowNum;
newPtr->seat = seatLetter;
newPtr->passengerName = passengersName;
newPtr->nextPtr = *topPtr;
*topPtr = newPtr;
} else {
printf("not inserted. No memory available.n");
}
}
PassengerStackPtr pop(PassengerStackPtr* topPtr)
{
PassengerStackPtr tempPtr;
tempPtr = *topPtr;
*topPtr = (*topPtr)->nextPtr;
return tempPtr;
free(tempPtr);
}
void top(PassengerStackPtr* topPtr)
{
}
void printStack(PassengerStackPtr currentPtr)
{
if(currentPtr == NULL) {
printf("The stack is empty.nn");
} else {
printf("The stack is:n");
while(currentPtr != NULL) {
printf("%d --> ", currentPtr->rowNumber);
printf("%c --> ", currentPtr->seat);
printf("%s --> ", currentPtr->passengerName);
currentPtr = currentPtr->nextPtr;
}
printf("NULLnn");
}
}
int full(PassengerStackPtr topPtr)
{
}
int empty(PassengerStackPtr topPtr)
{
return topPtr == NULL;
}
newPtr->passengerName = passengersName;
这是不正确的,因为它会导致所有节点都指向同一地址。字符串不能在 C 中使用赋值进行复制。您需要使用类似 strdup
或 malloc
然后strcpy
.
newPtr->passengerName = strdup(passengersName);
不要忘记在不再需要这些字符串时free
它们。