您的位置:首页 > 编程语言 > C语言/C++

顺序表C语言代码实现

2009-10-19 09:54 513 查看
#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<malloc.h>

#define MAXSIZE 100

/****************************************

* 结构体的声明 *

****************************************/

typedef struct std_info

{

int Num;

char Name[8];

float Score;

}ElemType;

/****************************************

* 头指针的定义 *

****************************************/

typedef struct

{

ElemType elem[MAXSIZE];

int length;

}SeqList;

/****************************************

* 创建链表 *

****************************************/

void Create_SeqList(SeqList *L1)

{

int i,n;

int num;

char name[8];

float score;

printf("请输入顺序表的长度:/n");

scanf("%d",&n);

L1->length=n;

for(i=1;i<=L1->length;i++)

{

printf("请输入一个学生的信息:Num,Name,Score/n");

scanf("%d",&num);

scanf("%s",name);

scanf("%f",&score);

getchar();

L1->elem[i].Num=num;

strcpy(L1->elem[i].Name,name);

L1->elem[i].Score=score;

}

}

/****************************************

* 链表输出 *

****************************************/

void Printf_SeqList(SeqList *L1)

{

int i;

printf("/t/t***Printf_SeqList***/n");

printf("Num/tName/t/tScore/n");

for(i=1;i<=L1->length;i++)

{

printf("%d/t%s/t/t%f/n",L1->elem[i].Num,L1->elem[i].Name,L1->elem[i].Score);

}

printf("/n");

}

/****************************************

* 链表-插入 *

****************************************/

int Insert_SeqList(SeqList *L1,int i,ElemType elemtype)

{

int j;

if(L1->length==MAXSIZE-1)

{

printf("Surface/n");

return (-1);

}

if(i<1||i>L1->length+2)

{

printf("position error/n");

return (0);

}

for(j=L1->length;j>=i-1;j--)

{

L1->elem[j+1]=L1->elem[j];

}

L1->elem[i-1]=elemtype;

L1->length++;

return (1);

}

/****************************************

* 链表-删除 *

****************************************/

int Delete_SeqList(SeqList *L1,int i)

{

int j;

if(i<1||i>L1->length+1)

{

printf("The elem doesn't i/n");

return (0);

}

for(j=i;j<L1->length;j++)

{

L1->elem[j-1]=L1->elem[j];

}

L1->length--;

return (1);

}

/****************************************

* 链表-逆置 *

****************************************/

int Inversion_SeqList(SeqList *L1)

{

int i,j;

ElemType temp;

for(i=1,j=L1->length;i<j;i++,j--)

{

temp=L1->elem[i];

L1->elem[i]=L1->elem[j];

L1->elem[j]=temp;

}

return 1;

}

/****************************************

* 链表-排序 *

****************************************/

int Sort_SeqList(SeqList *L1)

{

int i,j;

ElemType temp;

for(j=1;j<=L1->length-1;j++)

for(i=1;i<=L1->length-j;i++)

{

if(L1->elem[i].Num>L1->elem[i+1].Num)

{

temp=L1->elem[i];

L1->elem[i]=L1->elem[i+1];

L1->elem[i+1]=temp;

}

}

return 1;

}

/****************************************

* 链表-顺序插入 *

****************************************/

int Insert1_SeqList(SeqList *L1,ElemType elemtype)

{

int i;

if(L1->length==MAXSIZE-1)

{

printf("表满/n");

}

++L1->length;

for(i=L1->length-1;L1->elem[i].Num>elemtype.Num;i--)

L1->elem[i+1]=L1->elem[i];

L1->elem[i+1]=elemtype;

return 1;

}

/****************************************

* 链表-删除i以后k个元素 *

****************************************/

int Delete1_SeqList(SeqList *L1,int i,int k)

{

int j;

if(i<1||k<0||i+k-1>L1->length-1)

{

printf("删除位置错误/n");

return (0);

}

for(j=1;i+j-1<=i+k;j++)

{

L1->elem[i+j-1]=L1->elem[i+j];

}

return 1;

}

/****************************************

* 链表操作 *

****************************************/

void Print(SeqList *L1)

{

int i,j=1,k;

int num;

char name[8];

float score;

ElemType elemtype;

printf("/t&&Please select the operation of the table&&/n");

while(j)

{

printf("***********************************************************/n");

printf("*/t1.The order of the table insert/n");

printf("*/t2.The order of the table delete/n");

printf("*/t3.The order of the table Inversion(逆置)/n");

printf("*/t4.The order of the table sort /n");

printf("*/t5.插入一个学生信息放入递增排序的顺序表适当位置/n");

printf("*/t6.删除第i个元素后的k个元素/n");

printf("*/t0.Exit/n");

printf("*************************************************************/n");

scanf("%d",&j);

switch(j)

{

case 0:

printf("Bye-Bye ..../n");

exit(1);

break;

case 1:

printf("Please input one student massege:Num,Name,Score/n");

scanf("%d",&num);

scanf("%s",name);

scanf("%f",&score);

getchar();

printf("Please input number to Insert_SeqList/n");

scanf("%d",&i);

elemtype.Num=num;

strcpy(elemtype.Name,name);

elemtype.Score=score;

i=Insert_SeqList(L1,i,elemtype);

Printf_SeqList(L1);

printf("OK/n");

break;

case 2:

printf("Please enter the Location you want to delete/n");

scanf("%d",&i);

i=Delete_SeqList(L1,i);

Printf_SeqList(L1);

printf("OK/n");

break;

case 3:

Inversion_SeqList(L1);

Printf_SeqList(L1);

printf("OK/n");

break;

case 4:

Sort_SeqList(L1);

Printf_SeqList(L1);

printf("OK/n");

break;

case 5:

printf("Please you input one student Num Name Score/n");

scanf("%d",&num);

scanf("%s",name);

scanf("%f",&score);

elemtype.Num=num;

strcpy(elemtype.Name,name);

elemtype.Score=score;

Insert1_SeqList(L1,elemtype);

Printf_SeqList(L1);

printf("OK/n");

break;

case 6:

printf("Please input i and delete i last k number/n");

scanf("%d",&i);

scanf("%d",&k);

Delete1_SeqList(L1,i,k);

Printf_SeqList(L1);

printf("OK/n");

break;

default :printf("Input error,Retype number/n");

}

}

}

void main()

{

SeqList *L=(SeqList *)malloc(sizeof(SeqList));

Create_SeqList(L);

Printf_SeqList(L);

Print(L);

free(L);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: