資源簡介
南郵數據結構實驗源碼。內容包括線性表及多項式運算、二叉樹基本操作的實現及哈夫曼、圖的基本運算實現和最短路徑問題、排序驗證

代碼片段和文件信息
#include
#include
typedef?int?ElemType;
typedef?struct?Node
{
ElemType?element;
struct?Node?*link;
}Node;???????????????????/*定義Node*/
typedef?struct
{
struct?Node?*head;
int?n;
}HeaderList;????????????/*定義HeaderList*/
/*初始化*/
void?Init(HeaderList?*h)
{
h->head=(Node*)malloc(sizeof(Node));
if?(!h->head)
printf(“初始化失敗\n“);
h->head->link=NULL;
h->head->element=0;
h->n=0;
printf(“初始化完成\n“);
return?;
}
/*插入,在第i個位置后插入*/
void?Insert(HeaderList?*hint?iElemType?x)
{
int?j;
if?(i<-1?||?i>h->n-1?)
printf(“插入位置不合理\n“);
Node?*p*q;
p=h->head;????????????????????????????????
for?(j=0;j<=i;j++)
p=p->link;????????????/*找到插入位置,第i處*/
q=(Node*)malloc(sizeof(Node));????????/*為q申請空間*/
q->element=x;
q->link=p->link;??
p->link=q;???????????????????/*插入成功*/
h->n++;?????????????????????/*更新長度值*/
printf(“插入成功\n“);
return?;
}
/*刪除第i個元素*/
void?Delete(HeaderList?*hint?i)
{
int?j;
Node?*p*q;
if(!h->n)
printf(“表空,沒有刪除項“);
if(i<-1||i>h->n-1)
printf(“下標不合法“);
p=h->head;
for(j=0;j p=p->link;??????????/*找到要刪除的前一項*/
q=p->link;???????????????/*q指向刪除的結點*/
p->link=q->link;???????????/*讓q指結點脫離鏈表*/
free(q);
h->n--;
return?;
}
/*查找第i個元素,將值存到x中*/
void?Find(HeaderList?hint?iElemType?*x)
{
int?j;
Node?*p;
if?(i<0?||?i>h.n-1?)
printf(“下標不合法\n“);
p=h.head;???????????????????????????????????????
for?(j=0;j p=p->link;
*x=p->link->element;
printf(“查找成功\n“);
return?;
}
/*輸出函數*/
void?Display(HeaderList?h)
{
????if(h.n==0)?
{
printf(“表空!\n“);
}
????Node?*p=h.head;
????while(p->link)
????{
????????p=p->link;
????????printf(“%d?“p->element);
????}
????printf(“\n“);
}
/*撤銷函數*/
void?Destroy(HeaderList?*h)
{
Node?*p;
while?(h->head->link)???
{
p=h->head->link;
free(h->head);
h->head=p;
h->n--;
}
return?;
}
/*逆置鏈表*/
void?Inverse(HeaderList?*h)
{
????if(h->n==0||h->n==1)?
printf(“無須逆置\n“);
????Node?*p1*p2*p3;
????p1=h->head;
????p2=p1->link;
????while(p2)
????{
????????p3=p2->link;
????????p2->link=p1;
????????p1=p2;
????????p2=p3;
????}
????h->head->link->link=NULL;
????h->head->link=p1;
????printf(“逆置成功\n“);
return?;
}
/*主函數*/
int?main()
{
int?i;
int?x;
int?mn;
HeaderList?h1;
Init(&h1);
for?(i=0;i<10;i++)
Insert(&h1i-1i+1);
printf(“輸出初始化列表:\n“);
Display(h1);
printf(“在第m個位置后插入元素n,請輸入mn:\n“);
scanf(“%d%d“&m&n);
Insert(&h1mn);
Display(h1);
Inverse(&h1);
printf(“輸出逆置后:\n“);
???Display(h1);
printf(“刪除第m個元素,輸入m:\n“);
scanf(“%d“&m);
Delete(&h1m);
Display(h1);
printf(“查找第m個元素,輸入m:\n“);
scanf(“%d“&m);
Find(h1m-1&x);
printf(“查找結果:“);
printf(“%d\n“x);
Destroy(&h1);
printf(“撤銷鏈表后輸出:\n“);
Display(h1);
return?0;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2019-03-10?20:03??數據結構實驗\
?????目錄???????????0??2019-03-10?20:03??數據結構實驗\數據結構實驗一\
?????文件????????3049??2019-03-10?18:47??數據結構實驗\數據結構實驗一\HeaderList.cpp
?????文件????????4678??2019-03-10?18:51??數據結構實驗\數據結構實驗一\polynominal.cpp
?????文件????????2165??2019-03-10?18:46??數據結構實驗\數據結構實驗一\SeqList.cpp
?????目錄???????????0??2019-03-10?20:03??數據結構實驗\數據結構實驗三\
?????文件????????9530??2019-03-10?19:43??數據結構實驗\數據結構實驗三\圖的鄰接表實現加Dijkstra實現.cpp
?????文件????????7659??2019-03-10?19:34??數據結構實驗\數據結構實驗三\帶權值的有向圖(鄰接矩陣).cpp
?????文件???????????0??2019-03-10?19:35??數據結構實驗\數據結構實驗三\記得更改cpp文件的名字(中文改成英文).txt
?????目錄???????????0??2019-03-10?20:03??數據結構實驗\數據結構實驗二\
?????文件????????9775??2019-03-10?19:18??數據結構實驗\數據結構實驗二\BiTree.cpp
?????文件????????2418??2019-03-10?19:21??數據結構實驗\數據結構實驗二\HFMTree.cpp
?????目錄???????????0??2019-03-10?20:03??數據結構實驗\數據結構實驗四\
?????文件????????6194??2019-03-10?19:50??數據結構實驗\數據結構實驗四\main.cpp
?????文件???????????0??2019-03-10?19:51??數據結構實驗\數據結構實驗四\排序驗證.txt
- 上一篇:南郵算法實驗之動態(tài)規(guī)劃法
- 下一篇:AT89C51流水燈
評論
共有 條評論