鍊錶的建立,插入,刪除,遍歷

2021-08-20 10:31:15 字數 1538 閱讀 9385

#includeusing namespace std;

#define ok 1

#define error 0

#define overflow -2

typedef int status; //status 是函式返回值型別,其值是函式結果狀態**。

typedef int elemtype; //elemtype 為可定義的資料型別,此設為int型別

typedef struct lnode

lnode,*linklist; //linklist為指向結構體lnode的指標型別

status initlist_l(linklist &l)

status getelem_l(linklist l,int i,elemtype &e)

if(!p || j>i) return error; //第i個元素不存在

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

return ok;

} //getelem_l

lnode *locateelem_l(linklist l,elemtype e) //locateelem_l

status listinsert_l(linklist &l,int i,elemtype &e) //尋找第i-1個結點

if(!p||j>i-1) return error; //i大於表長+1或者小於1

s=new lnode; //生成新結點s

s->data=e; //將結點s的資料域置為e

s->next=p->next; //將結點s插入l中

p->next=s;

return ok;

} //listinsert_l

status listdelete_l(linklist &l,int i,elemtype &e) //尋找第i-1個結點

if(!(p->next) || j>i-1) return error; //i大於表長+1或者小於1

q=p->next; //臨時儲存被刪結點的位址以備釋放

p->next=q->next; //改變刪除結點前驅結點的指標域

e=q->data; //儲存刪除結點的資料域

delete q; //釋放刪除結點的空間

return ok;

} //listdelete_l

void createlist_f(linklist &l,int n)

} //createlist_f

void createlist_l(linklist &l,int n)

} //createlist_l

int main()

cout

} }return 0;

}

單向鍊錶 建立 插入 刪除 遍歷

include include include using namespace std struct list create 新建鍊錶 struct list insert struct list head,struct list temp 插入 struct list deletes struct...

雙向迴圈鍊錶(建立 插入 刪除 遍歷)

author chen ming dong include include typedef struct list str int n str creat str head head prior p p next head return head 遍歷 void gothrough str head...

鍊錶建立 插入 刪除

這兩天,拼命理解鍊錶,儘管現在理解還是不夠,但終於把長久以來一直折磨我的鍊錶用c 打出來了。還是有點小小的成就感。以下是 包括鍊錶建立 頭插法和尾插法 插入乙個位置的鍊錶 刪除乙個位置的鍊錶以及整個鍊錶的刪除。define null 0 include using namespace std int...