线性表初始化插入消毁
2009-03-13 11:10
169 查看
#include "stdio.h"
#include "iostream.h"
#include "malloc.h"
typedef struct
{
int no;
int grade;
}ElemType;
typedef struct
{
ElemType *elem;
int CurrentLength;
int MaxSize;
}SqList;
void InitList(SqList *pL)
{
pL->MaxSize = 10;
pL->elem = (ElemType*)malloc(pL->MaxSize*sizeof(ElemType));
/*上句为pL->elem 申请pL->MaxSize个ElemType结构体所请求的内存空间,若成功返回
一个ElemType结构体类型的指针,并存入pL->elem变量中*/
pL->CurrentLength=0;
}
void DestroyList(SqList *pL)
{
free(pL->elem); /*释放内存空间*/
}
int ListInsert(SqList *pL,ElemType e)
{
int i,j;
if (pL->CurrentLength>=pL->MaxSize)
{
pL->MaxSize = pL->CurrentLength + 10;
pL->elem = (ElemType *)realloc(pL->elem,(pL->MaxSize)*sizeof(ElemType));
/*上句为pL->elem 重新申请内存空间*/
}
/*找插入点的位置*/
for(i=0;i<pL->CurrentLength;i++)
{
if (e.grade>pL->elem[i].grade)
break;
}
/*向后移动数据*/
for(j=pL->CurrentLength+1;j>i;j--)
pL->elem[j]=pL->elem[j-1];
pL->elem[i]=e;
pL->CurrentLength=pL->CurrentLength+1;
return 1;
}
void ShowList(SqList*pL)
{
int i;
for(i=0;i<pL->CurrentLength;i++)
{
printf("\n(%d,%d)",pL->elem[i].no,pL->elem[i].grade);
printf("\n");
}
}
void main()
{
SqList L;
ElemType e;
InitList(&L);
e.no=1;e.grade=85;
ListInsert(&L,e);
e.no=2;e.grade=80;
ListInsert(&L,e);
e.no=3;e.grade=70;
ListInsert(&L,e);
e.no=1;e.grade=85;
ListInsert(&L,e);
e.no=2;e.grade=80;
ListInsert(&L,e);
e.no=3;e.grade=70;
ListInsert(&L,e);
e.no=1;e.grade=85;
ListInsert(&L,e);
e.no=2;e.grade=80;
ListInsert(&L,e);
ShowList(&L);
DestroyList(&L);
}
在VC6 WINDOWS XP2 下编译通过 结果如下:
(1,85)
(1,85)
(1,85)
(2,80)
(2,80)
(2,80)
(3,70)
(3,70)
Press any key to continue
这是为什么呢
#include "iostream.h"
#include "malloc.h"
typedef struct
{
int no;
int grade;
}ElemType;
typedef struct
{
ElemType *elem;
int CurrentLength;
int MaxSize;
}SqList;
void InitList(SqList *pL)
{
pL->MaxSize = 10;
pL->elem = (ElemType*)malloc(pL->MaxSize*sizeof(ElemType));
/*上句为pL->elem 申请pL->MaxSize个ElemType结构体所请求的内存空间,若成功返回
一个ElemType结构体类型的指针,并存入pL->elem变量中*/
pL->CurrentLength=0;
}
void DestroyList(SqList *pL)
{
free(pL->elem); /*释放内存空间*/
}
int ListInsert(SqList *pL,ElemType e)
{
int i,j;
if (pL->CurrentLength>=pL->MaxSize)
{
pL->MaxSize = pL->CurrentLength + 10;
pL->elem = (ElemType *)realloc(pL->elem,(pL->MaxSize)*sizeof(ElemType));
/*上句为pL->elem 重新申请内存空间*/
}
/*找插入点的位置*/
for(i=0;i<pL->CurrentLength;i++)
{
if (e.grade>pL->elem[i].grade)
break;
}
/*向后移动数据*/
for(j=pL->CurrentLength+1;j>i;j--)
pL->elem[j]=pL->elem[j-1];
pL->elem[i]=e;
pL->CurrentLength=pL->CurrentLength+1;
return 1;
}
void ShowList(SqList*pL)
{
int i;
for(i=0;i<pL->CurrentLength;i++)
{
printf("\n(%d,%d)",pL->elem[i].no,pL->elem[i].grade);
printf("\n");
}
}
void main()
{
SqList L;
ElemType e;
InitList(&L);
e.no=1;e.grade=85;
ListInsert(&L,e);
e.no=2;e.grade=80;
ListInsert(&L,e);
e.no=3;e.grade=70;
ListInsert(&L,e);
e.no=1;e.grade=85;
ListInsert(&L,e);
e.no=2;e.grade=80;
ListInsert(&L,e);
e.no=3;e.grade=70;
ListInsert(&L,e);
e.no=1;e.grade=85;
ListInsert(&L,e);
e.no=2;e.grade=80;
ListInsert(&L,e);
ShowList(&L);
DestroyList(&L);
}
在VC6 WINDOWS XP2 下编译通过 结果如下:
(1,85)
(1,85)
(1,85)
(2,80)
(2,80)
(2,80)
(3,70)
(3,70)
Press any key to continue
这是为什么呢
相关文章推荐
- 线性表的顺序存储格式基本操作:初始化、插入、删除、查找、打印
- 线性表链式存储结构下基本操作的实现(初始化、赋值、取值、插入、删除、归并等)
- 线性表初始化、判空、插入、删除、遍历、修改、查值、合并
- 数据结构-顺序线性表的初始化,插入,删除
- 线性单链表 初始化 插入 取出 头插法 合并升序排列
- 使用线性表的顺序存储结构定义(静态)实现线性表的初始化、插入、删除和显示功能
- 线性表的初始化,头部和尾部插入,删除C/C++代码实现
- c语言实现线性表的建立,初始化,插入,删除,查找,遍历以及时间复杂度分析
- 线性表之顺序表的创建、初始化、查找、删除、插入和合并
- 数据结构实践---单链表的初始化,建立,插入,查找,删除。
- 循环链表 实现对循环链表的初始化,创建,插入,删除,输出操作
- hive中创建子表并插入数据过程初始化MR报错解决方法
- C语言实现顺序表的初始化,插入,删除,查找
- 最大堆的插入 删除 初始化 堆排序
- 链表(LinkList)的设计与实现(初始化,创建,插入,删除,逆置)
- 线性表的输入输出查找删除插入(链式,尾插入)
- 链表插入(线性表)
- 单链表的初始化,建立,插入,查找,删除
- 数据结构中静态顺序表的初始化、尾插、尾删、头插、头删、任意位置的插入及删除
- 双向链表的功能实现:初始化,插入,按位置插入,按位置删除,按值删除,遍历打印等