链表相减

书上题目,要用带头链表处理

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  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<n;i++)
 30     {
 31         if(tail==NULL)    //开始出错了
 32         {
 33             tail=head;
 34             cin>>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<<p->date<<' ';
 55         p=p->next;
 56     }
 57     cout<<endl;
 58 }
 59 void destroylinklist(linklist head)
 60 {
 61     linklist p=head,q=NULL;
 62     while(p!=NULL)
 63     {
 64         q=p;
 65         p=p->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<n;i++)
 74     {
 75         q=p;
 76         p=p->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         else
121         {
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 }

 

转载于:https://www.cnblogs.com/a1225234/p/4662150.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值