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



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

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