回 帖 发 新 帖 刷新版面

主题:有个把文件读进链表出了问题,能不能帮忙解决一下

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

struct node
{
	int data;
	char name[100];
	char valu[100];
	struct node *next;
};

struct node * creat ( int n , char *argv )                                   //创建链表
{
	struct node *head, *p, *q;
	int i=0 ,t;
	char *pp , *qq , str[100];
	head = (struct node *)malloc(sizeof(struct node));                                                     //
	p = head;
	FILE *fp;
	fp = fopen( argv , "r");
	/*while( fscanf(fp , "%d" , &t) != EOF )
	{
		if( (q = ( struct node * )malloc(sizeof(struct node))) == NULL )
		{
			printf("error!");
		}
		else
		{
			q->data = t;
			//strcpy( q->name , "hello" );
			p->next = q;
			p = q;
		}
	}*/
	while( !feof(fp) )
		{
			fgets(str , 100 , fp );
			
			pp = strchr( str , '=' );
			qq = pp + 1;
			*pp = '\0';
			pp = NULL;
			if(pp = strchr( qq , '#' ))
			{
				*pp = '\0';
			}
			if( (q = ( struct node * )malloc(sizeof(struct node))) == NULL )
			{
				printf("error!");
			}
			else
			{
				strcpy( q->name , delblank(str) );
				strcpy( q->valu , delblank(qq) );
				p->next = q;
				p = q;
			}
		}
	p->next = NULL;
	head = head->next;
	return(head);
	fclose(fp);
}


void output(struct node *head)												//输出链表
{
	struct node *p;
	// p = head->next;
	p = head;
	while( p )
	{
		printf("%s=%s\n" , p->name , p->valu);
		p=p->next;
	}
}


struct node *search( struct node *head ,  int f , int a )            			//查找当前结点
{
	struct node *p , *q;
	q = (struct node *)malloc(sizeof(struct node));
	p = head;
	q->next = p;
	
	while( p != NULL )
	{
		if( f == p->data )
		{
			if( 1 == a )
				return(p);
			else if( 2 == a )
				return (q);
		}
		else
		{
			p = p->next;
			q = q->next;
		}
	}
	return NULL;				//找不到就返回空
}



struct node * insert(  struct node *head )                  		//插入                 
{
	int sequence_number;
	struct node *pnew , *p , *end;
	p = head;
	pnew = (struct node *)malloc(sizeof(struct node));
	printf("在哪插:\n");
	scanf( "%d" , &sequence_number );
	if( (p = search( head , sequence_number , 1 )) == NULL )
	{
		printf("没有此项!\n");
		return NULL;
	}
	else
	{
		pnew->data = 201601359;
		pnew->next = p->next;
		p->next = pnew;
		// printf("修改后的链表:\n");
		return(head);
	}
}



struct node * del( struct node * head )										//删除结点
{
	int sequence_number;
	struct node *frontdel , *del , *behinddel , *end;
	del = head;
	printf("输入要删除的序号:\n");
	scanf("%d" , &sequence_number);
	if( ( del = search( head , sequence_number , 1 )) == NULL )
	{
		printf("没有此项!\n");
		return NULL;
	}
	else
	{
		del = search( head , sequence_number , 1 );
		frontdel = search( head , sequence_number , 2 );
		behinddel = del->next;
		if( head == del )
		{
			del = head->next;
			head = del;	
			// printf("删除后的链表:\n");
			return(head);
		}
		else if( del->data == sequence_number )
		{
			frontdel->next = behinddel;
			printf("删除后的链表:\n");
			return (head);
		}
	}
}

char * delblank( char *str )
{
	char *p ,*q;
	int len = 0;
	
	len = strlen(str);
	p = str;
	if( *str == ' ')
	{
		while( *p == ' ' )
		{
			p++;
		}
	}
	q = str + len - 1;
	while( *q == ' ' )
	{
		q--;
	}
	*(q + 1) = '\0';
	return p;
}

int main( int argc , char * argv[] )																		//主函数
{
	int number=5 , i=0;
	struct node *head, *searchp, *searchp2, *q;
	head = creat(number , argv[1] );
	printf("原链表:\n");
	output(head);
	printf("\n\n");
	// searchp = search( head , 201601322 , 1 );
	// head = insert( head );
	// head = del( head );
	// output(head);
}

回复列表 (共1个回复)

沙发

你用fread数块读写方式看看,我一般都比较少用fgets读,比较容易出错,

我来回复

您尚未登录,请登录后再回复。点此登录或注册