標頭檔案
#ifndef __linklist_h__
#define __linklist_h__
#include #include #include #include typedef int datatype;
typedef struct node
node,*pnode,*plist;
void initlinklist(plist* pplist);//初始化
void destroylist(plist* pplist);//銷毀單鏈表
void display(plist plist);//遍歷單鏈表
void pushback(plist* pplist, datatype x);//尾插
void popback(plist* pplist);//尾刪
void pushfront(plist* pplist, datatype x);//頭插
void popfront(plist* pplist);//頭刪
pnode find(plist plist, datatype x);//查詢元素的位置
void insert(plist* pplist, pnode pos, datatype x);//指定位置插入
void erase(plist* pplist, pnode pos);//指定位置刪除
void remove(plist* pplist, datatype x);//刪除指定的元素
void removeall(plist* pplist, datatype x);//刪除指定元素的所有
void sort(plist* pplist);//單向鍊錶的排序
#endif
程式實現檔案
#include "linklist.h"
void initlinklist(plist* pplist)
void destroylist(plist* pplist)
free(cur);
*pplist = null;
}void pushback(plist* pplist, datatype x)
while(cur->_next)
cur->_next = newnode;
}void popback(plist* pplist)
if(cur->_next == null)
while(cur->_next->_next)
free(cur->_next);
cur->_next = null;
}void pushfront(plist* pplist, datatype x)
else }
void popfront(plist* pplist)
if(cur->_next == null)
del = cur;
cur = cur->_next;
free(del);
*pplist = cur;
}pnode find(plist plist, datatype x)
pos = pos->_next;
} return null;
}void insert(plist* pplist, pnode pos, datatype x)
while(cur->_next != pos)
newnode->_next = pos;
cur->_next = newnode;
}void erase(plist* pplist, pnode pos)
if((pos == *pplist)&&pos->_next)
while((cur->_next != pos)&&cur)
cur->_next = pos->_next ;
free(pos);
}void remove(plist* pplist, datatype x)
void removeall(plist* pplist, datatype x)
else
cur = cur->_next;
free(del);
} else
}}void sort(plist* pplist)
} }}void display(plist plist)
printf("null\n");
}
測試檔案
#include "linklist.h"
void test()
void test1()
void test2()
void test3()
void test4()
int main()
所有程式實現圖
C 實現單鏈表 不含頭結點
vs2005執行通過,如有問題,請各位大牛指正。注意 單鏈表不含有頭結點 include using namespace std template 定義結點 struct node 定義鍊錶 templateclass linklist templatelinklist linklist 初始化時,...
單鏈表的增加 無頭結點
單鏈表的增加 無頭結點 本文展示單鏈表的增加 1.從頭部新增 2.從尾部新增 初始化單鏈表 typedef struct listnode listnode typedef listnode list 頭結點為空的初始化 param head void initlist list head 單鏈表插...
無頭單鏈表
鍊錶有單鏈表 雙鏈表和雙向迴圈鍊錶,每種鍊錶都有無頭和帶頭兩種,帶頭就是頭結點不存放資料元素 ifndef linklist h define linklist h include stdio.h include assert.h include string.h include malloc.h ...