数据结构与算法---线性表
2017-04-18 20:04
225 查看
#include<stdio.h>
#include<stdlib.h>
typedef int ElementType;
#define Bool int
#define TRUE 1
#define FALSE 0
struct linearList
{
ElementType *data;
int MaxSize;
int Last;
};
typedef struct linearList LinearList;
void InitList(LinearList *L,int sz)
{
if(sz > 0)
{
L->MaxSize = sz;
L->Last = 0;
L->data = (ElementType *)malloc (sizeof(ElementType) * L->MaxSize);
}
}
void FreeList(LinearList *L)
{
free(L->data);
}
Bool ListEmepty(LinearList *L)
{
return (L->Last <= 0) ? TRUE:FALSE;
}
Bool ListFull(LinearList *L)
{
return (L->Last >= L->MaxSize) ? TRUE:FALSE;
}
int ListLength(LinearList *L)
{
return L->Last;
}
ElementType GetElem(LinearList *L,int i)
{
return (i<0 || i>= L->Last) ? NULL: L->data[i];
}
int LocateElem(LinearList *L ,ElementType x)
{
int i;
for(i=0;i<L->Last;i++)
if(L->data[i] == x) return i;
return -1;
}
Bool InsertElem(LinearList *L,ElementType x,int i)
{
int j;
if(i<0 || i> L->Last || L->Last == L->MaxSize)
return FALSE;
else
{
for(j=L->Last-1;j>=i;j--) L->data[j+1] = L->data[j];
L->data[i] = x;
L->Last++;
return TRUE;
}
}
Bool DeleteElem(LinearList *L,int i)
{
int j;
if(i<0 || i>= L->Last || L->Last == 0)
return FALSE;
else
{
for(j=i;j<L->Last-1;j++)
L->data[j] = L->data[j+1];
L->Last--;
return TRUE;
}
}
void printout(LinearList *L)
{
int i;
for(i=0;i<L->Last;i++)
printf("%d ",L->data[i]);
printf("\n");
}
int main()
{
LinearList *L = (LinearList *) malloc(sizeof(LinearList));
InitList(L,5);
InsertElem(L,10,0);
InsertElem(L,20,0);
InsertElem(L,30,0);
InsertElem(L,40,0);
InsertElem(L,50,0);
if(InsertElem(L,60,0))
printout(L);
else if(ListFull(L))
printf("list is full,failed to insert \n");
printout(L);
DeleteElem(L,1);
DeleteElem(L,1);
printf("after twice delteions the list is\n");
printout(L);
printf("the location of data 20 is %d\n",LocateElem(L,20));
printf("the 3rd value is %d\n",GetElem(L,2));
FreeList(L);
return 0;
}
#include<stdlib.h>
typedef int ElementType;
#define Bool int
#define TRUE 1
#define FALSE 0
struct linearList
{
ElementType *data;
int MaxSize;
int Last;
};
typedef struct linearList LinearList;
void InitList(LinearList *L,int sz)
{
if(sz > 0)
{
L->MaxSize = sz;
L->Last = 0;
L->data = (ElementType *)malloc (sizeof(ElementType) * L->MaxSize);
}
}
void FreeList(LinearList *L)
{
free(L->data);
}
Bool ListEmepty(LinearList *L)
{
return (L->Last <= 0) ? TRUE:FALSE;
}
Bool ListFull(LinearList *L)
{
return (L->Last >= L->MaxSize) ? TRUE:FALSE;
}
int ListLength(LinearList *L)
{
return L->Last;
}
ElementType GetElem(LinearList *L,int i)
{
return (i<0 || i>= L->Last) ? NULL: L->data[i];
}
int LocateElem(LinearList *L ,ElementType x)
{
int i;
for(i=0;i<L->Last;i++)
if(L->data[i] == x) return i;
return -1;
}
Bool InsertElem(LinearList *L,ElementType x,int i)
{
int j;
if(i<0 || i> L->Last || L->Last == L->MaxSize)
return FALSE;
else
{
for(j=L->Last-1;j>=i;j--) L->data[j+1] = L->data[j];
L->data[i] = x;
L->Last++;
return TRUE;
}
}
Bool DeleteElem(LinearList *L,int i)
{
int j;
if(i<0 || i>= L->Last || L->Last == 0)
return FALSE;
else
{
for(j=i;j<L->Last-1;j++)
L->data[j] = L->data[j+1];
L->Last--;
return TRUE;
}
}
void printout(LinearList *L)
{
int i;
for(i=0;i<L->Last;i++)
printf("%d ",L->data[i]);
printf("\n");
}
int main()
{
LinearList *L = (LinearList *) malloc(sizeof(LinearList));
InitList(L,5);
InsertElem(L,10,0);
InsertElem(L,20,0);
InsertElem(L,30,0);
InsertElem(L,40,0);
InsertElem(L,50,0);
if(InsertElem(L,60,0))
printout(L);
else if(ListFull(L))
printf("list is full,failed to insert \n");
printout(L);
DeleteElem(L,1);
DeleteElem(L,1);
printf("after twice delteions the list is\n");
printout(L);
printf("the location of data 20 is %d\n",LocateElem(L,20));
printf("the 3rd value is %d\n",GetElem(L,2));
FreeList(L);
return 0;
}
相关文章推荐
- 数据结构之算法2.6-在顺序线性表L中查找第1个值与e满足compare()的元素的位序
- [数据结构与算法] 3,线性表
- 再回首,数据结构——线性表、链表上的常见算法
- 数据结构和算法 (二)数据结构基础、线性表、栈和队列、数组和字符串
- 数据结构与算法--线性表
- 数据结构与算法——线性表链式存储(单循环链表)
- 【数据结构与算法003】基本数据结构——线性表(链表)
- 【数据结构与算法】线性表备忘
- 数据结构——线性表中的算法
- 数据结构与算法学习笔记02_2(线性表)
- 数据结构与算法——线性表链式存储(单链表)
- (4) 数据结构与算法 ---- 线性表 及Java实现 顺序表、链表、栈、队列
- Uva 算法入门经典(数据结构基础)线性表题目
- 数据结构与算法——线性表(一)
- 数据结构.算法 近期计划
- 数据结构及算法-何谓数据结构
- Pku acm 1469 COURSES 数据结构题目解题报告(十一)---- 匈牙利算法求二分图的最大匹配
- Pku acm 1125 Stockbroker Grapevine 数据结构题目解题报告(八)---- 弗洛伊德(floyd)算法
- 数据结构中关键路径算法的实现与应用
- Pku acm 2536 Gopher II 数据结构题目解题报告(十四)---- 匈牙利算法求二分图的最大匹配