gpt4 book ai didi

c - 在 C 中使用堆栈

转载 作者:行者123 更新时间:2023-11-30 15:37:44 25 4
gpt4 key购买 nike

我正在尝试实现以下功能它接受一行字符串作为输入,然后将它们标记化并将它们放入堆栈中,然后打印缓冲区反向“欢迎来到书房”谁会出现书房欢迎

到目前为止,我遇到的问题是它停止工作,调试后我意识到在 main 中调用的推送函数中, token 的值没有传递给该函数。

任何人都可以帮我解决为什么它不将 token 字符串传递给推送函数吗?我认为“char* data;”有问题在结构中

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// self-referential structure
struct stackNode
{
char* data;
struct stackNode *pNext;
};

typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;

// function prototypes
void push( StackNodePtr *pTop, char value );
//int pop( StackNodePtr *pTop );
//int isEmpty( StackNodePtr pTop );
void printStack( StackNodePtr pCurrent );


int main( void )
{
char *pToken = NULL;
int counter;
char input[BUFFER_SIZE];
StackNodePtr pStack = NULL;

printf("Please enter a line of text here :\n");
gets(input);

pToken = strtok(input, " ");

while(pToken != NULL)
{
push(&pStack, pToken);
printf("%p '%s'\n", pToken, pToken);
pToken = strtok(NULL, " ");
}
printf("I am out of while loop");
printStack(pStack);
return 0;
}


// Insert a node at the stack top
void push( StackNodePtr *pTop, char* value )
{
StackNodePtr pNew;

pNew = malloc( sizeof( StackNode ) );

if ( pNew != NULL )
{
pNew->data = value;
pNew->pNext = *pTop; // insert at top of stack
*pTop = pNew;
}
else
{
printf( "%d not inserted. No memory available.\n", value );
}
}

// output stack contents to the console
void printStack( StackNodePtr pCurrent )
{
if ( pCurrent == NULL )
{
printf( "The stack is empty.\n\n" );
}
else
{
printf( "The stack is:\n" );

while ( pCurrent != NULL )
{

printf( "%s", pCurrent->data );
pCurrent = pCurrent->pNext; // move to next element
}
printf( "NULL\n\n" );
}
}

最佳答案

这有效:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFFER_SIZE 256

// self-referential structure
struct stackNode
{
char* data;
struct stackNode *pNext;
};

typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;

// function prototypes
void push(StackNodePtr* pTop, char* value);
//int pop( StackNodePtr *pTop );
//int isEmpty( StackNodePtr pTop );
void printStack( StackNodePtr pCurrent );


int main( void )
{
char *pToken = NULL;
int counter;
char input[BUFFER_SIZE];
StackNodePtr pStack = NULL;

printf("Please enter a line of text here :\n");
gets(input);

pToken = strtok(input, " ");

while(pToken != NULL)
{
push(&pStack, pToken);
printf("%p '%s'\n", pToken, pToken);
pToken = strtok(NULL, " ");
}
printf("I am out of while loop");
printStack(pStack);
return 0;
}


// Insert a node at the stack top
void push( StackNodePtr* pTop, char* value )
{
StackNodePtr pNew;

pNew = (StackNode*)malloc( sizeof( StackNode ) );

if ( pNew != NULL )
{
pNew->data = value;
pNew->pNext = *pTop; // insert at top of stack
*pTop = pNew;
}
else
{
printf( "%s not inserted. No memory available.\n", value );
}
}

// output stack contents to the console
void printStack( StackNodePtr pCurrent )
{
if ( pCurrent == NULL )
{
printf( "The stack is empty.\n\n" );
}
else
{
printf( "The stack is:\n" );

while ( pCurrent != NULL )
{

printf( "%s", pCurrent->data );
pCurrent = pCurrent->pNext; // move to next element
}
printf( "NULL\n\n" );
}
}

输入:

welcome to the den

输出:

0x7fff31d13fc0  'welcome'
0x7fff31d13fc8 'to'
0x7fff31d13fcb 'the'
0x7fff31d13fcf 'den'
I am out of while loopThe stack is:
denthetowelcomeNULL

我实际上只是修改了代码以在合理的机器上进行编译。只需确保您没有将字符数组打印为十进制类型即可。我没有修复任何其他问题,所以不能保证,只是这个位可以编译并运行。我假设您稍后会添加它。只需确保您正在跟踪内存分配即可。

关于c - 在 C 中使用堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22108136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com