資料結構,嚴蔚敏c語言版。書上的基本實現。
1.h://預定義標頭檔案 和 巨集定義(函式結果狀態**)。
#pragma once
#include#include#define true 1
#define false 0
#define ok 1
#define error 0
#define infeasible -1
#define over -2
#define n 10
typedef int santus;
2.h:定義單向鍊錶的結構體。
採用遞迴定義,next指向下乙個結構體。
#pragma once
typedef int elemtype;
typedef struct lnode lnode,*linklist;
3.h:
#pragma once
#include"1.h"
#include"2.h"
using namespace std;
void show()
//santus list_creat2(linklist &l)
santus list_creat(linklist & l, elemtype a[n])
l->next = null;
linklist l1 = l;
for (int j = 0; j < n; j++)
p->data = a[j];
p->next = nullptr;
l1->next = p;
l1 = l1->next;
} return ok;
}void linkedlistcreatetaill(linklist& l, elemtype a[n])
l->next = null;
linklist tail = l; //設定尾指標,方便插入
for (int j = 0; jdata = a[j];
p->next = null;
tail->next = p;
tail = p; }}
santus getelem(linklist l, int i, elemtype&e)
if (!p||j>i)
return error;
e = p->data;
return ok; }
santus list_insert(linklist & l,int i,elemtype e)
if (!p || j > i - 1)
return error;
linklist q = (linklist)malloc(sizeof(lnode));
if (!q)
return error;
q->data = e;
q->next = p->next;
p->next = q;
return ok;
}void read(linklist l) }}
santus list_delet(linklist &l, int i, elemtype &e)
if (!p->next || j > i - 1)
return error;
linklist q = p->next;
e = q->data;
p->next = q->next;
return ok; }
santus clear_list(linklist&l)
free(q);
l->next = null;
return ok;
}santus destory_list(linklist& l)
return ok;
}santus list_empty(linklist l)
int list_length(linklist l)
return j;
}santus compare(elemtype e1, elemtype e2 )
int locate_elem(linklist l, elemtype e)
if (!p)
return 0;
return j;
}santus prior_elem(linklist l, elemtype cur_e, elemtype &pre_e)
if (!p||j>locate_elem(l, cur_e) - 1)
return error;
pre_e = p->data;
return ok;
}santus next_elem(linklist l, elemtype cur_e, elemtype &next_e)
if (!p->next || j > locate_elem(l, cur_e))
return error;
next_e = p->next->data;
return ok;
}
text.cpp:
#include"1.h"
#include"2.h"
#include"3.h"
using namespace std;
int main() ,e,e2;
list_creat(l, a);
//linkedlistcreatetaill(l, a);
int i;
show();
do break;
case 2:
cout<<"input set" << endl;
cin >> i;
cout << "input num" << endl;
cin >> e;
if (list_insert(l, i, e) == error)
cout << "插入失敗" << endl;
else
break;
case 3:
cout << "input set" << endl;
cin >> i;
if (list_delet(l, i, e) == error)
else
break;
case 4://清空鍊錶
if (clear_list(l))
cout<<"清空成功"<> e;
if (locate_elem(l, e) != 0)
cout << "存在於" << locate_elem(l, e) <<"位置"<< endl;
else
cout << "不存在" << endl;
break;
case 9:
if (destory_list(l))
cout << "銷毀成功" << endl;
break;
case 10:
cout << "input number" << endl;
cin >> e;
if (prior_elem(l, e, e2) != error)
cout << e2 << endl;
else
cout << "錯誤" << endl;
break;
case 11:
cout << "input number" << endl;
cin >> e;
if (next_elem(l, e, e2) != error)
cout << e2 << endl;
else
cout << "錯誤" << endl;
break;
default:
cout << "input right select" << endl;
break;
} } while (1);
system("pause");
return 0;
}
基本資料結構單向鍊錶
第一次發文章,謬誤之處請各位大佬指正。單向鍊錶的原理還是很簡單的而且和雙向鍊錶有很多相同之處 而且鍊錶和二叉搜尋樹有著很多聯絡以後我會在二叉搜尋樹的章節中寫一篇將二叉搜尋樹改寫成煉表的文章 只改變指標的指向 一下在 的注釋中寫出鍊錶的實現 include include struct node he...
資料結構 單向鍊錶
鍊錶結構的資料格式儲存 include stdafx.h 把這行放在最開始。include includeusing namespace std typedef struct data typedef struct node 這裡與上面的不同是多了node,注意如果沒有這個node,下面的struc...
資料結構(單向鍊錶)
ifndef linklist h define linklist h 鍊錶節點 template class linklistdata linklistdata linklistdata 獲取資料 t getdata public t data 資料 template class linklist...