问题描述
我正在尝试在C中实现一个喜欢的llist操作.我的方法是在标头C文件中实现所有操作,并将其包含在主文件中以管理链接列表.
Hi I'm trying to implement a liked llist operation in C. My approach is to implement all operation in a header C file, and to include it in a main file to manage linked lists.
这是头文件linkedlist.h
,其中包含操作实现
Here is the header file linkedlist.h
, containing operations implementations
//Here is my code for the node structure:
struct node {
struct node * next;
char cinfo;
};
//Here is a method to do a simple insertion in the begining of the list
void beginInsertSL(char val, struct node ** root){
struct node *p = malloc(sizeof(struct node*));
p->cinfo = val;
if(*root == NULL){
p->next = NULL;
} else {
p->next = *root;
}
*root = p;
};
//Here is a method to do many insertions
void beginInsertSLN(char *ch, struct node **root){
root = malloc(sizeof(struct node**));
int size, i;
size = strlen(ch);
i = 0;
if(size > 0){
while(i < size){
beginInsertSL(ch[i], root);
printSL(*root);
i++;
}
} else *root = NULL;
}
//And finally a method to print a list, to be able to see
void printSL(struct node *root){
struct node *p;
if(root == NULL) printf("[ ]\n");
else {
p = root;
printf("[ %c", p->cinfo);
while(p->next != NULL) {p = p->next; printf("->%c", p->cinfo);}
printf(" ]\n");
}
}
这是使用这些方法的主要功能
Here is the main function to use those methods
#include "linkedlist.h"
#include <stdio.h>
int main(int argc, char *argv[]){
// declaring the root of my linkedlist
struct node **root = NULL;
if(argc > 0){
// inserting char in the linked list
beginInsertSLN(argv[1], root);
// print the linked list to see the result
printSL(*root);
}
}
执行此代码时,我得到以下结果:
When executing this code, I get the following result :
./lltest stackoverflow
[ s ]
[ t->s ]
[ a->t->s ]
[ c->a->t->s ]
[ k->c->a->t->s ]
[ o->k->c->a->t->s ]
[ v->o->k->c->a->t->s ]
[ e->v->o->k->c->a->t->s ]
[ r->e->v->o->k->c->a->t->s ]
[ f->r->e->v->o->k->c->a->t->s ]
[ l->f->r->e->v->o->k->c->a->t->s ]
[ o->l->f->r->e->v->o->k->c->a->t->s ]
[ w->o->l->f->r->e->v->o->k->c->a->t->s ]
Erreur de segmentation (core dumped)
有人可以帮助我了解我在这里缺少什么吗?
Can some one help me to understand what I'm missing here ?
推荐答案
代码中的指针一团糟
尝试以下方法.仅考虑到我没有将程序拆分为模块,而是使用了显式指定的字符串.您可以删除它或取消注释main中的字符串定义以测试程序.
Try the following. Take only into account that I did not split the program into modules and used an explicitly specified string. You may remove it or uncomment the string definition in main for testing the program.
#include <stdio.h>
#include <stdlib.h>
//Here is my code for the node structure:
struct node
{
struct node *next;
char cinfo;
};
//Here is a method to do a simple insertion in the begining of the list
void beginInsertSL( struct node **root, char c )
{
struct node *p = malloc( sizeof( struct node ) );
p->cinfo = c;
p->next = *root;
*root = p;
}
void printSL( struct node *root );
//Here is a method to do many insertions
void beginInsertSLN( struct node **root, const char *s )
{
while ( *s )
{
beginInsertSL( root, *s++ );
printSL( *root );
}
}
//Here is a method to print a list, to be able to see
void printSL( struct node *root )
{
if ( root == NULL )
{
printf( "[ ]\n" );
return;
}
printf( "[ %c", root->cinfo );
while( ( root = root->next ) != NULL )
{
printf( "->%c", root->cinfo );
}
printf(" ]\n");
}
//And finally a method to clear the list
void clear( struct node **root )
{
while ( *root )
{
struct node *tmp = *root;
*root = ( *root )->next;
free( tmp );
}
}
int main( int argc, char * argv[] )
{
struct node *root = NULL;
if ( argc > 1 ) beginInsertSLN( &root, argv[1] );
// char *s = "stackoverflow";
// beginInsertSLN( &root, s );
printSL( root );
clear( &root );
return 0;
}
输出为
[ s ]
[ t->s ]
[ a->t->s ]
[ c->a->t->s ]
[ k->c->a->t->s ]
[ o->k->c->a->t->s ]
[ v->o->k->c->a->t->s ]
[ e->v->o->k->c->a->t->s ]
[ r->e->v->o->k->c->a->t->s ]
[ f->r->e->v->o->k->c->a->t->s ]
[ l->f->r->e->v->o->k->c->a->t->s ]
[ o->l->f->r->e->v->o->k->c->a->t->s ]
[ w->o->l->f->r->e->v->o->k->c->a->t->s ]
[ w->o->l->f->r->e->v->o->k->c->a->t->s ]
这篇关于在C函数中修改指针时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!