| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 726 人关注过本帖
标题:顺序表的问题
只看楼主 加入收藏
树上月
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:114
专家分:154
注 册:2010-1-6
结帖率:87.5%
收藏
已结贴  问题点数:15 回复次数:3 
顺序表的问题
因我刚刚自学数据结构,刚刚把顺序表看完。编了一个简单的顺序表。其中有些问题希望高手们能够指点、指点,,,,
// efgghh.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include"stdlib.h"

struct Sqlist{
    int *elem;         //存储空间基址
    int length;        //当前长度
    int listsize;      //当前分配的存储容量
};
 
//构造一个空的线性链表
void Sq(Sqlist &lm){
    lm.elem=(int *)malloc(10*sizeof(int));
    if(!lm.elem)   
        return ;        
    lm.length=0;        //空间长度为0
    lm.listsize=10;    //初始存储容量
    return ;
};

//创建链表

void CreatSqlist(Sqlist &lm){
    int number;
    scanf("%d",&number);
    do{
        lm.elem[lm.length]=number;
        lm.length++;
        scanf("%d",&number);
    }while(number!=-1);
}


//遍历顺序表

void searchSqlist(Sqlist &lm)
{
    int i;
    for(i=0;i<lm.length;i++)
        printf("第%d个元素是:%d\n",i+1,lm.elem[i]);
}


//处理顺序表 (基数全部位于偶数前)

void SWapSqlist(Sqlist &lm)
{
    int a[100];
   
    for(int i=0,j=0;i<lm.length;i++){
        if(lm.elem[i]%2==1){
              a[j] =lm.elem[i];
            j++;
        }
    }
    for(i=0;i<lm.length;i++){
        if(lm.elem[i]%2==0){
            a[j]=lm.elem[i];
            j++;
        }
    }
        for(i=0;i<lm.length;i++)
        printf("%d  ",a[i]);
}

//对顺序表进行排序

/*void ArrangeSqlist(Sqlist lm)
{
    struct Sqlist temp;                                             为什么这样定义temp不行,该这么定义temp
    int i,j,index;                                                
    for(i=0;i<lm.length;i++){                                          
        index=i;
        for(j=i+1;j<lm.length;j++){                                    
            if(lm.elem[index]>lm.elem[j])   //交换元素                    
                index=j;
            temp=lm.elem[index];
            lm.elem[index]=lm.elem[i];
            lm.elem[i]=temp;
            
        }
    }
    for(i=0;i<lm.length;i++)
        printf("%d  "lm.elem[i]);
}*/

//插入元素
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
/*void InsertSqlist(Sqlist &lm,int number ,int i)                     为什么这样number不能够传递过去(i可以传递过去)
{
    if(i<1||i>lm.listsize){                    //i值不合法
        printf("插入位置不合法!");                                    还有这个插入函数好像不行,能不能帮我改一下
        return ;
    }                                                                                 
     //存储空间已满,增加分配
    if(lm.length>lm.listsize){                                                           
        lm.elem=(int *)realloc(lm.elem, (lm.listsize+10)*sizeof(int));                     
        if(!lm.elem)                         //存储分配失败
        return ;                                          
        lm.listsize =lm.listsize+10;         //增加存储容量
        lm.elem[i] = number;
    }
    //表不满,直接插入
    if(lm.length<lm.listsize){
        if(i>lm.length){
            lm.elem[i]=number;
        }
        if(i<=lm.length){
            for(int k=lm.length; k>=i; k--){
                lm.elem[k+1]=lm.elem[k];
            }
            lm.elem[i] = number;
        }
    }
    for(i=0;i<lm.length;i++)
        printf("%d ",lm.elem[i]);
}
*/

//在顺序表中插入元素

void InsertSqlist(Sqlist &lm,int number ,int i)
{
    int *q,*p;                                                          // 问:                                                                          
    if(i<1||i>lm.listsize){                    //i值不合法             为什么这里 *q,*p要定义为int型(能否这样解释
        printf("插入位置不合法!");                                    //因为*p,*q所呈递都是单纯的数字、或地址,而两者
        return ;                                                    //都是int型的。)如果可以这样解释,那他们所呈递    }                                                             //如果是struct型的,那么他们是否就应该定义为:
                                                                       struct *** *p,*q;
//储空间已满,增加分配                                                      
   if(lm.length>lm.listsize)
   {                  
        lm.elem=(int *)realloc(lm.elem, (lm.listsize+10)*sizeof(int));
        if(!lm.elem)                         //存储分配失败
        return ;                                                                                    
        lm.listsize =lm.listsize+10;         //增加存储容量
        lm.elem[i] = number;
    }
    //表不满,直接插入
    q=&(lm.elem[i-1]);                      //q为插入的位置                             
    for(p=&(lm.elem[lm.length-1]);p>=q;--p)
        *(p+1)=*p;                           //插入位置及以后的元素全部右移
    *q=number;
    ++lm.length;                                //表长加1
    for(i=0;i<lm.length;i++)
        printf("%d ",lm.elem[i]);

}

//在顺序表中删除元素

void DeleteSqlist(Sqlist &lm,int number)
{
    int i,k;
    for(i=0;i<=lm.length;i++){
        if(number==lm.elem[i]){
            for(k=i;k<=lm.length;k++)
                lm.elem[k]=lm.elem[k+1];               //删除位置及其以后的元素全部左移
            break;                                    
        }
    }
    lm.length--;                                 //表长减1
    if(i==lm.length)
        printf("没有该数!");
    for(i=0;i<lm.length;i++)
        printf("%d ",lm.elem[i]);
}

//在顺序表中查找

int SearchSqlist(Sqlist &lm,int number)           
{
    int i;
    for(i=0;i<lm.length;i++){
        if(number==lm.elem[i])
            return i+1;
    }
    if(i==lm.length)
       return 0;
}

int main(int argc, char* argv[])
{
    int i,number,lb,lc,e;
    Sqlist lm;
    Sqlist elem[100];               
    Sq(lm);
       printf("elem的地址为:%d\n",lm.elem);
    printf("length=%d\n",lm.length);
    printf("listsize=%d\n",lm.listsize);
   
    printf("输入一串数字(以-1结束):");
    CreatSqlist(lm);                         //调用创建函数
    searchSqlist(lm);                        //调用遍历函数  

    //对顺序表处理
    printf("对该表进行处理(基数全部位于偶数前)后:");   
    SWapSqlist(lm);                       //调用处理函数
    printf("\n");

    //在顺序表中插入数字
    printf("请输入你要插入的数:");  
    scanf("%d",&number);
    printf("请输入你要插入的位置:");
    scanf("%d",&i);
    InsertSqlist(lm,number,i);              //调用插入函数
    printf("\n");

    //在顺序表中删除元素
    printf("请输入你要删除的元素:");
    scanf("%d",&lb);
    DeleteSqlist(lm,lb);                    //调用删除函数
    printf("\n");

    //在顺序表中查找元素
    printf("请输入你要查找的数字:");
    scanf("%d",&lc);
    e=SearchSqlist(lm,lc);                   //e接受返回值
    if(e==0)
        printf("没有该数!");
    else
      printf("%d在顺序表中的位置是:第%d个",lc,e);
    printf("\n");
    //printf("Hello World!\n");
    return 0;
}



搜索更多相关主题的帖子: 顺序 
2010-04-01 16:37
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
#include "stdio.h"
#include "stdlib.h"

struct Sqlist{
    int *elem;         //存储空间基址
    int length;        //当前长度
    int listsize;      //当前分配的存储容量
};

//构造一个空的线性链表
void Sq( Sqlist &Lm )
{
    Lm.elem=(int *)malloc(10*sizeof(int));
    if(!Lm.elem)   
        exit(0);        
    Lm.length=0;        //空间长度为0
    Lm.listsize=10;    //初始存储容量
    return ;
};

//创建链表

void CreatSqlist(Sqlist &Lm)
{
    int number;
    scanf("%d",&number);
    do{
        Lm.elem[Lm.length]=number;
        Lm.length++;
        scanf("%d",&number);
    }while( number != -1 );  //注意长度 Lm.length 10
}


//遍历顺序表

void searchSqlist(Sqlist Lm)
{
    int i;
    for(i=0;i<Lm.length;i++)
        printf("第%d个元素是:%d\n",i+1,Lm.elem[i]);
}


//处理顺序表 (基数全部位于偶数前)

void SWapSqlist(Sqlist &Lm)
{
    int a[100];
    int i, j;
   
    for(i=0,j=0;i<Lm.length;i++)
    {
        if(Lm.elem[i]%2==1)
        {
              a[j] =Lm.elem[i];
            j++;
        }
    }
    for(i=0;i<Lm.length;i++)
    {
        if(Lm.elem[i]%2==0)
        {
            a[j]=Lm.elem[i];
            j++;
        }
    }
        for(i=0;i<Lm.length;i++)
        printf("%d  ",a[i]);
}

//对顺序表进行排序

void ArrangeSqlist(Sqlist Lm)
{
    int temp;                                             //为什么这样定义temp不行,该这么定义temp 整型
    int i,j,index;
   
    for(i=0;i<Lm.length;i++)
    {                                          
        index=i;
        for(j=i+1;j<Lm.length;j++)
        {                                    
            if(Lm.elem[index]>Lm.elem[j])   //交换元素                    
                index=j;
            temp=Lm.elem[index];
            Lm.elem[index]=Lm.elem[i];
            Lm.elem[i]=temp;
            
        }
    }
    for(i=0; i<Lm.length; i++ )
        printf("%d ",Lm.elem[i]);
   // for(i=0;i<Lm.length;i++)
     //   printf("%d  "Lm.elem[i]);
}

//插入元素
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
void InsertSqlist(Sqlist &Lm,int number ,int i)                     //为什么这样number不能够传递过去(i可以传递过去)
{
    int j;

    if(i<1||i>Lm.length)
    {                    //i值不合法
        printf("插入位置不合法!");                                    //还有这个插入函数好像不行,能不能帮我改一下
        exit(0);
    }                                                                                 
     //存储空间已满,增加分配
    if(Lm.length>=Lm.listsize)
    {                                                           
        Lm.elem=(int *)realloc(Lm.elem, (Lm.listsize+10)*sizeof(int));                     
        if(!Lm.elem)                         //存储分配失败
            exit(0);                                          
        Lm.listsize =Lm.listsize+10;//增加存储容量
        //Lm.elem[i] = number;
    }
   
    for( j = Lm.length; j>=i; j-- )
        Lm.elem[j] = Lm.elem[j-1];
    Lm.elem[j] = number;
    Lm.length += 1;

    for(j=0;j<Lm.length;j++)
        printf("%d ",Lm.elem[j]);
}


//在顺序表中插入元素
/*
void InsertSqlist(Sqlist &lm,int number ,int i)
{
    int *q,*p;                                                          // 问:                                                                          
    if(i<1||i>lm.listsize){                    //i值不合法             为什么这里 *q,*p要定义为int型(能否这样解释
        printf("插入位置不合法!");                                    //因为*p,*q所呈递都是单纯的数字、或地址,而两者
        return ;                                                    //都是int型的。)如果可以这样解释,那他们所呈递是    }                                                             //如果是struct型的,那么他们是否就应该定义为:
                                                                       struct *** *p,*q;
//储空间已满,增加分配                                                      
   if(lm.length>lm.listsize)
   {                  
        lm.elem=(int *)realloc(lm.elem, (lm.listsize+10)*sizeof(int));
        if(!lm.elem)                         //存储分配失败
        return ;                                                                                    
        lm.listsize =lm.listsize+10;         //增加存储容量
        lm.elem[i] = number;
    }
    //表不满,直接插入
    q=&(lm.elem[i-1]);                      //q为插入的位置                             
    for(p=&(lm.elem[lm.length-1]);p>=q;--p)
        *(p+1)=*p;                           //插入位置及以后的元素全部右移
    *q=number;
    ++lm.length;                                //表长加1
    for(i=0;i<lm.length;i++)
        printf("%d ",lm.elem[i]);

}*/

//在顺序表中删除元素

void DeleteSqlist(Sqlist &Lm,int number)
{
    int i,k;
    for(i=0;i<Lm.length;i++)
        if(number==Lm.elem[i])
        {
            for(k=i;k<Lm.length;k++)
                Lm.elem[k]=Lm.elem[k+1];  //删除位置及其以后的元素全部左移
            Lm.length--; //表长减1
        }
                                                
                                 
    if(i==Lm.length)
        printf("没有该数!");
    for(i=0;i<Lm.length;i++)
        printf("%d ",Lm.elem[i]);
}

//在顺序表中查找

int SearchSqlist(Sqlist &Lm,int number)           
{
    int i;
    int flag = 0;//定义一个标志位 来判断是否查到
    for(i=0;i<Lm.length;i++)
        if(number==Lm.elem[i])
        {
            flag = 1;
            return i+1;
        }
    /*if(i==Lm.length)  //如果最后一个找到 i从for语句中出来是 i==Lm.length;
       return 0;*/
    if( flag == 0 )
        return 0;
}

int main(int argc, char* argv[])
{
    int i,number,lb,lc,e;
    Sqlist lm;
    //Sqlist elem[100];               
    Sq(lm);
       //printf("elem的地址为:%d\n",lm.elem);
    printf("length=%d\n",lm.length);
    printf("listsize=%d\n",lm.listsize);
   
    printf("输入一串数字(以-1结束):");
    CreatSqlist(lm);                         //调用创建函数
    searchSqlist(lm);                        //调用遍历函数  

    //对顺序表处理
    printf("对该表进行处理(基数全部位于偶数前)后:");   
    SWapSqlist(lm);                       //调用处理函数
    printf("\n");

    //在顺序表中插入数字
    printf("请输入你要插入的数:");  
    scanf("%d",&number);
    printf("请输入你要插入的位置:");
    scanf("%d",&i);
    InsertSqlist(lm,number,i);              //调用插入函数
    printf("\n");

    //在顺序表中删除元素
    printf("请输入你要删除的元素:");
    scanf("%d",&lb);
    DeleteSqlist(lm,lb);                    //调用删除函数
    printf("\n");

    //在顺序表中查找元素
    printf("请输入你要查找的数字:");
    scanf("%d",&lc);
    e=SearchSqlist(lm,lc);                   //e接受返回值
    if(e==0)
        printf("没有该数!");
    else
      printf("%d在顺序表中的位置是:第%d个",lc,e);
    printf("\n");
    //printf("Hello World!\n");
    return 0;
}
2010-04-03 13:28
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
//对顺序表进行排序

/*void ArrangeSqlist(Sqlist lm)
{
    struct Sqlist temp;                                             为什么这样定义temp不行,该这么定义temp
    int i,j,index;                                                
    for(i=0;i<lm.length;i++){                                          
        index=i;
        for(j=i+1;j<lm.length;j++){                                    
            if(lm.elem[index]>lm.elem[j])   //交换元素                    
                index=j;
            temp=lm.elem[index];   类型 要匹配
            lm.elem[index]=lm.elem[i];   
            lm.elem[i]=temp;
            
        }
    }
    for(i=0;i<lm.length;i++)
        printf("%d  "lm.elem[i]);
}*/

//插入元素
2010-04-03 13:40
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:15 
/*void InsertSqlist(Sqlist &lm,int number ,int i)                     为什么这样number不能够传递过去(i可以传递过去)都可以传递过去
{
    if(i<1||i>lm.listsize){    改成 lm.length                //i值不合法
        printf("插入位置不合法!");                                    还有这个插入函数好像不行,能不能帮我改一下
        return ;
    }                                                                                 
     //存储空间已满,增加分配
    if(lm.length>lm.listsize){                                                           
        lm.elem=(int *)realloc(lm.elem, (lm.listsize+10)*sizeof(int));                     
        if(!lm.elem)                         //存储分配失败
        return ;                                          
        lm.listsize =lm.listsize+10;         //增加存储容量
        lm.elem[i] = number;这句不能这样插  这里的if语句 只是完成加配空间 插入的时候不一定就是在最后位置 还有待判断
    }
    //表不满,直接插入
    if(lm.length<lm.listsize){
        if(i>lm.length){
            lm.elem[i]=number;
        }
        if(i<=lm.length){
            for(int k=lm.length; k>=i; k--){
                lm.elem[k+1]=lm.elem[k];
            }
            lm.elem[i] = number;
        }
        lm.length++;
        插入完毕 长度要加1

    }
    for(i=0;i<lm.length;i++)一般不用i 自己重新定义一个变量
        printf("%d ",lm.elem[i]);
}
2010-04-03 13:49
快速回复:顺序表的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020151 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved