注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

zjushuiping

追求的只是心中的那一份宁静!

 
 
 

日志

 
 

(1)实验一 线性表的顺序存储结构  

2010-04-10 21:12:32|  分类: 数据结构试验 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
#include <cstdlib>
#include <iostream>
#define INIT_SIZE 100
#define LISTCREAMENT 10
using namespace std;
/*typedef struct student{
        char name[20];
        char number[20];
        char class_[40];
        char phone_number[40];
        }student;*/
/*typedef struct{
        student *elem;
        int length;
        int listsize;
        }Sqliststudent;*/
        
typedef struct{
        int *elem;
        int length;
        int listsize;
        }Sqlist;
        
       
int initSqlist(Sqlist &L)//整型元素线性表的初始化; 
{
                  L.elem=(int *)malloc(INIT_SIZE*sizeof(int));
                  if(L.elem==NULL)
                  {cout<<"存储分配失败\n";exit(2);}
                  L.listsize=INIT_SIZE;
                  L.length=0;
                  return 1;
}

/*int initSqliststudent(Sqliststudent &L)//student线性表的初始化; 
{L.elem=(int *)malloc(INIT_SIZE*sizeof(student));
                  if(L.elem==NULL)
                  {cout<<"存储分配失败\n";exit(2);}
                  L.listsize=INIT_SIZE;
                  L.length=0;
                  return 1;
}*/
int input(Sqlist &L)//整型元素的输入; 
{
    cout<<"请输入整型数据,以^Z结束输入"<<endl;
    int i,a;
    i=0;
    while(scanf("%d",&a)!=EOF)
    {L.elem=a;
    i++;
    L.length++;
    
    
           if(L.length>=L.listsize)
           {int *p;
            p=NULL;
            p=(int *)realloc(L.elem,sizeof(int)*(L.listsize+LISTCREAMENT));
           
            if(p==NULL)
            {cout<<"存储空间扩展失败,存储空间已满,不能再输入\n";
            break;
            }
            L.elem=p;
            L.listsize+=LISTCREAMENT;
            }
    
    
    }
   cout<<"输入结束\n";
    return 1;
     
    
    
}
/*int input(Sqliststudent &L)//student的输入
{student *p;
p=NULL;
    cout<<"请输入学生的基本信息\n输入#结束输入\n";
    while(1)
    {
            cout<<"姓名: ";
            cin>>L.name;
            if(strcmp(L.name,"#")==0)
            {cout<<"输入结束\n";break;}
            cout<<"学号: ";
            cin>>L.number;
            cout<<"专业班级: ";
            cin>>L.class_;
            cout<<"手机号: ";
            cin>>L.phone_number;
            L.length++;
            if(L.length>=L.listsize)
            p=(student *)realloc(L.elem,sizeof(student)*(L.listsize+LISTCREAMENT));
            if(p==NULL)
            {cout<<"存储空间扩展失败,存储空间已满,不能再输入\n";
            break;
            }
            L.elem=p;
            
             
    } 
    return 1;
    
}*/

int output(Sqlist L)//整型输出; 
{int i;
if(L.length==0)
    {cout<<"表是空表\n";return 0;} 
for(i=0;i<L.length;i++)
{
                       cout<<L.elem<<" ";
}
cout<<endl;
return 1;
}
/*int output(Sqliststudent L)//student输出; 
{
    int i;
    if(L.length==0)
    {cout<<"表是空表\n";return 0;} 
    for(i=0;i<L.length;i++)
    {cout<<"姓名: "<<L.elem.name<<endl;
    cout<<"学号: "<<L.elem.number<<endl;
    cout<<"专业班级: "<<L.elem.class_<<endl;
    cout<<"手机号: "<<L.elem.phone_number<<endl;
    }
    cout<<"输出结束\n";
    return 1;
}*/

int check(int e,Sqlist L)//查找; 
{
    int i;
    for(i=0;i<L.length;i++)
    {if(e==L.elem)
     return 1;
    }
    return 0;
}
int judge(Sqlist L)//判断是否对称; 
{
    int i,n;
    n=L.length;
    if(n==0) 
    {cout<<"表是空表\n";return -1;}
    if(n==1)
    return 1;
    n/=2;
    for(i=0;i<n;i++)
    {
                    if(L.elem[L.length-i]!=L.elem)
                    break;
    }
    if(i<n)
    return 0;
    else
    return 1;
}

/*int change(Sqlist &L)
{int i,j,k,t;
for(i=0;i<L.length-1;i++)
{
                         for(j=0;j<L.length-1-i;j++)
                         if(L.elem[j]%2==0)
                         k=L.elem[j],L.elem[j]=L.elem[j+1],L.elem[j+1]=k;
}
return 1;
}
*/
int change(Sqlist &L)//奇偶对换; 
{
    int i,j,k,t;
    for(i=0;i<L.length-1;i++)
    {k=i;
    if(L.elem[k]%2==1)
    continue;
    else if(L.elem[k]%2==0)
    {
     for(j=i+1;j<L.length;j++)
     if(L.elem[j]%2==1)
     {k=j;break;}
    }
    if(k!=i)
    t=L.elem,L.elem=L.elem[j],L.elem[j]=t;
    }
    return 1;
}
int insertsort(Sqlist &L)//有序插入; 
{
    initSqlist(L);
    int i,j,k;
    int a;
    cout<<"请输入整型数据,以^Z结束输入\n"; 
    i=0;
    while(scanf("%d",&a)!=EOF)
    {
                 /*if(L.length==0)
                 {L.elem=a;L.length++;i++;}*/
                 //else
                 //{
                       //cout<<a<<endl;
                       for(j=0;j<L.length;j++)
                   if(a<L.elem[j])
                   break;
                   
                   if(j<L.length)
                   {       for(k=L.length-1;k>=j;k--)
                           L.elem[k+1]=L.elem[k];
                           L.elem[j]=a;
                           //i++;
                           L.length++;
                           
                   }
                   else
                   {L.elem[L.length]=a;L.length++;}
                   
                 //}
                 if(L.length>=L.listsize)
                 {int *p;
                  p=NULL;
                  p=(int *)realloc(L.elem,sizeof(int)*(LISTCREAMENT+L.listsize));
                  
            
            if(p==NULL)
            {cout<<"存储空间扩展失败,存储空间已满,不能再输入\n";
            break;
            }
            L.elem=p;
            L.listsize+=LISTCREAMENT;
            }
                  
                 
    }
    
    return 1;
}
                   
int mergelist(Sqlist L1,Sqlist L2,Sqlist &L3)//合并; 
{
    //insertsort(L1);
    //insertsort(L2);
    initSqlist(L3);
    int i,j,k;
    if(L1.length+L2.length==0)
    {cout<<"两个表均为空表,无法合并\n";return 0;}
    if(L3.listsize<L1.length+L2.length)
    {int *p;
     p=NULL;
     p=(int *)realloc(L3.elem,sizeof(int)*(L3.listsize+LISTCREAMENT));
     if(p==NULL)
     {cout<<"存储空间扩展失败,无法合并\n";return 0;}
     L3.elem=p;
     L3.listsize+=LISTCREAMENT;
     }
     
     
     i=j=0;k=0;
    while((i<L1.length)&&(j<L2.length))
    {if(L1.elem<L2.elem[j])
     {L3.elem[k]=L1.elem;k++;i++;}
     else 
     {L3.elem[k]=L2.elem[j];k++;j++;}
     
    }
    
    
    while(i<L1.length)
    {
                      L3.elem[k]=L1.elem;
                      i++;k++;
    }
    while(j<L2.length)
    {  L3.elem[k]=L2.elem[j];
       j++;k++;
    }
    L3.length=L1.length+L2.length;
    
    cout<<"合并成功\n";
    return 1;

     
     
     
     
                                         
                                         
                                          
                       
                   
                 
                 
     
    
         
     
     
      
                         
                    
 
                          
 
 
int main(int argc, char *argv[])
{Sqlist L1,L2,L3,L;
int e;
initSqlist(L);
input(L);
output(L);
cout<<"请输入要查找的元素\n";
scanf("%d",&e);//cin>>e;
if(1==check(e,L))
cout<<"该元素存在\n"; 
else
cout<<"该元素不存在\n";
if(1==judge(L))
cout<<"该表对称\n";
else
cout<<"该表不对称\n";
cout<<"将表中奇数排在偶数之前\n";
 
change(L);
cout<<"排列后的元素\n"; 
output(L); 
free(L.elem);
cout<<"建立有序表\n";
insertsort(L1);
output(L1);
free(L1.elem);
cout<<"建立有序表1\n";
insertsort(L1);
output(L1);
cout<<"建立有序表2\n";
insertsort(L2);
output(L2);
cout<<"将两表合并\n";
mergelist(L1,L2,L3);
cout<<"合并后的表为\n";
output(L3); 
free(L1.elem);
free(L2.elem);
free(L3.elem);
 
 
 
 
    system("PAUSE");
    return EXIT_SUCCESS;
}
  评论这张
 
阅读(72)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017