單鏈表頭插法尾插法

2021-08-22 10:27:42 字數 1305 閱讀 5378

標頭檔案如下:

#ifndef _linklist_h

#define _linklist_h

#define success 10000

#define failure 10001

#define size 10

typedef int element;

struct node

;typedef struct node sn;

#endif

功能函式如下:

#include #include "linklist2.h"

int linkinit(sn **p)

*p = (sn *)malloc(sizeof(sn));

if(*p == null)

(*p)->next = null;

return success;

}int eleminsert_tou(sn *p, element e)

sn *q = (sn *)malloc(sizeof(sn));

q->s = e;

q->next = p->next;

p->next = q;

return success;

}int eleminsert_wei(sn *p, element e)

sn *q = (sn *)malloc(sizeof(sn));

while(p->next != null)

q->s = e;

q->next = p->next;

p->next = q;

return success;

}int linkcheck(sn *head, void (*p)(element))

sn *q = head->next;

while(q)

return success;

}

主函式如下:只需將主函式中for迴圈中的函式呼叫名字更改就可呼叫頭插法或尾插法函式並實現功能。

#include #include "linklist2.h"

void print(element e)

int main()

else

int i;

for(i = 0; i < size; i++)

else

}ret = linkcheck(head, print);

if(ret == failure)

else

return 0;

}

建立單鏈表(頭插法 尾插法)

1 頭插法建立單鏈表 生成的鍊錶中結點的順序與輸入的順序相反 頭插法建立單鏈表 status createlist l linklist l,int n return ok 2 尾插法建立單鏈表 與輸入的順序相同 尾插法建立單鏈表 status createlist tail linklist l,...

C C 鏈 表 頭插法 尾插法

include stdafx.h include iostream using namespace std struct node node phead null 一開始沒有節點 先設定乙個空節點作為頭節點 此頭節點 資料data null,next null void addhead int d ...

建立單鏈表 頭插法與尾插法

建立單鏈表有兩種方式 頭插法與尾插法 尾插法是在鍊錶的結尾增加新的節點 頭插法是插入頭節點後面,剩餘節點前面 步驟需要新建乙個尾節點tail,初始head tail 建立新的節點new,連線到尾節點,tail.next new 尾節點後移,tail new q 為什麼要加新的節點?a 因為head的...