书上题目,要用带头链表处理
1 #include2 #include 3 #include 4 using namespace std; 5 typedef struct node 6 { 7 int date; 8 struct node *next; 9 }listnode,*linklist; 10 linklist initlist(linklist head) /*链表初始化*/ 11 { 12 head=new node; /*申请内存*/ 13 if(head==NULL) 14 cout<<"链表创建失败"; 15 else 16 head->next=NULL; 17 return head; 18 } 19 int listempty(linklist head) 20 { 21 if(head->next==NULL) 22 return 1; 23 return 0; 24 } 25 int inputlist(linklist head,int n) 26 { 27 int i,t; 28 linklist tail=NULL,temp=NULL; 29 for(i=0;i >t; 35 head->date=t; 36 } 37 else 38 { 39 temp=new node; 40 cin>>t; 41 temp->date=t; 42 tail->next=temp; 43 tail=temp; 44 tail->next=NULL; 45 } 46 } 47 return 0; 48 } 49 void outputlist(linklist head) 50 { 51 linklist p=head; 52 while(p!=NULL) 53 { 54 cout< date<<' '; 55 p=p->next; 56 } 57 cout< next; 66 free(q); 67 } 68 } 69 void insert(linklist head,int n,int num) 70 { 71 linklist p=head,q=head,temp,l; 72 int i; 73 for(i=0;i next; 77 } 78 temp=p; 79 l=new listnode; 80 l->date=num; 81 l->next=temp; 82 q->next=l; 83 } 84 int listlen(linklist head) 85 { 86 int count=0; 87 linklist p=head; 88 while(p!=NULL) 89 { 90 count++; 91 p=p->next; 92 } 93 return count; 94 } 95 linklist del(linklist head1,linklist head2) 96 { 97 int i,flag=0; 98 linklist H1,q=head2; 99 H1=new listnode;100 H1->next=head1;101 linklist pre=H1,p=H1->next;102 while(p!=NULL)103 {104 flag=0;105 q=head2;106 while(q!=NULL)107 {108 if(p->date==q->date)109 {110 pre->next=p->next;111 flag=1;112 }113 q=q->next;114 }115 if(flag==0)116 {117 pre=p;118 p=p->next;119 }120 else121 {122 p=pre->next;123 }124 }125 return H1->next;126 }127 int main()128 {129 linklist head1,head2;130 head1=initlist(head1);131 head2=initlist(head2);132 inputlist(head1,6);133 inputlist(head2,5);134 head1=del(head1,head2);135 outputlist(head1);136 return 0; 137 }