C 實現鍊錶基礎功能

2021-09-25 06:22:51 字數 1862 閱讀 7978

#include

#include

#include

#include

#define ok 1

#define error 0

#define true 1

#define false 0

using namespace std;

typedef int elmetype;

typedef int status;

//鍊錶的結構體

typedef struct node

node;

typedef struct node* linklist;

//初始化鍊錶

status initlist(linklist *l)

//頭插法建立鍊錶

status createlisthead(linklist *l, int len)

return ok;

}//尾插法建立鍊錶

status createlisttail(linklist *l, int len)

return ok;

}//刪除鍊錶

void deletelist(linklist *l)

(*l)->next = null;

}//列印出鍊錶內容

void printlinklist(linklist l)

cout << endl;

}//返回鍊錶的長度(不包括頭節點)

int linklistlength(linklist l)

return count;

}//判斷鍊錶是否為空

status isempty(linklist l)

//返回鍊錶中第乙個值為e的位置

int findelme(linklist l, elmetype e)

else

break;

}if(count < linklistlength(l))

return count + 1;

else

return -1;

}//獲得鍊錶中第i個元素的值

int getelme(linklist l, int i)

return p->data;

}//在第i個位置插入元素e

status insertlinklist(linklist *l, int i, elmetype e)

while(j < i - 2)

q = (linklist)malloc(sizeof(node));

q->data = e;

q->next = p->next;

p->next = q;

return ok;

}//刪除第i個位置的元素

int deleteelme(linklist *l, int i)

while(j < i - 2)

q = p->next;

e = q->data;

p->next = p->next->next;

delete(q);

return e;

}int main()

else

cout << "請輸入正確的索引!";

deletelist(&l);

i = isempty(l);

if(i == 0)

cout << "鍊錶不為空\n";

else

cout << "鍊錶為空\n";

cout << "尾插法建立鍊錶的長度:";

cin >> j;

createlisttail(&l, j);

printlinklist(l);

return 0;

}

C 鍊錶簡單功能實現

實現的是乙個小型圖書館的程式,功能包括增加新書,以及讀者借書和還書等。如下 include include include include include using namespace std class patron class book bool operator const book bk ...

C語言基礎 實現單向鍊錶

回歸c基礎 實現乙個單向鍊錶,並有逆序功能 大學資料結構經常是這麼入門的 定義單鏈表結構體 typedef struct nodenode 建立鍊錶 node createnode int value,node next 列印鍊錶 void printlist node list 反轉鍊錶 node...

鍊錶基礎 c

鍊錶有動態和靜態兩種 1.動態 動態鍊錶可以帶頭結點 head 也可以不帶,動態的寫法要先申請空間 申請空間有2種寫法 1 malloc 標頭檔案stdlib.h int p int malloc sizeof int node p node malloc sizeof node 因為malloc返...