線性鍊錶基本操作實現(C C )

2021-05-28 17:00:42 字數 2644 閱讀 8529

//函式結果狀態

#define true 1

#define false 0

#define ok 1

#define error 0

#define infeasible -1

#define null 0

#define overflow -2

#include

#include

#include

using namespace std;

typedef int status;//staus是函式的型別,其值是函式結果

typedef int datetype; //datetype可以使相應任何的資料型別如int、float、char

//結點型別定義

typedef struct lnode

lnode;

typedef lnode *linklist;

status initlist_l(linklist &l)//初始化單鏈表

status displist_l(linklist &l)//輸出單鏈表

return ok; }

status createlisttou(linklist &l,int n)//頭插法建立長度為n的單鏈表,帶頭結點

return ok; }

status listlength_l(linklist l) //求單鏈表的長度

return(n);

}// listlength_l

status listempty_l(linklist l) //判斷單鏈表是否為空,若為空返回true,否則返回false

status destroylist_l(linklist &l) //銷毀乙個單鏈表

free(p);

return ok; }

status getelem_l(linklist l, int i, datetype &e) //用e返回l中第i個資料元素的值

if(!p || j > i)

return error;//第i個元素不存在

e = p->date;//取第i個元素

return ok; }

status locateelem_l(linklist l,datetype e)//在鍊錶中按元素值查詢,返回該元素在鍊錶中的位序

if(p)

printf("查詢成功!是第%d個元素。\n",i);

return  ok; }

status listinsert_l(linklist &l,int i,datetype e)//在帶頭結點的單鏈表l中第i個位置之前插入元素e

if(p == null)//i小於1或者大於表長加1,不能插入

else

}//時間複雜度為o(n)

status listdelete_l(linklist &l,int i,datetype &e)//在帶頭結點的單鏈表l中,刪除第i個元素並返回其值

if(!(p->next) || j > i - 1)//刪除位置不合理

else

}//時間複雜度為o(n)

status inversionlist_l(linklist &l)//將乙個鍊錶逆置

return ok; }

status maxandminlist_l(linklist l,datetype &e1,datetype &e2)//找最大值最小值並返回其值

e1 = max;

e2 = min;

printf("最大值為:%d\n",e1);

printf("最小值為:%d\n",e2);

return ok; }

status listmerge_l(linklist &la,linklist &lb,linklist &lc)

else

} while(pa)

while(pb)

return ok; }

else

p = p->next; }

cout << "after delete:" << endl;

displist_l(l);

return ok; }

void output()//邊框

void mainpp()//顯示視窗

int main()

,x,y,z,e1,e2,mink,maxk;

linklist l;

initlist_l(l);//初始化單鏈表

mainpp();

while(k) 

case 2:

case 3:

case 4:

case 5:

case 6:

case 7:

case 8:

case 9:

case 10:

case 11:

case 12:

case 13:

default :exit(0); }

printf("繼續執行嗎?y(1)/n(0):");

scanf("%d",&k);

if(k == 0)

exit(0);  }

return 0; }

C C 實現單線鍊錶及基本操作

class cmylist node m pfirst 鏈首結點指標 int m icount cmylist cmylist void cmylist cmylist void 每次都在鍊錶頭部加入元素 void cmylist add int ia 刪除鍊錶中的乙個元素 void cmylist...

C C 實現雙向鍊錶及基本操作

接下來,我要用一段c c 實現雙向鍊錶的構建,插入,刪除操作。這是鍊錶操作中比較基本的操作,我是模仿單向鍊錶來進行雙向鍊錶的構建,其插入和刪除操作也和單向鍊錶大同小異,並不晦澀難懂,適合新手研讀,有錯誤或者不夠完善的希望大家指出。include pch.h include include inclu...

線性表的基本操作實現 基於鍊錶

用c 鍊錶方式實現了線性表的一些基本操作,包括插入元素,刪除元素,反轉線性表等.include include define error null typedef int elementtype typedef struct lnode ptrtolnode struct lnode typedef...