單鏈表是一種鏈式訪問的資料結構,用一組位址任意的儲存單元存放線性表中的資料元素.
以結點來表示,每個結點是由元素 + 指標組成.
以結點的序列表示線性表稱作線性鍊錶(單鏈表),單鏈表是鏈式訪問的結構.
單鏈表設頭指標head指向開始結點
/**
建立乙個單鏈表
*/typedef struct _sample_list
sample_list;
/** 初始化乙個頭結點
@return 返回乙個鍊錶
*/sample_list *create_link()
head->data = 0;
head->next = null;
return head;
}
/**
順序插入乙個節點
@param head 頭結點
@param value 插入值
@return 返回乙個新鍊錶
*/int inser_link(sample_list *head, int value)
new->data = value;
while ((current->next != null) && (value > current->next->data))
if (current->next == null) else
return 0;
}/**
單鏈表的列印
@param head 表頭
*/void sample_list_display(sample_list *head)
}int main(int argc, char * ar**) ,c = ,b= , a = ;
inser_link(&a, 20);
inser_link(&a, 2000);
sample_list_display(&a);
return 0;
}
1
1020
1001000
2000
這裡的順序插入,一種是在最後插入,一種是在中間插入,在中間插入有點繞.我們用乙個來說明
刪除已有的節點
@param head 頭節點
@param value 刪除值
@return 返回新鍊錶
*/int delete_note(sample_list *head, int value)
if (current == null) else
return -1;
}else
return 0;
}有關於刪除鍊錶需要替換掉對應指標
查詢任意乙個節點
@param head 頭節點
@param key 查詢值
@return 返回 -1 失敗 0 成功
*/int search_link(sample_list *head, int key)
if (current == null) else
return -1;
}else
return 0;
}
/**
鍊錶逆序(逆置)
@param head 頭節點
@return 返回新鍊錶
*/sample_list *reverse_link(sample_list *head)
current = head->next;
head->next = null;
while (current != null)
return head;
}/**
遍歷鍊錶各個節點
@param head 頭節點
*/void print_link(sample_list *head)
}/**
獲取鍊錶長度,多少個節點
@param head 頭節點
@return 返回長度
*/int length_link(sample_list *head)
return count;
}/**
釋放鍊錶
@param head 頭節點
*/void free_link(sample_list *head)
free(head);
printf("free success");
exit(0);
}
參考: 資料結構單鏈表
初學資料結構,貼段自己編寫的單鏈表程式,希望自己能夠一直以強大的學習熱情持續下去!自勉!2012年3月30日 於大連 include using namespace std typedef struct node linklist,node linklist makelist int n void ...
資料結構 單鏈表
今天浪費了好多時間,也許是心裡想著明天的考試吧 可自己也知道這次的考試,自己畢竟過不了了,只好等到今年11月份,想想那時自己已經大三了 還有那麼多時間嗎!很懊惱今天不知怎麼回事,感嘆環境真的可以影響乙個人,真的可以 把今天的學習筆記寫下來,沒有進行好好的整理,這回單鏈表的功能較多,操作比較散,最後乙...
資料結構 單鏈表
實現乙個單鏈表 1 查詢 查詢第index個節點 查詢指定的元素 2 插入 將指定的元素插入到第index個節點上 3 刪除 將第index個節點刪除 規律 刪除和新增元素前務必儲存兩個元素的位址引用資訊 public class mylinkedlist 記錄鍊錶結構的頭結點位址引用 privat...