數據結構 順序表的操作(源代碼)

//順序表

#include <stdio.h>
#include <string.h>//strcmp()函數的頭文件
#include <conio.h> //getch()函數的頭文件,不包含也可使用,但會有警告
#pragma warning (disable : 4996)//忽略vs2012對printf()等函數的新定義
#define MAXSIZE 100

typedef struct 
{
	char key[15];//此程序中的key其實是一個數字串(學號),定義爲int也可
	char name[20];
	int age;
}DATA;
typedef struct 
{
	DATA ListData[MAXSIZE + 1];//保存順序表的數組 ,爲方便操作,0號不使用,故加1
	int ListLen;//順序表已存節點數量
}SeqListType;

void SeqListInit(SeqListType *SL);//初始化順序表
int SeqListLength(SeqListType *SL);//返回順序表元素數量
int SeqListAdd(SeqListType *SL,DATA data);//向順序表中添加元素
int SeqListInsert(SeqListType *SL, int n, DATA data);//插入元素
int SeqListDelete(SeqListType *SL, int n);//刪除元素
DATA *SeqListFindByNum(SeqListType *SL, int n);//按序號返回元素
int SeqListFindByCount(SeqListType *SL, char *key);//按關鍵字返回元素
int SeqListAll(SeqListType *SL);//遍歷表

void SeqListInit(SeqListType *SL)
{
	SL->ListLen = 0;//初始化長度爲0
}

int SeqListLength(SeqListType *SL)
{
	return (SL->ListLen);
}

int SeqListAdd(SeqListType *SL, DATA data)//添加節點
{
	if (SL->ListLen >= MAXSIZE)
	{
		printf("順序表已滿\n");
		return 0;//添加失敗時返回0
	}
	SL->ListData[ ++SL->ListLen ] = data;
	return 1;//添加成功時返回1
}

int SeqListInsert(SeqListType *SL, int n, DATA data)//插入節點,n爲指定要插入的序號,data爲需要插入的數據
{
	int i;
	if(SL->ListLen >= MAXSIZE)//判斷長度
	{
		printf("順序表已滿,不能插入節點\n");
		return 0;
	}
	if(n < 1 || n > SL->ListLen -1)//判斷序號是否越界
	{
		printf("插入節點序號錯誤\n");
		return 0;
	}
	for(i = SL->ListLen; i >= n; i--)//i移動到要插入的序號處
	{
		SL->ListData[i + 1] = SL->ListData[i];//n號以後的數據整體後移
	}
	SL->ListData[n] = data;
	SL->ListLen++;
	return 1;
}

int SeqListDelete(SeqListType *SL, int n)//刪除數據元素
{
	int i;
	if(n < 1 || n > SL->ListLen + 1)//判斷序號是否越界
	{
		printf("插入節點序號錯誤\n");
		return 0;
	}
	for(i = n; i < SL->ListLen; i++)//i從n移動到末尾
	{
		SL->ListData[i] = SL->ListData[i + 1];//數據整體前移
	}
	SL->ListLen--;
	return 1;
}

DATA *SeqListFindByNum(SeqListType *SL, int n)//根據序號返回元素,返回值爲一個地址
{
	if(n < 1 || n > SL->ListLen + 1)
	{
		printf("插入節點序號錯誤\n");
		return NULL;
	}
	return &(SL->ListData[n]);
}

int SeqListFindByCount(SeqListType *SL, char *key)//按關鍵字查詢
{
	int i;
	for(i = 1; i <= SL->ListLen; i++)
	{
		if(strcmp(SL->ListData[i].key, key) == 0)//strcmp爲比較函數,包含於string.h中
			return i;
	}
	return 0;
}

int SeqListAll(SeqListType *SL)
{
	int i;
	for(i = 1; i <= SL->ListLen; i++)
	{
		printf("(%d, %s, %s, %d)\n",i, SL->ListData[i].key, SL->ListData[i].name, SL->ListData[i].age);
	}
	return 0;
}

int main()
{
	int i;
	SeqListType SL;
	DATA data, *data1;
	char key[15];//保存關鍵字

	SeqListInit(&SL);

	 do  //do..while 循環添加元素,直到年齡爲0或者表已滿,但爲0元素不會添加到表中
	 {
		 printf("輸入添加的節點(學號、姓名、年齡):");
		 fflush(stdin);//清空輸入緩存區
		 scanf("%s %s %d",&data.key, &data.name, &data.age);
		 if(data.age)
		 {
			 if(!SeqListAdd(&SL, data))//若由於表已滿,添加失敗,並且這步執行添加操作
				 break;
		 }
		 else break;//若由於添加年齡小於0,添加失敗
	 }
	 while(1);
	 printf("\n順序表的節點順序爲:\n");
	 SeqListAll(&SL);

	 fflush(stdin);//清空輸入緩存區,因爲scanf函數會在輸入流緩存區中提取元素,
          //並且是提取到不合格的才停止,導致緩存區會有很多無用但對scanf有害的數據
	 printf("\n 要取出的節點序號:");
	 scanf("%d", &i);
	 data1 = SeqListFindByNum(&SL, i);
	 if(data1)
	 {
		 printf("第%d個結點爲:(%s,%s,%d)\n",i,data1->key,data1->name,data1->age);
	 }
	 fflush(stdin);                                                             //清空輸入緩衝區 
	 printf("\n要查找結點的關鍵字(即學號):");
	 scanf("%s",key);  //輸入關鍵字     
	 i=SeqListFindByCount(&SL,key);      //按關鍵字查找 ,返回結點序號 
	 fflush(stdin);  
	 printf("\n要插入的節點序號,以及學號,姓名,年齡:");
	 scanf("%d %s %s %d",&i, &data.key, &data.name, &data.age);
	 SeqListInsert(&SL, i, data);
	 printf("\n順序表的節點順序爲:\n");
	 SeqListAll(&SL);
	 fflush(stdin);
	 printf("\n要刪除的節點序號");
	 scanf("%d", &i);
	 SeqListDelete(&SL, i);
	 SeqListAll(&SL);
	 getch();//從鍵盤接收一個字符,但不顯示出來
	 return 0;
} 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章