c語言實現(xiàn)單鏈表算法示例分享
來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次
復(fù)制代碼 代碼如下:
#include <stdio.h>
#include <stdlib.h>
typedef char DataType;
typedef struct Node{
DataType data;
struct Node * Next;
}ListNode,* LinkList;
void Judement(LinkList head){ //判斷分配內(nèi)存
if (!head){
printf("Overflow.");
exit(-1);
}
}
LinkList CreatListF(void){ //頭插法創(chuàng)建Single Linked List
DataType ch;
LinkList head = (ListNode*)malloc(sizeof(ListNode));
Judement(head);
ListNode* s;
ch = getchar();
while (ch != '\n'){
s = (ListNode*)malloc(sizeof(ListNode));
Judement(s);
s->data = ch;
s->Next = head->Next;
head->Next = s;
ch = getchar();
}
return head;
}
LinkList CreatListS(void){ //尾插法創(chuàng)建Single Linked List
char ch;
ListNode* s;
LinkList head = (ListNode*)malloc(sizeof(ListNode));
Judement(head);
ch = getchar();
while (ch != '\n'){
s = (ListNode*)malloc(sizeof(ListNode));
Judement(s);
s->data = ch;
head->Next = s;
head = s;
ch = getchar();
}
head->Next = NULL;
return head;
}
int GetLength(LinkList head){ //獲取長度
int length = 0;
LinkList p = head->Next;
while (p){
length += 1;
p = p->Next;
}
return length;
}
ListNode* GetNodeById(LinkList head, int i){ //依序號查找元素
if (i<1 || i>GetLength(head)){
exit(1);
}
int j=1; //防止極端情況掃描逾界
LinkList p = head->Next;
while (p != NULL && j < i){
j += 1;
p = p->Next;
}
return p;
}
ListNode* GetNodeByValue(LinkList head, DataType e){ //依值查找元素
LinkList p = head->Next;
while (p != NULL&&p->data != e){
p = p->Next;
}
return p;
}
int InsertList(LinkList head, DataType e, int i){ //插入e值在第i節(jié)點
if (i<1 || i>GetLength(head) + 1){
exit(1);
}
LinkList s = (ListNode*)malloc(sizeof(ListNode));
s->data = e;
LinkList q, p = head;
int j = 1;
while (j <= i){
q = p;
p = p->Next;
j += 1;
}
s->Next = q->Next;
q->Next = s;
return 0;
}
int DeleteListNodeById(LinkList head, int i){ //依序號刪除節(jié)點
int j = 1;
ListNode* p,* q;
if (i<1 || i>GetLength(head)){
exit(1);
}
p = head;
while (j < i){
p = p->Next;
j += 1;
}
q = p->Next;
p->Next = q->Next;
free(q);
return 0;
}
int DeleteListRepeatNode(ListNode* head){ //清除冗余數(shù)據(jù)
ListNode* p, *q, *s;
if (p == NULL){
exit(1);
}
p = head->Next; //首節(jié)點無數(shù)據(jù)
while (p->Next != NULL){
q = p;
while (q->Next != NULL){
if (q->Next->data == p->data){
s = q->Next;
q->Next = q->Next->Next;
free(s);
}
q = q->Next;
}
p = p->Next;
}
return 0;
}
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05織夢dedecms什么時候用欄目交叉功能?