点击查看代码
typedef struct NODE {
int data;
struct NODE *next;
}NODE,*Linkedlist;
//初始化头节点
void Init(Linkedlist &L){
L=(NODE*)malloc(sizeof(NODE));
L->next=NULL;
}
//尾插法创建链表
Linkedlist CreateFromRear(Linkedlist L){
NODE *rear=L;
for(int i=0;i<6;i++){
NODE *newNode=(NODE*)malloc(sizeof(NODE));
newNode->data=i+1;
newNode->next=NULL;
rear->next=newNode;
rear=newNode;
}
}
//去除偶数节点
Linkedlist GetOdd(Linkedlist L){
NODE *ptr=L->next;
Linkedlist r=L;
r->next=NULL;
Linkedlist tail=r;
while(ptr!=NULL){
if(ptr->data%2!=0){
tail->next=ptr;
tail=ptr;
}
ptr=ptr->next;
}
tail->next=NULL;
return r;
}
int main(){
Linkedlist L;
Init(L);
CreateFromRear(L);
GetOdd(L);
return 0;
}