zoukankan      html  css  js  c++  java
  • 链表相减

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

      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 }
  • 相关阅读:
    Linux的CPU负载
    C++ 内接连与外接连
    boost 串口通信
    创建型模式--单例模式
    Python urllib与urllib2
    CodeBlocks使用boost+MinGW
    Python 线程(七):local(线程局部存储)
    Python 线程(六):Timer(定时器)
    Python 线程(五):Event
    Python 线程(四):Semphore同步
  • 原文地址:https://www.cnblogs.com/a1225234/p/4662150.html
Copyright © 2011-2022 走看看