1. 程式人生 > >線性表的操作

線性表的操作

標頭檔案部分
#include <stdio.h>
#include <string.h>
#define MAXSIZE 100  //定義線性表的最大長度
typedef struct    //定義順序表結構
{
    DATA ListData[MAXSIZE+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 SeqListFindByCont(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; } SL->ListData[++SL->ListLen]=data; return 1; } int SeqListInsert(SeqListType *SL,int n,DATA data) { int i; if(SL->ListLen>=MAXSIZE) //順序表結點數量已超過最大數量 { printf("順序表已滿,不能插入結點!\n"); return 0; //返回0表示插入不成功 } if(n<1 || n>SL->ListLen-1) //插入結點序號不正確 { printf("插入元素序號錯誤,不能插入元素!\n"); return 0; //返回0,表示插入不成功 } for(i=SL->ListLen;i>=n;i--) //將順序表中的資料向後移動 SL->ListData[i+1]=SL->ListData[i]; SL->ListData[n]=data; //插入結點 SL->ListLen++; //順序表結點數量增加1 return 1; //返回成功插入 } int SeqListDelete(SeqListType *SL,int n) //刪除順序表中的資料元素 { int i; if(n<1 || n>SL->ListLen+1) //刪除元素序號不正確 { printf("刪除結點序號錯誤,不能刪除結點!\n"); return 0; //返回0,表示刪除不成功 } for(i=n;i<SL->ListLen;i++) //將順序表中的資料向前移動 SL->ListData[i]=SL->ListData[i+1]; SL->ListLen--; //順序表元素數量減1 return 1; //返回成功刪除 } DATA *SeqListFindByNum(SeqListType *SL,int n) //根據序號返回資料元素 { if(n<1 || n>SL->ListLen+1) //元素序號不正確 { printf("結點序號錯誤,不能返回結點!\n"); return NULL; //返回0,表示不成功 } return &(SL->ListData[n]); } int SeqListFindByCont(SeqListType *SL,char *key) //按關鍵字查詢結點 { int i; for(i=1;i<=SL->ListLen;i++) if(strcmp(SL->ListData[i].key,key)==0) //如果找到所需結點 return i; //返回結點序號 return 0; //遍歷後仍沒有找到,則返回0 } 主函式部分 #include <stdio.h> typedef struct { char key[15]; //結點的關鍵字 char name[20]; int age; } DATA; //定義結點型別,可定義為簡單型別,也可定義為結構 #include "2-1 SeqList.h" #include "2-2 SeqList.c" int SeqListAll(SeqListType *SL) //遍歷順序表中的結點 { int i; for(i=1;i<=SL->ListLen;i++) printf("(%s,%s,%d)\n",SL->ListData[i].key,SL->ListData[i].name,SL->ListData[i].age); } int main() { int i; SeqListType SL; //定義順序表變數 DATA data,*data1; //定義結點儲存資料型別變數和指標變數 char key[15]; //儲存關鍵字 SeqListInit(&SL); //初始化順序表 do { //迴圈新增結點資料 printf("輸入新增的結點(學號 姓名 年齡):"); fflush(stdin); //清空輸入緩衝區 scanf("%s%s%d",&data.key,&data.name,&data.age); if(data.age) //若年齡不為0 { if(!SeqListAdd(&SL,data)) //若新增結點失敗 break; //退出死迴圈 }else //若年齡為0 break; //退出死迴圈 }while(1); printf("\n順序表中的結點順序為:\n"); SeqListAll(&SL); //顯示所有結點資料 fflush(stdin); //清空輸入緩衝區 printf("\n要取出結點的序號:"); scanf("%d",&i); //輸入結佔點序號 data1=SeqListFindByNum(&SL,i); //按序號查詢結點 if(data1) //若返回的結點指標不為NULL printf("第%d個結點為:(%s,%s,%d)\n",i,data1->key,data1->name,data1->age); fflush(stdin); //清空輸入緩衝區 printf("\n要查詢結點的關鍵字:"); scanf("%s",key); //輸入關鍵字 i=SeqListFindByCont(&SL,key); //按關鍵字查詢 ,返回結點序號 data1=SeqListFindByNum(&SL,i); //按序號查詢,返回結點指標 if(data1) //若結點指標不為NULL printf("第%d個結點為:(%s,%s,%d)\n",i,data1->key,data1->name,data1->age); getch(); return 0; } 這裡才是自己些的 //這個檔案可以直接賦值到C++中執行 #include <stdio.h> //標頭檔案 不用理他 #include <string.h> typedef struct //定義資料的內容 { char id[15]; char name[20]; int age; } DATA; typedef struct //定義順序表 { DATA ListData[101]; //儲存資料的陣列 int ListLen; //順序表的元素個數 }SeqListType; void main() //主函式 { SeqListType SL; //生成一個順序表格式的變數SL SeqListType *p; //生成一個順序表格式的指標p p=&SL; //讓這個指標指向SL順序表 這樣的話你就可以把p當成我們的順序表 DATA data,data1; //生成2個數據格式的變數 data和data1 p->ListLen=0; //初始階段順序表的元素個數為0 //增!!!!!!!!!!!(增加元素) do { //迴圈 新增元素 printf("輸入新增的結點(學號 姓名 年齡):"); fflush(stdin); //清空輸入緩衝區 不要管他 scanf("%s%s%d",&data.id,&data.name,&data.age); //輸入的值存在data變數中 if(data.age) //這個if是為了不讓它無限迴圈 {//如果年齡不為0就迴圈 p->ListData[++p->ListLen]=data;//把上邊輸入的元素新增到第1個元素中 同時元素數+1 }else //若年齡為0 break; //退出死迴圈 }while(1); printf("\n順序表增加後為:\n");//輸出增加之後的表 以便檢測錯誤 for(int i=1;i<=p->ListLen;i++)//迴圈1到現在的元素個數 簡單書就是遍歷表 printf("(%s,%s,%d)\n",p->ListData[i].id,p->ListData[i].name,p->ListData[i].age);//輸出當前個數的值 因為是一個for迴圈每次會不一樣 fflush(stdin);//不要管他 int n;//後邊用... //刪!!!!!! printf("請輸入需要刪除元素位置:"); scanf("%d",&n);//把輸入的值 賦值給n for(int c=n;c<p->ListLen;c++)//迴圈第n個元素 到最後一個元素 p->ListData[c]=p->ListData[c+1];//把從n+1每一個元素向前移一位 printf("刪除之後的表為:\n");//輸出刪除之後的表 以便檢測錯誤 for(int d=1;d<p->ListLen;d++) printf("(%s,%s,%d)\n",p->ListData[d].id,p->ListData[d].name,p->ListData[d].age); fflush(stdin); //所謂的刪除其實就是用後邊的把它覆蓋。 //改!!!!!!! printf("更改第幾號元素:學號 姓名 年齡"); scanf("%d %s %s %d",&n,&data.id,&data.name,&data.age); p->ListData[n]=data; //賦值給n元素 printf("\n順序表更改之後為:\n");//輸出更改之後的表 以便檢測錯誤 for(int b=1;b<p->ListLen;b++) printf("(%s,%s,%d)\n",p->ListData[b].id,p->ListData[b].name,p->ListData[b].age); fflush(stdin); //所謂的更改就是重新賦值 //查!!!!!!!!!! printf("要檢視第幾號元素:"); scanf("%d",&n); printf("(%s,%s,%d)\n",p->ListData[n].id,p->ListData[n].name,p->ListData[n].age); //所謂的查 就是輸出第n個元素 //插入!!!!! printf("插入第幾號元素:學號 姓名 年齡"); scanf("%d %s %s %d",&n,&data.id,&data.name,&data.age); for(int a=p->ListLen;a>=n;a--) //迴圈最後一個元素到第n個元素 p->ListData[a+1]=p->ListData[a]; //將順序表中的資料向後移動 p->ListData[n]=data; //給第n個元素賦值 printf("\n順序表插入之後為:\n");//輸出插入之後的表 以便檢測錯誤 for(int q=1;q<=p->ListLen;q++) printf("(%s,%s,%d)\n",p->ListData[q].id,p->ListData[q].name,p->ListData[q].age); fflush(stdin); //所謂的插入元素就是把元素都向後移一位 再把那個騰出來的位置賦值 }