鍊錶相對於順序表來說,插入和刪除更加方便,然而想要查詢乙個元素時卻沒有順序表方便。
我們需要實現以下介面:
#ifndef __linklist_h__
#define __linklist_h__
#include #include #include #include typedef int datatype;
typedef struct node
node, *pnode, list, *plist;
void initlinklist(plist* pplist);
pnode buynode(datatype d);
void destroylinklist(plist* pplist);
void pushback(plist* pplist, datatype d);
void popback(plist* pplist);
void pushfront(plist* pplist, datatype d);
void popfront(plist* pplist);
pnode find(plist plist, datatype d);
void insert(plist* pplist, pnode pos, datatype d);//在指定位置之前插入乙個值
void erase(plist* pplist, pnode pos);//指定位置刪除
void remove(plist* pplist, datatype d);
void removeall(plist* pplist, datatype d);
void removep(plist* pplist, datatype d);
void erasenottailnode(pnode pos);
void printlinklist(plist plist);
int getlistlength(plist plist);
void printtailtohead1(plist plist);//逆序列印單項鍊表
void printtailtohead2(plist plist)//逆序列印單項鍊表
#endif //__linklist_h__
實現主函式及測試介面:
#define _crt_secure_no_warnings 1
#include "list.h"
static pnode plist;
void testback()
void testfront()
void testfind()
insert(&plist, tmp, 3);//在指定位置之前插入乙個值
insert(&plist, tmp, 3);//在指定位置之前插入乙個值
erase(&plist, tmp);//指定位置刪除
printlinklist(plist);
//erasenottailnode(tmp);
printlinklist(plist);
}void testremove()
void testlength()
void testprinttailtohead()
int main()
實現子函式:
#define _crt_secure_no_warnings 1
#include "list.h"
void initlinklist(plist* pplist)
pnode buynode(datatype d)
newnode->data =d;
newnode->next = null;
return newnode;
}void destroylinklist(plist* pplist)
*pplist = null;
}void pushback(plist* pplist, datatype d)
else
cur->next = newnode; }}
void popback(plist* pplist)
else
cur->next = del->next;
free(del);
del = null; }}
void pushfront(plist* pplist, datatype d)
else }
void popfront(plist* pplist)
else }
pnode find(plist plist, datatype d)
else
cur = cur->next;
} return null; }}
void insert(plist* pplist, pnode pos, datatype d)//在指定位置之前插入乙個值
else
if (cur)
}}void erase(plist* pplist, pnode pos)//指定位置刪除
if ((*pplist)==pos)
else
if (cur)
}}void remove(plist* pplist, datatype d)
if ((*pplist)->data == d)
else
if (cur)
}}void removeall(plist* pplist, datatype d)
pnode pre = *pplist;
while (cur)
else if (cur->data == d)
pre = cur;
cur = cur->next; }}
void erasenottailnode(pnode pos)
void printlinklist(plist plist)
printf("null\n");
}int getlistlength(plist plist)
return count;
}void printtailtohead1(plist plist)//逆序列印單項鍊表
void printtailtohead2(plist plist)//逆序列印單項鍊表
printf("%d ", cur->data);
tail = cur;
cur = plist;
}}
無頭節點的單鏈表就完成了! C語言實現無頭節點的單鏈表
測試 singlelinkedlist.h標頭檔案 ifndef singlelinkedlist h include include include typedef int datatype typedef struct listnode listnode listnode initlist da...
單鏈表 無頭節點
就這書上 敲了一邊,加深印象,沒有頭結點的時候插入第乙個就有所不同了,而刪除時要找到前乙個,注意current link null 就這樣。include include include using namespace std struct linknode class list bool list...
單鏈表操作 無頭節點
ifndef singlelist h define singlelist h 這是沒有頭結點的版本檔案 有頭結點的版本更簡單些,操作統一 include include include include typedef int elemtype typedef struct nodenode,lin...