帶頭結點的單鏈表實現

2021-09-30 12:06:19 字數 1624 閱讀 3934

#includeusing namespace std;

typedef char datatype;

struct node

;//按序號查詢鍊錶

node * getnode(node * head, int i)

if(i > j)

return null;

else

return p; //說明沒找到第 i 個結點

}//按值查詢

node * locatenode(node * head, datatype key)

if(p->data == key)

return p;

else

return null;

}//刪除第 i 個結點的實現

int link_del(node * head, int i)

if((j < i) || (p->next == null))

return -2;

r = p->next;

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

free(r);

return 0;

} //按序號插入

int link_insert(node* head, int i, datatype x) //i>0,即i=1,2,3... 表示第i個位置

if((i<=0)||(j < i)) //插入位置 i 錯誤

return -2;

//cout << p->data << endl;

node * s = (node*)malloc(sizeof(node));

s->data = x;

s->next = p->next;

p->next = s;

return 0;

}//頭插法建表

node * creat_unbrainlink()

return head;

}//尾插法建表

node * creat_unbrainlink_end()

//if(p != null)

p->next = null;

//s->next = null; //這樣寫也可以

return head;

} // 建立大小為n的鍊錶

node * creat_link(int n)

p->next = null;

return head;

}//鍊錶顯示函式

int show(node * head)

cout << p->data << endl;

return 0;

}//刪除所有鍊錶,並釋放空間

int destroylist(node* &head)

temp = head;

//temp->data = 'h';

while(temp->next != null)

//cout << temp->data;

free(temp);

head = null;

return 0;

}int main()

單鏈表的實現 帶頭結點

linklist.cpp 定義控制台應用程式的入口點。include stdafx.h include using namespace std template struct node template class linklist template linklist linklist templa...

帶頭結點的單鏈表實現

帶頭結點的單鏈表實現 通過對結點的指標操作來實現各個核心功能.中要注重核心思想,但是必要的安全檢查還是必須的 後期可以省略,但是要有這個意識 include include define null null typedef int elemtype typedef struct lnode node...

帶頭結點的單鏈表

帶頭結點的單鏈表 1 頭結點 在棧區開闢,指標域指向第乙個首元結點,資料域不儲存資料,可以儲存當前結點的個數 2 普通結點 無論是頭結點還是普通結點都是乙個結構體型別,由指標域和資料域組成 指標域指向下乙個結點,儲存下乙個結點的位址 資料域可以設定成聯合體型別,成員由資料元素和結點個數組成,之所以將...