| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
 Reworld，下班在家制作游戏，1500万奖金等你拿 以码会友 以友辅仁

问题点数：0  回复次数：1

#include <stdio.h>
#include <stdlib.h>

#define FALSE -1
#define TURE  0
#define Max_size 30
#define LIST_INIT_SIZE 10
typedef int ElemType;

typedef struct
{
ElemType pBase[Max_size];
int nLength;
int nSize;
}SqList;

int InitList(SqList *pList)    /*线性表初始化*/
{
pList = (SqList *) malloc(sizeof (SqList));
pList -> nLength = 0;
pList -> nSize = Max_size;
return TURE;
}

void DestroyList(SqList *pList)        /*销毁线性表*/
{
free(pList);
}

int ListLength(SqList *pList)        /*线性表的长度*/
{
return (pList -> nLength);
}

int ListEmpty(SqList *pList)        /*判断线性表是否为空*/
{
if (pList -> nLength == 0)
return FALSE;
else
return TURE;
}

int ShowList(SqList *pList)        /*输出线性表*/
{
int pos, n = 0;
if (ListEmpty (pList))
return FALSE;
for (pos = 0; pos < pList -> nLength; pos++)
{
printf ("%d  ", pList -> pBase[pos]);
n++;
if (n % 5 == 0)
printf ("\n");
}

printf ("\n");
}

int GetElem(SqList *pList, int pos, int e)        /*求线性表中某个元素的值*/
{
if (pos < 1 || pos > pList -> nLength)
return FALSE;
else
e = pList -> pBase [pos - 1];

return TURE;

}

int LocateElem(SqList *pList, int e)        /*查找元素e*/
{
int pos;
for (pos = 0; pos < pList -> nLength && pList -> pBase[pos] != e;)
pos++;
if (pos >= pList -> nLength)
return FALSE;
else
return (pos + 1);
}

int ListDelete(SqList *pList, int pos, int e)            /*删除数据元素*/
{
int j;
if (pos < 1 || pos > pList -> nLength)
return FALSE;
pos--;
e = pList -> pBase[pos];

for (j = pos; j < pList -> nLength; ++j)
{
pList -> pBase[j - 1] = pList -> pBase[j];
}

--pList -> nLength;
return TURE;
}

int ListInsert(SqList *pList, int pos, int e)            /*插入数据元素*/
{
int j;
if (pos < 1 || pos > pList -> nLength)
return FALSE;
pos--;

for (j = pList -> nLength; j > pos; j--)
{
pList -> pBase[j] = pList -> pBase[j - 1];
}

pList -> pBase[pos] = e;
++pList -> nLength;
return TURE;
}

int ReadList(SqList *pList,int n)        /*从文件读入n(n > 10)个整数,填入顺序表*/
{
int i;
FILE * fp = fopen("list.txt","rw");

if (fp == NULL)
{
printf ("错误！无法读入文件！");
exit(0);
}

for(i = 0; i < n; i++)
fscanf(fp,"%d ",&pList -> pBase[i]);

pList -> nLength = n;
fclose(fp);
return TURE;
}

int SaveList(SqList *pList)            /*将线性表保存到文件中*/
{
FILE *fp;
int i;
fp=fopen("list.txt","r+");

for(i = 0;i < pList -> nLength; i++)
fprintf(fp,"%d ", pList -> pBase[i]);

fclose(fp);
return TURE;
}

int cShowList(SqList *pList)
{
int pos, n, t = 0;
if (ListEmpty (pList))
return FALSE;
for (pos = 0; pos < (pList -> nLength) / 2; pos++)
{
n = pList -> pBase[pos];
pList -> pBase[pos] = pList -> pBase[pList -> nLength - pos - 1];
pList -> pBase[pList -> nLength - pos - 1] = n;
}

for (pos = 0; pos < pList -> nLength; pos++)
{
t++;
printf ("%d  ", pList -> pBase[pos]);
if (t % 5 == 0)
printf ("\n");

}
}

int main()
{      int n, e1, e2;
SqList pList;
printf ("(1).初始化线性表\n");
InitList (&pList);

printf ("请输入n的值:  ");
scanf ("%d", &n);
printf ("\n");

printf ("(2).输出%d个数至屏幕\n", n);
ShowList(&pList);
printf ("\n");

printf ("(3).查找元素的位序\n");
printf ("元素7的位序为%d\n", LocateElem(&pList, 7));
printf ("\n");

printf ("(4).在顺序表头、中、尾各插一个元素\n");
ListInsert(&pList, 1, 1);
ListInsert(&pList, 9, 8);
ListInsert(&pList, 17, 15);
ShowList(&pList);
printf ("\n");

printf ("(5).输入修改后的表至屏幕,并保存至文件\n");
ShowList(&pList);
SaveList(&pList);
printf ("\n");

printf ("(6).删除顺序表的任意两个元素\n");
ListDelete(&pList, 5, e1);
ListDelete(&pList, 11, e2);
ShowList(&pList);
SaveList(&pList);
printf ("\n");

printf ("(7).输入修改后的表至屏幕，并保存至文件\n");
ShowList(&pList);
SaveList(&pList);
printf ("\n");

printf ("(8).逆序排列顺序表，输出至屏幕，并保存至文件\n");
cShowList(&pList);
SaveList(&pList);

DestroyList(&pList);
printf ("\n");

system("pause");

}

得分:0

• 2
• 1/1页
• 1

Powered by Discuz, Processed in 0.029927 second(s), 8 queries.