zoukankan      html  css  js  c++  java
  • 4)线性表[顺序表和链表]

    顺序表:

      1 #include<iostream>
      2 using namespace std;
      3 
      4 enum error{rangeerror,underflow,overflow,success};
      5 const int maxlen=1000;
      6 
      7 class list{
      8 public:
      9     list();
     10     int length()const;//求长度
     11     int get_int(const int i,int &x)const;//按序号取元素运算
     12     int locate(const  int x)const;//搜索元素运算对应的函数
     13     int insert(const int i,const int x);//插入元素运算对应的函数
     14     int dele(const int i);//删除元素运算对应的函数
     15 private:
     16     int data[maxlen];
     17     int count;
     18 };
     19 
     20 
     21 list::list(){//初始顺序表化
     22     count=0;
     23 }
     24 
     25 int list::length()const{//求表长度的实现
     26     return count;
     27 }
     28 
     29 int list::get_int(const int i,int &x)const{//按序号求元素
     30     if(i<=0||i>count)return underflow;
     31     else {
     32         x=data[i-1];
     33         return success;
     34     }
     35 }
     36 
     37 int list::locate(const int x)const{//查找元素
     38     bool flags=false;
     39     for(int i=0;i<count;i++){
     40         if(data[i]==x){
     41             flags=true;
     42             return i+1;
     43             break;
     44         }
     45     }
     46     if(flags==false)return -1;
     47 }
     48 
     49 int list::insert(const int i,const int x){//插入元素
     50     if(count==maxlen)return overflow;//溢出,不能插入
     51     if(i<1||i>length()+1)return rangeerror;//插入范围有错
     52     else {
     53         for(int j=count-1;j>=i-1;j--){
     54             data[j+1]=data[j];
     55         }
     56         data[i-1]=x;
     57         count++;
     58         return success;
     59     }
     60 }
     61 
     62 
     63 int list::dele(const int i){//删除元素
     64     int j;
     65     if(length()==0)return underflow;
     66     if(i<1||i>length())return rangeerror;
     67     else {
     68         for(j=i+1;j<=length();j++){
     69             data[j-2]=data[j-1];
     70         }
     71         count--;
     72         return success;
     73     }
     74 }
     75 
     76 
     77 bool subset(list A,list B){//判断集合A是否是集合B 的子集
     78     int ia,ib,x,y;
     79     bool flags;
     80     bool suc=true;
     81     for(ia=0;ia<A.length();ia++){
     82         A.get_int(ia+1,x);
     83         flags=false;
     84         for(ib=0;ib<B.length();ib++){
     85             B.get_int(ib+1,y);
     86             if(x==y){
     87                 flags=true;
     88                 break;
     89             }
     90         }
     91         if(flags==false){
     92             suc=false;
     93             break;
     94         }
     95     }
     96     return suc;
     97 }
     98 
     99 
    100 void merge_list(list A,list B, list &C){//顺序表A,B合并
    101     int ia=0,ib=0,ic=1;
    102     int x,y;
    103     while(ia<A.length()&&ib<B.length()){
    104         A.get_int(ia+1,x);
    105         B.get_int(ib+1,y);
    106         if(x==y){
    107             C.insert(ic,x);
    108             ic++;
    109             ia++;
    110             C.insert(ic,y);
    111             ic++;
    112             ib++;
    113         }else if(x>y){
    114             C.insert(ic,y);
    115             ic++;
    116             ib++;
    117         }else {
    118             C.insert(ic,x);
    119             ic++;
    120             ia++;
    121         }
    122     }
    123 
    124     while(ia<A.length()){
    125         A.get_int(ia+1,x);
    126         C.insert(ic,x);
    127         ic++;
    128         ia++;
    129     }
    130     while(ib<B.length()){
    131         B.get_int(ib+1,y);
    132         C.insert(ic,y);
    133         ic++;
    134         ib++;
    135     }
    136 
    137     int i;
    138     for(i=0;i<C.length();i++){
    139         C.get_int(i+1,x);
    140         cout<<x<<" ";
    141     }
    142     cout<<endl;
    143         
    144 }
    145 int main(){
    146     list A,B,C;
    147     A.insert(1,1);
    148     A.insert(2,3);
    149     A.insert(3,5);
    150     A.insert(4,7);
    151     A.insert(5,9);
    152     A.insert(6,11);
    153     A.insert(7,13);
    154     B.insert(1,3);
    155     B.insert(2,4);
    156     B.insert(3,6);
    157     B.insert(4,8);
    158     B.insert(5,10);
    159     B.insert(6,12);
    160     B.insert(7,14);
    161     B.insert(8,15);
    162 
    163     int x,i;
    164     for( i=0;i<A.length();i++){
    165     A.get_int(i+1,x);
    166     cout<<x<<" ";
    167     }
    168     cout<<endl;
    169 
    170 
    171     for( i=0;i<B.length();i++){
    172     B.get_int(i+1,x);
    173     cout<<x<<" ";
    174     }
    175     cout<<endl;
    176 
    177     merge_list(A,B,C);
    178 /*    for( i=0;i<C.length();i++){
    179     C.get_int(i+1,x);
    180     cout<<x<<" ";
    181     }
    182     cout<<endl;*/
    183     //cout<<subset(A,B)<<endl;
    184     return 0;
    185 }

    链表:

      1 #include <iostream>
      2 using namespace std;
      3 
      4 
      5 enum error{rangeerror,overflow,underflow,success};
      6 struct node{
      7     int data;
      8     node *next;
      9 };
     10 
     11 class list{
     12 public:
     13     list();
     14     int length() const;//求长度的函数
     15 //    ~list();//释放链表存储空间的析构函数
     16     int get_int(const int i ,int & x)const;//按序号取元素运算函数
     17     int locate(const  int x)const;//搜索元素运算对应的函数
     18     int insert(const int i,const int x);//插入元素运算对应的函数
     19     int dele(const int i);//删除元素对应的函数]
     20     void  create();//创建链表
     21     node *get_head(){return head;}
     22     bool Judge();//判断链表L中的元素是否为递增的
     23     void copy(list A,list &B);//复制链表A中的内容到B表中
     24 private:
     25     int count;
     26     node * head;
     27 };
     28 
     29 
     30 list::list(){//初始化对应的构造函数
     31     head =new node;
     32     head->next=NULL;
     33     count=0;
     34 }
     35 
     36 int list::length() const{//求长度
     37     return count;
     38 }
     39 
     40 int list::get_int(const int i,int &x)const{//按序号取元素
     41     node *p=new node;
     42     p=head->next;
     43     int j=1;
     44     while(p!=NULL&&j!=i){//当前节点不是目标节点,并且不空时就继续搜索
     45         p=p->next;
     46         j++;
     47     }
     48     if(p==NULL)return rangeerror;
     49     else {
     50         x=p->data;
     51         return success;
     52     }
     53 }
     54 
     55 int list::locate(const int x)const{//搜索元素运算对应的函数
     56     bool flags=false;
     57     int j=1;
     58     node *p = new node;
     59     while(p!=NULL){
     60         if(p->data==x){
     61             cout<<"元素所在的位置为:"<<j<<endl;
     62             flags=true;
     63             break;
     64         }else{
     65             j++;
     66             p=p->next;
     67         }
     68     }
     69     if(flags==false)return -1;
     70     return 0;
     71 }
     72 
     73 
     74 int list::insert(const int i,const int x){//插入元素
     75     node *p=new node;
     76     p=head;
     77     int j;
     78     j=0;
     79     while(j!=i-1&&p!=NULL){
     80         p=p->next;
     81         j++;
     82     }
     83 
     84     if(i<1||i>count+1)return rangeerror;
     85     node *s = new node;//产生节点
     86     s->data=x;//装入数据
     87     s->next=p->next;
     88     p->next=s;
     89     count++;
     90     return success;
     91 }
     92 
     93 
     94 int list::dele(const int i){//删除元素
     95     node * p=new node;
     96     p=head;
     97     int j=0;
     98     while(j!=i-1&&p!=NULL){
     99         p=p->next;
    100         j++;
    101     }
    102 
    103     if(i<1||i>count+1)return rangeerror;
    104     node *u=new node;
    105     u=p->next;
    106     p->next=u->next;
    107     delete u;
    108     count--;
    109     return success;
    110 }
    111 
    112 
    113 void list::create(){//创建链表
    114     int x;
    115     cin>>x;
    116     node * rear=head;
    117     while(x!=0){
    118         count++;
    119         node *s=new node;
    120         s->data=x;
    121         rear->next=s;
    122         rear=s;
    123         rear->next=NULL;
    124         cin>>x;
    125     }
    126 }
    127 
    128 bool list::Judge(){//用于判断链表是否为递增的
    129     node *p=new node;
    130     p=head;
    131     bool flags=true;
    132     while(p->next!=NULL){
    133         if(p->data<p->next->data)p=p->next;
    134         else {
    135             flags=false;
    136             break;
    137         }
    138     }
    139     return flags;
    140 }
    141 
    142 void list::copy(list A,list &B){
    143     node *pa=new node;
    144     node *pb=new node;
    145     pa=A.get_head()->next;
    146     pb=head;
    147     while(pa!=NULL){
    148         node * s = new node;
    149         s->data=pa->data;
    150         pb->next=s;
    151         pb=s;
    152         B.count++;
    153         pa=pa->next;
    154         pb->next=NULL;
    155     }
    156 }
    157 int main()
    158 {
    159     list l;
    160     l.create();
    161     int i,x;
    162     for(i=0;i<l.length();i++){
    163         l.get_int(i+1,x);
    164         cout<<x<<" ";
    165     }
    166 
    167     cout<<endl;
    168 
    169     cout<<l.Judge()<<endl;
    170 /*    l.insert(5,10);
    171     for(i=0;i<l.length();i++){
    172         l.get_int(i+1,x);
    173         cout<<x<<" ";
    174     }
    175 
    176     cout<<endl;
    177 
    178     l.dele(2);
    179         for(i=0;i<l.length();i++){
    180         l.get_int(i+1,x);
    181         cout<<x<<" ";
    182     }
    183 
    184     cout<<endl;
    185     */
    186 
    187     return 0;
    188 }
  • 相关阅读:
    CentOS中用top命令CPU负载
    Oracle文档阅读指南
    修改oracle用户密码永不过期
    让secureCRT以命令行方式启动图形界面
    .NET 2.0 SQL依赖注意事项(转载:原文《使用SQL Server 2005 Query Notification的几个注意事项》)
    SQL Server 2005服务状态管理工具
    一个常用的正则表达验证类
    解决TFS中MSBuild开发项目和开发编译中一些冲突(小技巧)
    ASP.NET 2.0 WebService中传递DataTable参考
    LightningFramework系列(各层详细设计类图)
  • 原文地址:https://www.cnblogs.com/minmsy/p/5022056.html
Copyright © 2011-2022 走看看