Reputation: 89
I have made a stack in C, using the linked list implementation. I'm having trouble with when I try to input string. In each node I push to the stack, the string in all nodes are equal to the value of the string of the top node. For example:
push-> row:9 seatletter:a name: fsda
push-> row:3 seatletter:f name: jfjfjjf
pop <- row:3 seatletter:f name: jfjfjjf
pop<- row:9 seatletter:a name: 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;
}
Upvotes: 1
Views: 58
Reputation: 14046
newPtr->passengerName = passengersName;
That is incorrect as it results in all nodes pointing to the same address. Strings can not copied using assignment in C. You need to make a copy of the name using something like strdup
or malloc
then strcpy
.
newPtr->passengerName = strdup(passengersName);
Don't forget to free
those strings when they are no longer needed.
Upvotes: 2