|
程序是将192.168.100.2和192.168.100.3调用append函数,存放到链表里,然后调用travel函数输出这两个字符串,现在是指能输出192.168.100.3,请大家帮忙看看,问题出在哪里?
#include<malloc.h>
#include<string.h>
#include <stdio.h>
#define null 0
typedef struct node
{
char data[20];
struct node *next;
}slnode;
struct node *h,*r;
int flag=0;
slnode *initiate()
{
h=r=(struct node*)malloc(sizeof(slnode));
h->next=null;
return h;}
//添加结点
void append(slnode *r,char str[20])
{
slnode *s;
s=(slnode*)malloc(sizeof(slnode));
strcpy(s->data,str);
s->next = null;
printf("%s\n",s->data);
r->next=s;
r=s;
flag++;
printf("flag=%d\n",flag);
}
//遍历结点
void travel(slnode *h)
{
slnode *p,*q;
char str[20];
p=h->next;
if(flag>0)
do
{
strcpy(str,p->data);
printf("%s\n",str);
q=p;
p=p->next;
h->next=p;
free(q);
flag--;
printf("%d\n",flag);
}
while(p!=NULL);
else{printf("Slnode isempty!\n");}
}
main()
{
char str1[20]="192.168.100.2";
char str2[20]="192.168.100.3";
h = initiate();
append(r,str1);
append(r,str2);
travel(h);
return 0;
} |
|