本文只要實現單鏈表的初始化、插入(尾插、頭插、任意位置插入)、刪除(尾刪、頭刪、刪除指定元素)、查詢等。
定義單鏈表
typedef int datatype;
typedef struct linknode
linknode, *plinknode, *plist;
實現單鏈表的所有介面:
void initlinklist(plist* phead);//單鏈表的初始化
void destroy(plist *phead);//銷毀(動態開闢的記憶體釋放)
void pushback(plist* phead, datatype x);//尾插
void popback(plist* phead);//尾刪
void pushfront(plist* phead, datatype x);//頭插
void popfront(plist* phead);//頭刪
void printlist(plist list);//列印單鏈表的元素
int getlistlength(plist head);//獲取單鏈表的長度
plinknode find(plist head, datatype x);//查詢元素
void insert(plist *phead, plinknode pos, datatype x);//任意位置插入元素
void remove(plist *phead, datatype x);//刪除指定元素
void removeall(plist *phead, datatype x);//刪除單鏈表中所有指定元素
void erase(plist *phead, plinknode pos);//按位置刪除
實現介面的函式:
void initlinklist(plist* phead)//單鏈表的初始化
void destroy(plist *phead)//銷毀(動態開闢的記憶體釋放)
}plinknode buynode(datatype x)//建立新節點
void pushback(plist* phead, datatype x)
else
cur->next = newnode; }}
void printlist(plist list)
printf("over\n");
}void popback(plist* phead)
else if (cur->next == null)
else
del = cur->next;
cur->next = null;
free(del);
del = null; }}
void pushfront(plist* phead, datatype x)
else }
void popfront(plist* phead)
else }
int getlistlength(plist head)
return count;
}plinknode find(plist head, datatype x)
return null;
}void insert(plist *phead, plinknode pos, datatype x)
else
newnode->next =cur->next;
cur->next = newnode; }}
void remove(plist *phead, datatype x)
else
free(del);
del = null;
break;
} prev = cur;
cur = cur->next; }}
void erase(plist *phead, plinknode pos)
else
free(del);
del = null;
break;
} prev = cur;
cur = cur->next; }}
void removeall(plist *phead, datatype x)
else
free(del);
del = null;
} else
}}
資料結構 線性表 單鏈表
資料結構 線性表的鏈式表示 單鏈表 線性表元素序號從1算起 date 2017 4 13 include include define initsize 100 define elemtype char typedef struct lnodelnode,linklist linklist crea...
資料結構 線性表 單鏈表
include include 結構體的定義和數序表的定義 typedef int elemtype typedef struct node node 函式的宣告 void initnode node h int addnode node h,elemtype e void deletenode n...
資料結構 線性表 單鏈表Java
建立自定義的類和構造方法來實現簡單的單鏈表。將結點結構定義為私有內部類,在外部類中對鍊錶結構進行初始化,包括頭結點和初始大小。單鏈表操作原理不難,難點在於對鍊錶進行插入和刪除操作時,對於指標交換和分配的邏輯。插入 找到要插入的位置 i 後,用新結點的後繼指標替換 i 的後繼指標,再將 i 的後繼指標...