线性表的定义源代码
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#define ERROR 0
#define OK 1
#define EQUAL 1
struct STU{
char name[20];
char stuno[10];
int age;
int score;
}stu[50];
typedef struct STU ElemType;
struct LIST
{
ElemType elem[50];
int length;
};
typedef struct LIST List;
int init(List **L)
{
*L=(List *)malloc(sizeof(List));
(*L)->length=0;
}
int ListLength(List *L)
{
return L->length;
}
void GetElem(List L,int i,ElemType *e)
{
*e=L.elem[i];
}
int EqualList(ElemType *e1,ElemType *e2)
{
if (strcmp(e1->name,e2->name))
return 0;
if (strcmp(e1->stuno,e2->stuno))
return 0;
if (e1->age!=e2->age)
return 0;
if (e1->score!=e2->score)
return 0;
return 1;
}
int LocateElem(List *La,ElemType e,int type)
{
int i;
switch (type)
{
case EQUAL:
for(i=0;i<La->length;i++)
if(EqualList(&La->elem[i],&e))
return 1;
break;
default:
break;
}
return 0;
}
void UnionList(List *La, List *Lb)
{
int La_len,Lb_len;
int i;
ElemType e;
La_len=ListLength(La); Lb_len=ListLength(Lb);
for(i=0;i<Lb_len;i++)
{
GetElem(*Lb,i,&e);
if(!LocateElem(La,e,EQUAL))
ListInsert(La,++La_len,e);
}
}
int printlist(List L)
{
int i;
printf("name stuno age score\n");
for(i=0;i<L.length;i++)
printf("%-10s %s\t%d\t%d\n", L.elem[i].name, L.elem[i].stuno,
L.elem[i].age, L.elem[i].score);
printf("\n");
}
int ListInsert(List *L,int i,struct STU e)
{
struct STU *p,*q;
if (i<1||i>L->length+1) return ERROR;
q=&(L->elem[i-1]);
for(p=&L->elem[L->length-1];p>=q;--p)
*(p+1)=*p;
*q=e;
++L->length;
return OK;
}/*ListInsert Before i */
main()
{
struct STU e;
List *La,*Lb;
clrscr();
printf("\n\n-------------------List Demo is running...----------------\n\n");
printf("First is InsertList function.\n");
init(&La);
strcpy(e.name,"stu1");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
ListInsert(La,1,e);
strcpy(e.name,"stu2");
strcpy(e.stuno,"100002");
e.age=80;
e.score=1000;
ListInsert(La,2,e);
printlist(*La);
printf("List A length now is %d.\n\n",La->length);
getch();
strcpy(e.name,"stu3");
strcpy(e.stuno,"100003");
e.age=80;
e.score=1000;
ListInsert(La,3,e);
printlist(*La);
printf("List A length now is %d.\n\n",La->length);
getch();
init(&Lb);
strcpy(e.name,"zmofun");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
ListInsert(Lb,1,e);
strcpy(e.name,"bobjin");
strcpy(e.stuno,"100002");
e.age=80;
e.score=1000;
ListInsert(Lb,2,e);
strcpy(e.name,"stu1");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
ListInsert(Lb,3,e);
printlist(*Lb);
printf("List B length now is %d.\n\n",Lb->length);
getch();
printf("Second is UnionList function.\n");
printf("Now union List A an
北邮数据结构实验一线性表及源代码_电脑基础知识_IT/计算机_专业资料。doc版附...1.实验要求根据线性表的抽象数据类型的定义, 选择下面任一种链式结构实现线性表,...
北京邮电大学数据结构实验报告与源代码实验一线性表北京邮电大学数据结构实验报告...1.实验要求根据线性表的抽象数据类型的定义, 选择下面任一种链式结构实现线性表,...
【数据结构算法】实验1 线性表应用-多项式计算(附源代码) 隐藏>> 浙江大学...要求:把多项式线性表的结构定义及多项式基本操作实现函数存放在文件 Seqpoly.h 中...
“按序号查找算法” 初始条件: 顺序线性表 L 已存在, 元素位置为 i, 1≤i...功能清 晰明了,其常量定义和常用系统函数原型声明的文件“pubuse.h”代码复用,...
第二章线性表1线性表定义及顺序存储_IT/计算机_专业资料。数据结构数据结构 第二章 线性表 2.1 2.2 2.3 线性表的类型定义 线性表的顺序表示和实现 线性表...
[MAXLISTSIZE]; /* 顺序表*/ /* 定义顺序表最大容量 */ /* 定义顺序表... C语言双向链表-代码 10页 1下载券 C语言线性链表 2页 免费喜欢此文档的还...
5页 1财富值 C++线性表源代码 1页 免费 线性链表源代码 4页 1财富值 ...(); //保存元素 //定义读取文件格式 //open 函数 //写入文件名 //写入...
35 基本操作的实现: 本书约定: 常量的定义: #define TRUE 1 #define FALSE ... typedef int Status; //函数类型,函数结果状态代码 36 第2章 线性表 ? ...
线性结构的数据包括: 线性表 栈 队列 串 2.1 线性表的类型定义 2.2 线性表的顺序表示和实现 2.3 线性表的链式表示和实现 2.1 线性表的类型定义线性表(...
实验指导-线性表 (1)_表格类模板_表格/模板_实用文档。实验一 线性表的应用...2、请根据如下存储结构定义改写第一题的代码: #define LIST_INIT_SIZE 100 #...
热门文档