C語言單鏈表的建立、插入、查詢、刪除、求長、排序、遍歷
1.定義連結串列節點
typedef struct Node
{
int data;
struct Node *pNext;
}NODE, *PNODE;
2.連結串列的建立
PNODE createList()
{
PNODE pHead = (PNODE)malloc(sizeof(NODE));
if (pHead == NULL)
{
printf("memory allocation failure!");
exit(-1);
}
pHead->pNext = NULL;
return pHead;
}
3.連結串列的插入-頭插法
void instertNodeBehindTheHead(PNODE pHead, int data) { PNODE newNode = (PNODE)malloc(sizeof(NODE)); if (newNode == NULL) { printf("memory allocation failure!"); exit(-1); } newNode->data = data; newNode->pNext = pHead->pNext; pHead->pNext = newNode; }
4. 連結串列的查詢
PNODE findNode(PNODE pHead, int dfind)
{
pHead = pHead->pNext;
while (pHead)
{
if (pHead->data == dfind)
{
break;
}
pHead = pHead->pNext;
}
return pHead;
}
5.連結串列的刪除
void deleteNode(PNODE pHead, PNODE pDelete) { while (pHead->pNext != pDelete) { pHead = pHead->pNext; } pHead->pNext = pDelete->pNext; free(pDelete); pDelete = NULL; }
6.連結串列的求長
int lengthOfList(PNODE pHead)
{
pHead = pHead->pNext;
int len = 0;
while (pHead)
{
len++;
pHead = pHead->pNext;
}
return len;
}
7.連結串列的排序-冒泡法,可以優化為不交換資料而交換指標
void popSortList(PNODE pHead) { int len = lengthOfList(pHead); PNODE t; for (int i = 0; i < len-1; i++) { t = pHead->pNext; for (int j = 0; j < len-1-i; j++) { if (t->data > t->pNext->data) { t->data ^= t->pNext->data; t->pNext->data ^= t->data; t->data ^= t->pNext->data; } t = t->pNext; } } }
8.連結串列的遍歷
void traverseList(PNODE pHead)
{
pHead = pHead->pNext;
while (pHead)
{
printf("%d ", pHead->data);
pHead = pHead->pNext;
}
printf("\n");
}
9. 主函式
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <ctime>
int main()
{
srand((unsigned int)time(NULL));
PNODE pHead = createList();
for (int i = 0; i < 10; i++)
{
int data = rand() % 255;
instertNodeBehindTheHead(pHead, data);
}
int len = lengthOfList(pHead);
printf("length of list = %d\n", len);
traverseList(pHead);
PNODE pFind = findNode(pHead, 109);
if (pFind == NULL)
{
printf("no find!\n");
}
else
{
printf("find!\n");
deleteNode(pHead, pFind);
printf("********************delete******************\n");
traverseList(pHead);
}
popSortList(pHead);
printf("********************after******************\n");
traverseList(pHead);
system("pause");
return 0;
}
10. 執行結果
相關推薦
C語言單鏈表的建立、插入、查詢、刪除、求長、排序、遍歷
1.定義連結串列節點 typedef struct Node { int data; struct Node *pNext; }NODE, *PNODE;2.連結串列的建立 PNODE crea
C語言單鏈表的3種排序演算法,插入排序,氣泡排序,選擇排序
//插入排序 stu *view_sort_math(stu *head) { struct student *first; struct student *t; struct student *p; struct student *q; fi
C++寫的帶有頭結點單鏈表建立,插入,刪除,顯示
#include<iostream> usingnamespacestd; structlink { chardata; structlink*next; }; link*head,*tail;//建立頭指標和尾指標 intcreat(); /******
c語言-單鏈表(一)
printf blog 定義 單鏈表 mage 操作 img 生成 return 定義節點: typedef struct Node { int data; Node* pNext; }NODE, *PNODE; 細節說明,PNode 就代表str
C語言——單鏈表——學生管理系統
鞏固了一下單鏈表的知識點,並運用單鏈表寫了個簡單的學生管理系統 實現功能:增、刪、改、查 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #includ
C語言單鏈表
學過線性表中的順序表的都知道,順序表裡的資料在實體記憶體上是相鄰的,所以當我們在順序表中想要訪問下一個元素時可以直接去訪問,就像陣列一樣。但是單鏈表卻不同,單鏈表的資料儲存的位置是動態分配的,也就是說單鏈表的儲存在實體記憶體上不是相鄰的,所以我們就只能通過指標這種方式來把單鏈表串起來,
C語言——單鏈表的增刪改查
我們需要先定義一下連結串列的結構 #ifndef LIST_H #define LIST_H #define L 50 //引入標頭檔案 #include<stdio.h> #include<string.h> #include<stdbool.h&
C語言單鏈表的基本操作總結
刷LeedCode時使用單鏈表頻出錯誤,於是花時間總結了一下單鏈表的基本操作,把所寫程式碼和詳細註釋貼在這裡,以備後用。 #include <stdio.h> #include <stdlib.h> /*定義結構體連結串列節點: 該結構體有兩個屬性,一個是int型
C語言單鏈表及基本操作的實現
資料結構中,單向連結串列(又名單鏈表、線性連結串列)是連結串列的一種,其特點是連結串列的連結方向是單向的,對連結串列的訪問要通過從頭部開始,依序往下讀取。 下面的程式碼是使用指標實現的
C語言單鏈表--增刪查
廢話不多說直接看程式碼內建詳細註釋 //結點結構 typedef struct node { int data;//結點資料域 struct node * next;//結點指標域(儲存著下一個結點所在的地址也就是位置,即指向下一個結點) }NODE,*
C語言單鏈表實現19個功能完全詳解
#include "stdafx.h" #include "stdio.h" #include <stdlib.h> #include "string.h" typedef int elemType ; /************************
C語言單鏈表(線性表的鏈式儲存)實現程式碼
linklist.h //標頭檔案:資料 及 函式的宣告#ifndef __LINKLIST_H__ #define __LINKLIST_H__ #include <stdio.h> #include <stdlib.h> typedef in
單鏈表的初始化,建立,插入,查詢,刪除
#include <stdio.h> #include <stdlib.h> typedef int ElemType; //定義結點型別 typedef struct Node {ElemType data; //單鏈表中的資料域 struct Node *next; //單鏈表
c語言單鏈表的基本操作(程式碼)
c語言實現單鏈表的基本操作:建立、列印、刪除、插入、逆序。僅供學習之用還需不斷完善,有待讀者自己研究 #include <stdio.h> #include <stdlib.h> typedef struct stu { int data; /
c語言單鏈表的基本操作
連結串列 是一種動態儲存方式,和順序表相比,連結串列物理儲存上是非連續的,而且採用動態記憶體開闢,其優點是方便插入,沒有上限的儲存,不需要記憶體空間的重組,能有效的分配和利用記憶體資源,不會造成空間浪費,缺點是排序很麻煩,查詢也很麻煩,而且需要多個指標操作,容
使用c語言單鏈表實現的一個集合,包含了交,差,並集運算
下面是set的標頭檔案原始碼,set.h// // Created by admin on 18/3/12. // #ifndef _SET_H #define _SET_H #include <stdio.h> typedef enum BOOL { false
C語言-單鏈表的基本操作-嚴蔚敏版的資料結構
以下部分是標頭檔案 #ifndef __LianBiao__ #define __LianBiao__ typedef int ElemType; typedef struct LNode { int data; struct LNode
單鏈表建立,插入,刪除
學程式設計的小菜鳥一枚,希望未來好好學習天天向上~ 歡迎各位大神前來指正~ 放上資料結構的初次作業^_^(其實。。只做了一部分(°-°)) 以下函式在VC6.0的環境中執行通過 //定義結構體 typedef struct link { int
第一篇部落格—c語言單鏈表的基本操作
c語言單鏈表的基本操作 對於c語言的初學者來說,連結串列是一塊大難點,其中用到結構體,指標的知識,這裡講的是最簡單的單鏈表,連結串列是一種動態儲存方式,和陣列相比,連結串列的優點是方便插入,沒有上限的儲存(相對來說),缺點也很明顯,排序很煩,查詢也很麻煩。
C語言線性表的順序儲存(一)各種新增刪除
順序儲存結構的處理 什麼是順序儲存結構? 順序儲存結構是儲存結構型別中的一種,該結構是把邏輯上相鄰的結點儲存在物理位置上相鄰的儲存單元中,結點之間的邏輯關係由儲存單元的鄰接關係來體現。檢視具體. 順序表的儲存示意圖 程式碼操作 初始化線性表 //初始化連結串