#include <string.h>
#include <stdlib.h>
struct node
{
int data;
char name[100];
struct node *next;
};
struct node * creat ( int n ) //创建链表
{
struct node *head, *p, *q;
int i=0 ;
p = (struct node *)malloc(sizeof(struct node));
p->data = 1; //
//strcpy( p->name , "hello" ); //
//p->name[0]='\0';
head = p;
while( i<n )
{
if( (q = ( struct node * )malloc(sizeof(struct node))) == NULL )
{
printf("error!");
}
else
{
p->data = i;
strcpy( p->name , "hello" );
p->next = q;
p = q;
i = i+1;
}
}
p = head;
q->next = NULL;
return(head);
}
void output(struct node *head) //输出链表
{
struct node *p;
p = head;
while( p->next != NULL )
{
printf("%d %s\n" , p->data, p->name);
//printf("%d\n" , p->data);
p=p->next;
}
}
struct node *search( struct node *head , int f ) //查找当前结点
{
int i=0;
struct node *p;
p = head;
while( p != NULL )
{
if( f == i )
{
return(p);
}
else
{
p = p->next;
i = i+1;
}
}
}
struct node *search2( struct node *head , int f ) //查找当前结点的前一个结点
{
int i=0;
struct node *p, *q;
p = head;
q = head->next;
while( p != NULL )
{
if( f == i )
{
return(q);
//break; //return后已经返回了,不需要再用break;
}
else
{
p = p->next;
q = q->next;
i = i+1;
}
}
}
struct node * insert_head( struct node *head )
{
struct node *pnew , *p;
p = head;
pnew = (struct node *)malloc(sizeof(struct node)); //在头节点前插入结点
pnew->data = 78;
strcpy( pnew->name , "start" );
pnew->next = p;
head = pnew;
}
void insert( struct node *head ) //在中间插入
{
int f=0;
struct node *pnew , *p;
p = head;
pnew = (struct node *)malloc(sizeof(struct node));
while( p )
{
if( f == 3 )
{
strcpy( pnew->name , "zhongjian" );
pnew->data = 99;
pnew->next = p->next;
p->next = pnew;
break;
}
else
{
f = f+1;
p = p->next;
}
}
}
void insert_end( struct node *head ) //在结尾插入
{
struct node *pnew , *p , *phead;
p = head;
pnew = (struct node *)malloc(sizeof(struct node));
while( head->next != NULL )
{
head = head->next;
}
pnew->data = 22;
strcpy( pnew->name , "end" );
head->next = pnew;
pnew->next = NULL;
while( p )
{
printf("%d %s\n" , p->data, p->name);
p = p->next;
}
//p = head;
}
void del( struct node *x , struct node *y ) //删除结点
{
struct node *del;
del = y;
x->next = y->next;
free(del);
}
int main() //主函数
{
int number=5 , i=0;
struct node *head, *searchp, *searchp2, *q;
head = creat(number);
// searchp = search( head , 2 );
// searchp2 = search2( head , 2 );
//head = insert_head(head);
// insert( head );
insert_end( head );
//del( searchp , searchp2 );
//output(head);
}
4 个解决方案
#1
注意,新节点,malloc后,要把它的next置NULL
#2
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
int data;
char name[100];
struct node *next;
};
struct node * creat ( int n )
{
struct node *head, *p, *q;
int i=0 ;
p = (struct node *)malloc(sizeof(struct node));
p->data = 1;
head = p;
while (i < n) {
q = (struct node * )malloc(sizeof(struct node));
if (!q) {
printf("error!\n");
exit(0);
}
q->data = i; /* for q->data, not for p->data */
strcpy(q->name, "hello"); /*for q->name, not for p->name*/
p->next = q;
p = q;
i = i+1;
}
//p = head; /*冗余代码*/
q->next = NULL;
return(head);
}
void output(struct node *head) //输出链表
{
struct node *p;
p = head->next;
while (p)
{
printf("%d %s\n" , p->data, p->name);
p = p->next;
}
}
struct node *search( struct node *head , int f ) //查找当前结点
{
struct node *p;
p = head->next;
while(p)
{
if( f == p->data )
return(p);
p = p->next;
}
return NULL;
}
struct node *search2( struct node *head , int f ) //查找当前结点的前一个结点
{
int i=0;
struct node *p, *q;
p = head;
q = head->next;
while( p != NULL )
{
if( f == i )
{
return(q);
//break; //return后已经返回了,不需要再用break;
}
else
{
p = p->next;
q = q->next;
i = i+1;
}
}
return NULL;
}
struct node * insert_head( struct node *head )
{
struct node *pnew;
pnew = (struct node *)malloc(sizeof(struct node)); //在头节点前插入结点
pnew->data = 78;
strcpy(pnew->name , "start" );
pnew->next = head;
head = pnew;
return head;
}
void insert( struct node *head ) //在中间插入
{
int f=0;
struct node *pnew , *p;
p = head;
pnew = (struct node *)malloc(sizeof(struct node));
while( p )
{
if( f == 3 )
{
strcpy( pnew->name , "zhongjian" );
pnew->data = 99;
pnew->next = p->next;
p->next = pnew;
break;
}
else
{
f = f+1;
p = p->next;
}
}
}
void insert_end( struct node *head ) //在结尾插入
{
struct node *pnew, *p;
p = head;
pnew = (struct node *)malloc(sizeof(struct node));
if (!pnew)
exit(0);
while(p->next != NULL )
{
p = p->next;
}
pnew->data = 22;
strcpy( pnew->name , "end" );
pnew->next = NULL;
p->next = pnew;
/*
while( p )
{
printf("%d %s\n" , p->data, p->name);
p = p->next;
}
//p = head;
*/
}
void del( struct node *x , struct node *y ) //删除结点
{
struct node *del;
del = y;
x->next = y->next;
free(del);
}
int main() //主函数
{
int number=5 , i=0;
struct node *head, *searchp, *searchp2, *q;
head = creat(number);
printf("Show link: \n");
output(head);
// searchp = search( head , 2 );
// searchp2 = search2( head , 2 );
//head = insert_head(head);
// insert( head );
insert_end( head );
printf("Show link: \n");
output(head);
//del( searchp , searchp2 );
//output(head);
}
参考一下吧;
创建链表时出现结构体对象赋值错误,详见代码里的注释;
头插法也已经修改,问题是要返回新的头结点;
搜索的问题,参考函数search;
有些代码冗余,个人感觉可以去掉的,也一并修改了。参考一下吧;
#4
还有一个问题
为什么不能读我输入的文件名对应的文件,是不是要用到argv[]这个参数,怎么改呢?
void readfile(char name[])
{
FILE *fp;
char str[100];
fp = fopen( "name" , "r" );
while( !feof(fp) )
{
fgets( str , 100 , fp );
printf( "%s" , str );
}
fclose (fp);
}
int main()
{
char name[10];
scanf("%s" , name);
readfile(name);
}
为什么不能读我输入的文件名对应的文件,是不是要用到argv[]这个参数,怎么改呢?
#1
注意,新节点,malloc后,要把它的next置NULL
#2
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
int data;
char name[100];
struct node *next;
};
struct node * creat ( int n )
{
struct node *head, *p, *q;
int i=0 ;
p = (struct node *)malloc(sizeof(struct node));
p->data = 1;
head = p;
while (i < n) {
q = (struct node * )malloc(sizeof(struct node));
if (!q) {
printf("error!\n");
exit(0);
}
q->data = i; /* for q->data, not for p->data */
strcpy(q->name, "hello"); /*for q->name, not for p->name*/
p->next = q;
p = q;
i = i+1;
}
//p = head; /*冗余代码*/
q->next = NULL;
return(head);
}
void output(struct node *head) //输出链表
{
struct node *p;
p = head->next;
while (p)
{
printf("%d %s\n" , p->data, p->name);
p = p->next;
}
}
struct node *search( struct node *head , int f ) //查找当前结点
{
struct node *p;
p = head->next;
while(p)
{
if( f == p->data )
return(p);
p = p->next;
}
return NULL;
}
struct node *search2( struct node *head , int f ) //查找当前结点的前一个结点
{
int i=0;
struct node *p, *q;
p = head;
q = head->next;
while( p != NULL )
{
if( f == i )
{
return(q);
//break; //return后已经返回了,不需要再用break;
}
else
{
p = p->next;
q = q->next;
i = i+1;
}
}
return NULL;
}
struct node * insert_head( struct node *head )
{
struct node *pnew;
pnew = (struct node *)malloc(sizeof(struct node)); //在头节点前插入结点
pnew->data = 78;
strcpy(pnew->name , "start" );
pnew->next = head;
head = pnew;
return head;
}
void insert( struct node *head ) //在中间插入
{
int f=0;
struct node *pnew , *p;
p = head;
pnew = (struct node *)malloc(sizeof(struct node));
while( p )
{
if( f == 3 )
{
strcpy( pnew->name , "zhongjian" );
pnew->data = 99;
pnew->next = p->next;
p->next = pnew;
break;
}
else
{
f = f+1;
p = p->next;
}
}
}
void insert_end( struct node *head ) //在结尾插入
{
struct node *pnew, *p;
p = head;
pnew = (struct node *)malloc(sizeof(struct node));
if (!pnew)
exit(0);
while(p->next != NULL )
{
p = p->next;
}
pnew->data = 22;
strcpy( pnew->name , "end" );
pnew->next = NULL;
p->next = pnew;
/*
while( p )
{
printf("%d %s\n" , p->data, p->name);
p = p->next;
}
//p = head;
*/
}
void del( struct node *x , struct node *y ) //删除结点
{
struct node *del;
del = y;
x->next = y->next;
free(del);
}
int main() //主函数
{
int number=5 , i=0;
struct node *head, *searchp, *searchp2, *q;
head = creat(number);
printf("Show link: \n");
output(head);
// searchp = search( head , 2 );
// searchp2 = search2( head , 2 );
//head = insert_head(head);
// insert( head );
insert_end( head );
printf("Show link: \n");
output(head);
//del( searchp , searchp2 );
//output(head);
}
参考一下吧;
创建链表时出现结构体对象赋值错误,详见代码里的注释;
头插法也已经修改,问题是要返回新的头结点;
搜索的问题,参考函数search;
有些代码冗余,个人感觉可以去掉的,也一并修改了。参考一下吧;
#3
数据结构对单链表进行数据排序
http://bbs.csdn.net/topics/392201633
#4
还有一个问题
为什么不能读我输入的文件名对应的文件,是不是要用到argv[]这个参数,怎么改呢?
void readfile(char name[])
{
FILE *fp;
char str[100];
fp = fopen( "name" , "r" );
while( !feof(fp) )
{
fgets( str , 100 , fp );
printf( "%s" , str );
}
fclose (fp);
}
int main()
{
char name[10];
scanf("%s" , name);
readfile(name);
}
为什么不能读我输入的文件名对应的文件,是不是要用到argv[]这个参数,怎么改呢?