輸入若干個正整數(輸入-1為結束標志)建立一個單向鏈表,頭指針為L,將鏈表L中奇數值的結點重新組成一個新的鏈表NEW,並輸出新建鏈表的信息。
第一種方法:逆向思維
#include<stdio.h> #include<stdlib.h> struct stu_node{ int num; struct stu_node*next; }; struct stu_node*Create_Stu_Doc();/*新建鏈表*/ struct stu_node*DeleteDoc(struct stu_node*head);/*奇數項相連就是去掉偶數項*/ void Print_Stu_Doc(struct stu_node*head);/*遍歷*/ int main(void) { struct stu_node*head; head=Create_Stu_Doc(); head=DeleteDoc(head); Print_Stu_Doc(head); } struct stu_node*Create_Stu_Doc() { struct stu_node*head,*tail,*p; int num; int size=sizeof(struct stu_node); head=tail=NULL; scanf("%d",&num); while(num!=-1) { p=(struct stu_node*)malloc(size); p->num=num; p->next=NULL; if(head==NULL) head=p; else tail->next=p; tail=p; scanf("%d",&num); } return head; } struct stu_node*DeleteDoc(struct stu_node*head) { struct stu_node*ptr1,*ptr2; while(head!=NULL&&head->num%2==0) { ptr1=head; ptr2=head->next; free(ptr2); } if(head==NULL) return NULL; ptr1=head; ptr2=head->next; while(ptr2!=NULL) { if(ptr2->num%2==0) { ptr1->next=ptr2->next; free(ptr2); } else ptr1=ptr2; ptr2=ptr1->next; } return head; } void Print_Stu_Doc(struct stu_node*head) { struct stu_node*ptr; if(head==NULL) { printf("N0 Records\n"); return; } for(ptr=head;ptr;ptr=ptr->next) printf("%d",ptr->num); printf("\n"); }
第二種方法:正向思維
#include<stdio.h> #include<stdlib.h> #include<string.h> struct stud_node{ int num; struct stud_node *next; }; void Ptrint_Stu_Doc(struct stud_node *head); int main() { struct stud_node *L,*tail1,*tail2,*p1,*p2,*NEW; int num; int size=sizeof(struct stud_node); L=tail1=NULL; scanf("%d",&num); while(num!=-1){ /*---新建鏈表L--*/ p1=(struct stud_node *)malloc(size); p1->num=num; p1->next=NULL; if(L==NULL) L=p1; else tail1->next=p1; tail1=p1; scanf("%d",&num); } NEW=tail2=NULL; p1=L; while(p1!=NULL){ if(p1->num%2==0&&p1!=NULL){ /*--刪除鏈表L中的偶數值---*/ if(p1->next!=NULL){ p1=p1->next; continue; } else break; } if(p1==NULL) break; /*----將鏈表L中奇數值的結點重新組成一個新的鏈表NEW--*/ p2=(struct stud_node *)malloc(size); p2->num=p1->num; p2->next=NULL; if(NEW==NULL) NEW=p2; else tail2->next=p2; tail2=p2; p1=p1->next; } tail2->next=NULL; Ptrint_Stu_Doc(NEW); } void Ptrint_Stu_Doc(struct stud_node *head) { struct stud_node *ptr; if(head==NULL){ printf("No Records\n"); return; } for(ptr=head;ptr;ptr=ptr->next) printf("%d",ptr->num); printf("\n"); }
再加一個
#include<stdio.h> struct elem { int data; elem *next; }; int main() { int temp; //head指向鏈表頭元素。tail指向鏈表最后元素,tempP是個臨時存放地址 //frontP指向即將刪除元素的前一個元素,deleteP指向將要刪除的元素 elem * head,*tail,*tempP,*frontP,*deleteP; head=tail=NULL; //讀取信息 scanf("%d",&temp); while(-1!=temp) { if(head==NULL) { tail=head=new elem; head->data=temp; head->next=NULL; } else { tempP=new elem; tempP->data=temp; tempP->next=NULL; tail->next=tempP; tail=tempP; } scanf("%d",&temp); } //刪除偶數 tempP=head; while(NULL!=tempP) { if(0==tempP->data%2) { if(tempP==head) { head=tempP->next; } else { //找到偶數的前一個數,該數的next指向偶數的next; for(frontP=head;NULL!=frontP;++frontP) { if(frontP->next==tempP) { frontP->next=tempP->next; break; } } } } deleteP=tempP; tempP=tempP->next; if(0==deleteP->data%2) delete deleteP; } //打印數據 tempP=head; while(NULL!=tempP) { printf("%d\n",tempP->data); deleteP=tempP; tempP=tempP->next; delete deleteP; } }
