無頭結點鍊錶

2021-10-03 01:27:50 字數 3447 閱讀 9339

#include

#include"linklist.h"

struct node* create_linklist()//creat_linklist:根據使用者輸入,建立乙個單鏈表

struct node* pnew=(struct node* )malloc(sizeof(struct node));

pnew->data=num;

pnew->next=null;

if (firstnull)

#if 1 //尾插法

#else //頭插法

#endif

}

return first;//返回鍊錶首位址

/*

creat_linklist:根據使用者輸入,建立乙個有序鍊錶

返回值:返回首位址

/struct node creat_sort_linklist()

struct node*pnew=malloc(sizeof(struct node));

pnew->data=num;

pnew->next=null;

if (firstnull)

else

pre=p;

p=p->next;

}//再插入

if §

else

}else//沒有找到}}

return first;}/*

insert_node:在乙個鍊錶中,在值為x的節點前面新增乙個值為y的節點。

多個值為x的節點,則在第乙個值為x的節點前面加

如果沒有值為x的節點,則新增在最後

@first :要插入的鍊錶的首位址

@x:要查詢的節點的值

@y: 要插入的節點的值

返回值:

返回值插入後新鍊錶的首位址

struct nodeinsert_node(struct node first, elemtype x, elemtype y)

//2.查詢值為x所在的節點的位置

//兩種方式出迴圈,(1)break,(2)尾結點=null,p=null;

while (p!=null)

pre=p;

p=p->next;

}//3.插入

if §//p!=null

else

}else//沒有找到,插在最後

return first;
}

/*insert_node:在乙個鍊錶中,在值為x的節點前面新增乙個值為y的節點。

多個值為x的節點,則在第乙個值為x的節點前面加

如果沒有值為x的節點,則新增在最後

@first :要插入的鍊錶的首位址

@x:要查詢的節點的值

@y: 要插入的節點的值

返回值:

返回值插入後新鍊錶的首位址

/

struct nodeinsert_node_1(struct node first, elemtype x, elemtype y)

while §

else

break;//?

}else

} if (p==null)

return first;/*

delete_x_node:在乙個鍊錶中,找到值為x的節點,將其刪除,如果有多個值為x的節點,則刪除值為x

的第乙個節點。如果沒有值為x的節點,則不刪除。

@first:要刪除的鍊錶的首位址

@x:要刪除的節點的值

返回值:刪除節點後新鍊錶的首位址(首節點的位址)

建議:先給要刪除節點的前面節點找好下家,最後再把要刪除結點的關係斷掉

/struct nodedelete_x_node(struct nodefirst,elemtype x)

pre=p;

p=p->next;

}//printf("__%d__,__%s__\n",__line__,__function__);

if (p)

else

free(p);

}//printf("__%d__,__%s__\n",__line__,__function__);

return first;}

/*delete_all_x_node:在乙個鍊錶中,找到值為x的節點,將其刪除。如果有多個值為x的節點,都刪除。如果沒有值為x的節點,則不刪 除。

@first :要刪除節點的鍊錶的首位址

@x :要刪除的節點的值

返回值:刪除節點後新鍊錶的首位址

///可以用遞迴

struct nodedelete_all_x_node(struct nodefirst,elemtype x)

else

}

pre=p;

p=p->next;

} return first;

/*

get_node_num:計算鍊錶的節點數

返回值:節點數

/int get_node_num(struct nodefirst)

return num;}/

(1)倒序變順序:

求得整個鍊錶的總數,減去k,這個就是遍歷指標要移動的次數

struct node*get_k_node(struct node*first,int k)

struct node*p = first;

while(n)//3

return p;

}

(2)哨兵法:q是哨兵,p是遍歷指標,讓哨兵比遍歷指標先行k步,

然後兩個指標同時移動,迴圈到哨兵值為null時,

遍歷指標所指的位置就是倒數第k個節點的位址

哨兵法:更高效,只需要一次遍歷,但是要保證節點個數要大於k.

/struct nodeget_p_node(struct nodefirst,int k)

while (q)

return p;

void print_linklist(struct node* first)

printf(」\n");}/*

銷毀鍊錶:在某些情況,在建立完鍊錶,使用鍊錶的資料之後不再需要,

為節省空間,要銷毀鍊錶

@first:要銷毀的鍊錶

返回值:返回銷毀成功或失敗 或則無返回值

/void destroy_linklist(struct nodefirst)//被釋放的空間,再操作會產生段錯誤

無頭結點的鍊錶實現

鍊錶的插入 任何位置 int link1insert node l,int p,int e else if null l p 1 p 1 else if p count 1 return failure else if p count 1 else if p count 1 p 1 n next q...

頭結點鍊錶練習

這是乙份關於頭結點鍊錶的問題 完成一些基本的煉表處理函式,包括插入,查詢,刪除,輸出等一些功能的實現 include include include linklist.h node creat list 尾插法 int insert last node head,linkdata data 頭插法 ...

無頭雙向鍊錶

class node public class mylinkedlist else 尾插法 public void addlast int data else private void checkindex int index private node searchindex int index r...