码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构中构建顺序表

时间:2014-07-30 17:36:44      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:数据结构   c语言   struct   malloc   

     顺序表指的是数据元素在内存中连续分配地址的数组,由于指针无法指出数组长度,编译时不会报错,所有用结构体来表示一个顺序表:

顺序表用C语言的表示方法如下:

<span style="font-family: Arial, Helvetica, sans-serif;">  #define OK 1</span>
  #define ERROR -1
  typedef int elem_type;
  typedef int Statue;
 //  int Arrylength;
   typedef struct sqlist
   {
   	elem_type  *Arry;
   	int  Arrylength;
   	} Sqlist;
 
   //建立一个空表
  Statue Create_Sqlist(Sqlist *&S)
   {
    //S->Arry = (elem_type *) malloc(MaxSize*sizeof(elem_type));

	//L = (sqlist *)malloc(sizeof(sqlist));
   // L->data = (char *)malloc(maxsize);
    S = (Sqlist *)malloc(sizeof(Sqlist)); 
	S->Arry = (elem_type *)malloc(MaxSize);
   	if(!S->Arry)
   		{
   			return ERROR;
   			}
   	else
   		{
   			S->Arrylength = 0;
   			return OK;
	    }
   	}

//顺序表赋初始值
  void   Init_Sqlist(Sqlist *&S)
  {
  	int i;
  	for(i=0;i<20;i++)
  	{
  	   S->Arry[i]=rand()%100;
	   
  	   S->Arrylength++;
  	 }
  }
  	
//在第i个位置添加一个元素m
  Statue Insert_Sqlist(Sqlist *&S,int i,int InsertNum)
{
  	if(i<=0 && i>S->Arrylength) 
  		return ERROR;
  	else
  		{
  			int j;
  			for(j=0;j<=S->Arrylength-i;j++)
  			 S->Arry[S->Arrylength-j]=S->Arry[S->Arrylength-1-j];    //i以后元素往后移动一位
  			 S->Arry[i-1]=InsertNum;
  			 S->Arrylength++;
  			 return OK;
  		}
}

//将第i个元素删除
  Statue Delete_Sqlist(Sqlist *&S,int i)
{
  	if(i<=0 && i>S->Arrylength) 
  		return ERROR;
  	else
  		{
  			int j;
  			for(j=0;j<=S->Arrylength-i;j++)
  			  S->Arry[i-1+j]= S->Arry[i+j];
  			 S->Arrylength--;
  			 return OK;
  			}
}

//删除值为x的元素
  void DeleteX_Sqlist(Sqlist *&S,int x)
{
  	int i;
  	for(i=0;i< S->Arrylength;i++)
      {
      	if(x == S->Arry[i])	
      		{
      			//Delete_Sqlist( *S, i);
				int j;
  			    for(j=0;j<=S->Arrylength-i;j++)
  			      S->Arry[i+j]= S->Arry[i+j+1];
  			    S->Arrylength--;
      		}
      	}
}

  //打印函数
  void print(Sqlist *&S)
  { 
    int m;
	for(m=0;m<S->Arrylength;m++)		
		{
			cout<<setw(4)<<S->Arry[m];
			if((m+1)%4==0)
				cout<<endl;
	     }
  }

int main()
{
	Sqlist *p1;
    Create_Sqlist( p1);
	cout<<"建立顺序表is OK"<<endl;
    Init_Sqlist(p1);
	cout<<"初始化顺序表is OK,数据如下:"<<endl;
	print(p1); 

	int i,InsertNum;
	cout<<"输入两个如下:";
	cin>>i;cin>>InsertNum;
	cout<<"插入数操作如下:在第"<<i<<"行插入数字"<<InsertNum<<"后。结构显示如下:"<<endl;
	Insert_Sqlist( p1, i, InsertNum);	
	print(p1);

	int k;
	cout<<endl;
	cout<<"输入一个数如下:";
	cin>>k;
	cout<<"删除数操作如下:"<<endl;
	cout<<"想要删除第"<<k<<"个数 显示如下:"<<endl;
    Delete_Sqlist(p1,k);	
    print(p1);

	int j;
	cout<<"输入一个数"<<endl;
    cin>>j;
	cout<<"删除指定数操作如下:输入想要删除的数是:"<<j<<"    结果显示如下:"<<endl;
    DeleteX_Sqlist(p1,j);
	print(p1);

	while(1);
	return 0;
	}
        显示结果如下:

bubuko.com,布布扣

                分析比较下面代码段的差别:

A段——建立空表没有bug的代码:

 typedef struct sqlist
   {
   	elem_type  *Arry;
   	int  Arrylength;
   	} Sqlist;
 
   //建立一个空表
  Statue Create_Sqlist(Sqlist *&S)
   {
        S = (Sqlist *)malloc(sizeof(Sqlist)); 
	S->Arry = (elem_type *)malloc(MaxSize);
   	}
B段——建立空表出现bug的代码:

typedef struct sqlist
   {
   	elem_type  Arry[MaxSize];
   	int  Arrylength;
   	} Sqlist;
 
   //建立一个空表
  Statue Create_Sqlist(Sqlist *S)
   {
    S->Arry = (elem_type *) malloc(MaxSize*sizeof(elem_type));  }
               
         
       




数据结构中构建顺序表,布布扣,bubuko.com

数据结构中构建顺序表

标签:数据结构   c语言   struct   malloc   

原文地址:http://blog.csdn.net/wqthaha/article/details/38301939

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!