主题:怎么FOR不能实现循环啊
#include "iostream.h"
#include "stdlib.h"
#include "stdio.h"
typedef int elemtype;
typedef struct node
{elemtype data;
struct node *next;
}lklist;
//建立单链表
void crea(lklist *L)
{lklist *p;
elemtype x;
cin>>x;
while(x!=-1)
{p=(lklist*)malloc(sizeof(lklist));
p->data=x; p->next=L->next;
L->next=p; cin>>x;
}
}
//输出单链表
void print(lklist *h)
{ lklist *p;
for(p=h->next;p;p=p->next)
cout<<p->data<<" ";
}
//被调函数定义
void sandeng(lklist *h)
{lklist *p,*q,*s;s=h->next;
for(p=h->next;p;p=p->next)
for(q=s->next;q;q=q->next)
{if(q->data==p->data) s->next=q->next;
else s=s->next;}
}
//主函数
void main()
{ lklist *hl;
hl=(lklist*)malloc(sizeof(lklist));
hl->next=NULL;
cout<<endl<<"建立单链表:";
crea(hl);
//输出单链表
cout<<endl<<"输出原单链表:";
print(hl);
//调用函数
sandeng(hl);
//输出处理以后的单链表
cout<<endl<<"输出新单链表:";
print(hl);
}
#include "stdlib.h"
#include "stdio.h"
typedef int elemtype;
typedef struct node
{elemtype data;
struct node *next;
}lklist;
//建立单链表
void crea(lklist *L)
{lklist *p;
elemtype x;
cin>>x;
while(x!=-1)
{p=(lklist*)malloc(sizeof(lklist));
p->data=x; p->next=L->next;
L->next=p; cin>>x;
}
}
//输出单链表
void print(lklist *h)
{ lklist *p;
for(p=h->next;p;p=p->next)
cout<<p->data<<" ";
}
//被调函数定义
void sandeng(lklist *h)
{lklist *p,*q,*s;s=h->next;
for(p=h->next;p;p=p->next)
for(q=s->next;q;q=q->next)
{if(q->data==p->data) s->next=q->next;
else s=s->next;}
}
//主函数
void main()
{ lklist *hl;
hl=(lklist*)malloc(sizeof(lklist));
hl->next=NULL;
cout<<endl<<"建立单链表:";
crea(hl);
//输出单链表
cout<<endl<<"输出原单链表:";
print(hl);
//调用函数
sandeng(hl);
//输出处理以后的单链表
cout<<endl<<"输出新单链表:";
print(hl);
}