C 使用链表反向打印字符串

C 使用链表反向打印字符串,c,list,debugging,data-structures,C,List,Debugging,Data Structures,我构建了一个程序,从命令行获取一个字符串,并使用一个链表反向打印它 我目前正在调试我的程序,我完全被卡住了。我有一种感觉,大多数都与记忆有关 /* Takes a string from the command line. Makes a linked-list out of it in reverse order. Traverse it to construct a string in reverse. Clean up (release memory). */ #inclu

我构建了一个程序,从命令行获取一个字符串,并使用一个链表反向打印它

我目前正在调试我的程序,我完全被卡住了。我有一种感觉,大多数都与记忆有关

/*

Takes a string from the command line.  
Makes a linked-list out of it in reverse order.  
Traverse it to construct a string in reverse.  
Clean up (release memory).
*/

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

typedef struct st_CharNode 
{
char theChar;
struct st_CharNode *next;
} CharNode;


void reverseIt( char *stringbuffer );


int main( int argc, char *argv[] )
{
char *stringBuffer;

//  Check number of user supplied arguments.  
if( argc != 2 )
{
    fprintf( stderr, "usage: %s string.  This reverses the string "
             "given on the command line\n" );
    exit( -1 );
}

// Copy the argument so we can make changes to it
stringBuffer = malloc( strlen(argv[1]) );
strcpy( argv[1], stringBuffer );

// Reverse the string
reverseIt( stringBuffer );

// Print the reversed string
printf( "the reversed string is '%s'\n", *stringBuffer );

return 0;
}


// Build a linked list backwards, then traverse it.

void reverseIt( char *stringbuffer )
{
CharNode *head, *node;
char *scan, *stop;

// initialize local vars
head = node = NULL;

// find the start and end of the string so we can walk it
scan = stringbuffer;
stop = stringbuffer + strlen(stringbuffer) + 1;

// walk the string
while (scan < stop)
{
    if (head == NULL)
    {
        head = malloc( sizeof(CharNode*) );
        head->theChar = *scan;
        head->next = NULL;
    }
    else
    {
        node = malloc( sizeof(CharNode*) );
        node->theChar = *scan;
        node->next = head;
        head = node;
    }
    scan++;
}

// Re-point to the buffer so we can drop the characters
scan = stringbuffer;

//  Traverse the nodes and add them to the string
while( head != NULL )
{
    *scan = head->theChar;
    free( head );
    node = head->next;
    head = node;
    scan++;
}
// Release head
free( head );   
}
/*
从命令行获取字符串。
以相反的顺序从中生成一个链表。
遍历它以反向构造字符串。
清理(释放内存)。
*/
#包括
#包括
#包括
类型定义结构st_CharNode
{
炭黑;
struct st_CharNode*next;
}夏诺德;
无效反转(字符*字符串缓冲区);
int main(int argc,char*argv[])
{
字符*字符串缓冲区;
//检查用户提供的参数数。
如果(argc!=2)
{
fprintf(stderr,“用法:%s字符串。这将反转字符串”
“在命令行上给定\n”);
出口(-1);
}
//复制参数,以便我们可以对其进行更改
stringBuffer=malloc(strlen(argv[1]);
strcpy(argv[1],stringBuffer);
//把绳子倒过来
反转(stringBuffer);
//打印反向字符串
printf(“反转的字符串是'%s'\n',*stringBuffer);
返回0;
}
//向后建立一个链表,然后遍历它。
void reverseIt(字符*stringbuffer)
{
CharNode*头,*节点;
字符*扫描,*停止;
//初始化本地变量
head=node=NULL;
//找到绳子的起点和终点,这样我们就可以走了
扫描=字符串缓冲区;
stop=stringbuffer+strlen(stringbuffer)+1;
//循序渐进
while(扫描<停止)
{
if(head==NULL)
{
head=malloc(sizeof(CharNode*));
头部->头部=*扫描;
head->next=NULL;
}
其他的
{
node=malloc(sizeof(CharNode*));
node->theChar=*扫描;
节点->下一步=头部;
头部=节点;
}
扫描++;
}
//重新指向缓冲区,这样我们可以删除字符
扫描=字符串缓冲区;
//遍历节点并将其添加到字符串中
while(head!=NULL)
{
*扫描=头部->颈部;
自由(头);
节点=头部->下一步;
头部=节点;
扫描++;
}
//释放头
自由(头);
}

我看到的一个错误是,您使用strcpy的方式如下:

strcpy( argv[1], stringBuffer );
它将stringBuffer的值复制到argv[1]中,您应该以另一种方式执行,如下所示:

strcpy( stringBuffer, argv[1]);

有关更多信息,请参阅strcpy手册页:

“我完全卡住了”。这不是对问题的充分描述。请说明输入、预期行为、实际行为、您自己调试它所做的工作以及您的具体问题(“为我调试它”不是一个具体的问题)。仅指标题:为什么有人会这样做?从头到尾遍历字符串,并将字符复制到新分配的相同大小的缓冲区中。我见过很多关于链表概念的“滥用”,但这似乎是“最好的”之一。你可以把链表当作一个堆栈来使用。创建新元素时,将其下一个指针设置为指向第一个元素结构,然后将第一个元素指针设置为指向新元素结构。在用户停止键入后,您只需从第一个指针浏览列表,因为它现在已经被重新显示。@Lászlókardinal这是真的,但我仍然在想:为什么要使用链接列表?应用资源浪费?当您需要设计更高级的数据结构时,学习链表将非常有用。在您的生活中,您可能不会实现任何其他链表,因为将有用于此的现有库,但是当您遇到类似问题时,您将需要这些结构的逻辑。