堆栈 - 链接列表实现 - 字符串未正确使用

时间:2016-07-10 22:30:43

标签: c linked-list stack

我使用链表实现在C中创建了一个堆栈。当我尝试输入字符串时,我遇到了麻烦。在我推送到堆栈的每个节点中,所有节点中的字符串等于顶部节点的字符串的值。 例如:

推> 行:9 seatletter:一 名称:fsda

推> 行:3 seatletter:F 名称:jfjfjjf

pop< - 行:3 seatletter:F 名称:jfjfjjf

弹出< - 行:9 seatletter:一 名称: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.\n\n");
        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.\n\n");
    } 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("NULL\n\n");
    }
}

int full(PassengerStackPtr topPtr)
{
}

int empty(PassengerStackPtr topPtr)
{

    return topPtr == NULL;
}

1 个答案:

答案 0 :(得分:2)

newPtr->passengerName = passengersName;

这是不正确的,因为它导致所有节点指向相同的地址。无法使用C中的分配复制字符串。您需要使用strdupmalloc然后strcpy等内容复制名称。

newPtr->passengerName = strdup(passengersName);

当不再需要这些字符串时,不要忘记free