第二章(1).线性表
来源:互联网 发布:powershell管理linux 编辑:程序博客网 时间:2024/06/05 20:26
#include<stdio.h>
#include<stdlib.h>#define LIST_INIT_SIZE 10 //线性表存储空间的初始分配量
#define LISTADD 10 //线性表存储空间的分配增量
#define TRUE 1
#define FALSE 0
#define ERROR -1typedef int ElemType;
typedef struct
{
ElemType *elem; //存储空间基址
int length; //当前长度
int listsize; //当前分配的容量
}SqList , *Sqlist;void InitList_Sq(Sqlist L) //构造一个空线性表
{
L->elem = (ElemType *)malloc(sizeof(ElemType)*LIST_INIT_SIZE);
if(!L->elem) exit(0);
L->length = 0; //当前长度
L->listsize = LIST_INIT_SIZE; //当前分配的存储容量(以sizeof(ElemType)为单位)
}int ListLength(SqList L) //求线性表长度
{
return L.length;
}int ListEmpty(SqList L) //判断线性表是否为空
{
if(L.length == 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
void ListInsert(Sqlist L , int i , ElemType e) //在线性表中插入元素
{
ElemType *q,*p;
ElemType *newbase;
if(i < 1 || i > L->length + 1)
{
exit(0);
}
else
{
if(L->length >= L->listsize) //存储空间已满,需要增加分配
{
newbase = (ElemType *)realloc(L->elem,(L->listsize + LISTADD)*sizeof(ElemType)); //realloc.追加
if(!newbase)
{
exit(0); //分配失败
}
else
{
L->elem = newbase; //新基址
L->listsize += LISTADD; //增加后长度
}
}
else
{
q = &(L->elem[i - 1]); //q为插入的位置
for( p = &(L->elem[L->length - 1]) ; p >= q; --p )
{
*(p+1) = *p; //插入位置及以后的元素都右移
}
*q = e; //插入e
++L->length;
}
}
}
ElemType GetElem(SqList L, int i, ElemType *e) //得到线性表中的元素并返回
{
if(i < 1 || i > L.length )
{
exit(0);
}
else
{
*e = L.elem[i-1];
return *e;
}
}ElemType ListDelete(Sqlist L,int i,ElemType *e) //删除元素
{
ElemType *q,*p;
if(i < 1 || i > L->length )
{
exit(0);
}
else
{
p = &(L->elem[i-1]); //q为删除的元素的位置
*e= *p;
q = L->elem + L->length - 1 ;
for( ++ p ; p <= q ; ++ p )
{
*(p-1) = *p; //删除元素位置及以后的元素都左移
}
--L->length;
return *e;
}
}//返回第一个与e满足关系compare()的数据元素的位序,若这样的元素不存在,则返回值为0.
int LocateElem(SqList L,ElemType e) //LocateElem( L, e,compare()),compare()为数据元素判定函数
{ //此处只比较非字符
int i;
for(i = 0 ; i < L.length ; i++)
{
if(e == L.elem[i] )
{
return (i+1);
}
}
return 0;
}
int main(void)
{
SqList L;
int i;
ElemType *e ;
e = NULL;
InitList_Sq(&L);
printf("%d\n", ListLength(L));
for(i = 1;i < 12 ; i++) {
ListInsert(&L,i,i); }
ListInsert(&L,12,13);
printf("%d\n",GetElem(L,1,e));
// printf("%d\n",ListDelete(&L,3,e));
// printf("%d\n",GetElem(L,2,e));
printf("%d\n",LocateElem( L,13 ));return 0;
}//**************************数组合并*****************************//
#include<stdio.h>
#include<stdlib.h>#define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量
#define LISTADD 10 //线性表存储空间的分配增量
#define TRUE 1
#define FALSE 0
#define ERROR -1typedef int ElemType;
typedef struct
{
ElemType *elem; //存储空间基址
int length; //当前长度
int listsize; //当前分配的容量
}SqList , *Sqlist;
void InitList_Sq(Sqlist L) //构造一个空线性表
{
L->elem = (ElemType *)malloc(sizeof(ElemType)*LIST_INIT_SIZE);
if(!L->elem)exit(0);
L->length = 0; //当前长度
L->listsize = LIST_INIT_SIZE; //当前分配的存储容量(以sizeof(ElemType)为单位)
}int ListLength(SqList L) //求线性表长度
{
return L.length;
}int ListEmpty(SqList L) //判断线性表是否为空
{
if(L.length == 0)
{
return TRUE;
}
else
{
return FALSE;
}
}void ListInsert(Sqlist L , int i , ElemType e) //在线性表中插入元素
{
ElemType *q,*p;
ElemType *newbase;
if(i < 1 || i > L->length + 1)
{
exit(0);
}
else
{
if(L->length >= L->listsize) //存储空间已满,需要增加分配
{
newbase = (ElemType *)realloc(L->elem,(L->listsize + LISTADD)*sizeof(ElemType)); //realloc.追加
if(!newbase)
{
exit(0); //分配失败
}
else
{
L->elem = newbase; //新基址
L->listsize += LISTADD; //增加后长度
}
}
else
{
q = &(L->elem[i - 1]); //q为插入的位置
for( p = &(L->elem[L->length - 1]) ; p >= q; --p )
{
*(p+1) = *p; //插入位置及以后的元素都右移
}
*q = e; //插入e
++L->length;
}
}
}
ElemType GetElem(SqList L, int i, ElemType *e) //得到线性表中的元素并返回
{
if(i < 1 || i > L.length )
{
exit(0);
}
else
{
*e = L.elem[i-1];
return *e;
}
}ElemType ListDelete(Sqlist L,int i,ElemType *e) //删除元素
{
ElemType *q,*p;
if(i < 1 || i > L->length )
{
exit(0);
}
else
{
p = &(L->elem[i-1]); //q为删除的元素的位置
*e= *p;
q = L->elem + L->length - 1 ;
for( ++ p ; p <= q ; ++ p )
{
*(p-1) = *p; //删除元素位置及以后的元素都左移
}
--L->length;
return *e;
}
}//返回第一个与e满足关系compare()的数据元素的位序,若这样的元素不存在,则返回值为0.
int LocateElem(SqList L,ElemType e) //LocateElem( L, e,compare()), compare()为数据元素判定函数
{
int i;
for(i = 0 ; i < L.length ; i++)
{
if(e == L.elem[i] )
{
return (i+1);
}
}
return 0;
}
//数组合并,剔除相同元素
void Union(Sqlist La , SqList Lb) //将所有在Lb中且不在La中的元素添加到La中
{
int i,La_len,Lb_len;
ElemType e,a;
La_len = ListLength(*La),Lb_len = ListLength(Lb);
for(i = 1; i <= Lb_len ; i++)
{
a = GetElem(Lb,i,&e); //取Lb中第i个数据元素赋给e必须赋值,不然后面的e则仍然为一个地址值
// printf("%d\n",GetElem(Lb,i,&e)); It's right!
// printf("%d \n",e); It's wrong!
if(!LocateElem(*La,a))
{
// printf("A");
ListInsert(La,++La_len,a);
// printf("%d\n",GetElem(*La,6,&e));
}
// printf("%d\n",ListLength(*La));
}
}
int main(void)
{
int i,j;
SqList La,Lb;
ElemType *e;
int a[] = {1,2,3,4,5};
int b[] = {4,5,6,7,8,9,0};
e = NULL; //不要忘记初始化InitList_Sq(&La);
InitList_Sq(&Lb);
for(i = 0; i < 5 ;i++)
{
La.elem[i] = a[i];
++La.length;
}
// printf("%d\n",ListLength(La));
// printf("%d\n",GetElem(La,4,e));
for(j = 0; j < 7 ; j++)
{
Lb.elem[j] = b[j];
++Lb.length;
}
// printf("%d\n",ListLength(Lb));
// printf("%d\n",GetElem(Lb,6,e));Union(&La,Lb); //合并
// printf("%d\n",ListLength(La));
for(i = 1; i <= ListLength(La); i++)
{
printf("%d",GetElem(La,i,e));
if(i%5 == 0)
{
printf("\n");
}
}
printf("\n");
return 0;
}
- 第二章(1).线性表
- 第二章 线性表
- 第二章:线性表
- 第二章 线性表
- 第二章 线性表
- 第二章 线性表
- 线性表 第二章
- 第二章:线性表
- 第二章线性表
- 第二章 线性表
- 第二章:线性表
- 第二章:线性表
- 第二章线性表
- 第二章 线性表
- 第二章 线性表
- 第二章 线性表
- 第二章线性表
- 第二章 线性表
- Oracle建立数据表的前期工作及常用的管理命令
- 2015编程之美初赛第二场扑克牌
- 前言
- 第二章(3).静态链表
- unicode下各种类型转换,CString,string,char*,int,char[]
- 第二章(1).线性表
- java面向对象的三大特性之继承
- 第二章(2).链表
- 第二章(4).循环链表
- Oracle中关于PCTFREE和PCTUSED的说明
- 第二章(5).双向循环链表
- 腾讯 2016届实习生招聘笔试
- 10句编程箴言 (转)
- HDOJ Can you find it? 2141(二分搜索)