單迴圈鍊錶

2021-07-29 05:22:19 字數 4503 閱讀 3654

/*****************************************

頭插 尾插 顯示 頭刪 尾刪

按值插入 按位置插入 查詢 長度

逆序 清除 摧毀 初始化 排序

按位置刪除 按值刪除

****************************************/

//**可以進一步優化

#ifndef sclist_h_

#define sclist_h_

#include typedef int elementtype;

typedef enum

bool_;

//鍊錶的各節點型別

typedef struct node

node, *pnode;

//鍊錶的管理結構

typedef struct list

list, *plist;

bool_ init(list *mylist);

bool_ push_front(list *mylist, elementtype e);

bool_ push_back(list *mylist, elementtype e);

void show(list mylist);

bool_ pop_front(list* mylist, elementtype *e);

bool_ pop_back(list* mylist, elementtype *e);

bool_ insert_val(list *mylist, elementtype e);

bool_ insert_pos(list *mylist, int i, elementtype e);

node *find(list mylist, elementtype e, bool_ precursor);

int length(list mylist);

void reverse(list *mylist);

void clear(list *mylist);

void destroy(list *mylist);

void sort(list *mylist);

bool_ delete_pos(list *mylist, int i, elementtype *e);

bool_ delete_val(list *mylist, elementtype e);

#endif //sclist_h_

#include "sclist.h"

#include "stdlib.h"

/*******************

與單鏈表無太大差異

只是判斷條件由 != null 變為了 != 頭結點

值得注意的是,迴圈鍊錶的頭結點千萬不能搞丟了,即使是指向頭結點的指標不能隨意亂指

********************/

bool_ init(list *mylist)

bool_ push_front(list *mylist, elementtype e)

p->next = mylist->first->next;

mylist->first->next = p;

mylist->size++;

return true_;

}bool_ push_back(list *mylist, elementtype e)

void show(list mylist)

printf("head\n");

printf("尾節點的值為%d\n", mylist.last->data);

printf("尾節點的後繼結點 %s 頭結點\n", mylist.last->next == mylist.first ? "是" : "不是");

printf("頭結點的值為%d\n", mylist.last->next->data);

}bool_ pop_front(list* mylist, elementtype *e)

node *p = mylist->first->next;

if( p == mylist->last )

mylist->last = mylist->first; //頭結點自迴圈

mylist->first->next = p->next;

*e = p->data;

free(p);

mylist->size--;

return true_;

}int length(list mylist)

bool_ pop_back(list* mylist, elementtype *e)

node *p = mylist->first;

while(p->next != mylist->last)

p = p->next;

mylist->last = p; //尾指標

*e = p->next->data;

free(p->next);

p->next = mylist->first;

mylist->size--;

return true_;

}bool_ insert_val(list *mylist, elementtype e) //此函式的先決條件:鍊錶中已存在節點的資料域值必須有序

else

return true_;

}//頭結點為位置0

bool_ insert_pos(list *mylist, int i, elementtype e)

if( i > mylist->size + 1 )

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

if( null == s )

return false_;

s->data = e;

node *p = mylist->first;

for(int j = 1; j < i; j++)

p = p->next;

s->next = p->next;

p->next = s;

mylist->size++;

return true_;

}node *find(list mylist, elementtype e, bool_ precursor)

//還是跟之前一樣的做法,將鍊錶分為兩個小煉表,鍊錶1包含頭結點的節點1,鍊錶2為剩下的節點,然後將鍊錶2的每個節點頭插入鍊錶1

void reverse(list *mylist)

}void clear(list *mylist)

mylist->last = mylist->first; //處理尾指標

mylist->last->next = mylist->first; //自迴圈

mylist->size = 0;

}void destroy(list *mylist)

void sort(list *mylist)

mylist->last->next = mylist->first; //迴圈

}bool_ delete_val(list *mylist, elementtype e)

node *q = p->next; //目標節點

p->next = q->next;

if( mylist->last = q ) //更改尾指標

mylist->last = p;

free(q);

mylist->size--;

return true_;

}bool_ delete_pos(list *mylist, int i, elementtype *e)

if( i < 1 || i > mylist->size )

node *p = mylist->first;

node *q = null;

for(int j = 1; j < i; j++)

q = p->next;

*e = q->data;

p->next = q->next;

if( q->next == mylist->first )

free(q);

mylist->size--;

return true_;

}

/*****************************************

頭插 尾插 顯示 頭刪 尾刪

按值插入 查詢 長度 按值刪除 排序

逆序 清除 摧毀 初始化 按位置插入

按位置刪除

****************************************/

#include "sclist.h"

int main(int argc, char**argv)

}exit:

return 1;

}

單迴圈鍊錶

乙個遊戲,數到第n人出列 include include include struct people struct people creat struct people head,int n else p struct people malloc sizeof struct people tail ...

拆分單迴圈鍊錶

設單迴圈鍊錶l1,對其遍歷的結果是x1,x2,xn。將該迴圈鍊錶拆分成兩個單迴圈鍊錶l1和l2,使得l1中含有原l1表中序號為奇數的節點,l2中含有原l1表中序號為偶數的節點 include includeusing namespace std template struct node templa...

單迴圈鍊錶 合併

include include define len sizeof struct node typedef int datatype typedef struct node linklist 用尾指標表示帶頭結點的單迴圈鍊錶的建立 linklist hcirl creat rear next hea...