C++ 單鏈表的基本操作(詳解)
鏈表一直是面試的高頻題,今天先總結(jié)一下單鏈表的使用,下節(jié)再總結(jié)雙向鏈表的。本文主要有單鏈表的創(chuàng)建、插入、刪除節(jié)點等。
1、概念
單鏈表是一種鏈式存取的數(shù)據(jù)結(jié)構(gòu),用一組地址任意的存儲單元存放線性表中的數(shù)據(jù)元素。
鏈表中的數(shù)據(jù)是以結(jié)點來表示的,每個結(jié)點的構(gòu)成:元素 + 指針,元素就是存儲數(shù)據(jù)的存儲單元,指針就是連接每個結(jié)點的地址數(shù)據(jù)。如下圖:
2、鏈表的基本操作
SingleList.cpp:
#include "stdafx.h" #include "SingleList.h" #include <cstdlib> #include <iostream> #include <string.h> #include <conio.h> #include <stdio.h> /*c++實現(xiàn)簡單的單鏈表操作*/ using namespace std; SingleList::SingleList() { int num; char name[128]; // 創(chuàng)建鏈表 node *stuList = CreatNode(); PrintList(stuList); // 插入節(jié)點 printf("\n請輸入要插入的學生學號和姓名,輸入0 0表示結(jié)束."); scanf_s("%d%s", &num, name, 100); stuList = InsertNode(stuList, num, name); PrintList(stuList); // 刪除節(jié)點 printf("\n請輸入要刪除的學生學號:"); scanf_s("%d", &num, 100); stuList = DeleteNode(stuList, num); PrintList(stuList); // 逆序 printf("\n逆序后的鏈表為:\n"); stuList = ReverseList(stuList); PrintList(stuList); system("PAUSE"); } SingleList::~SingleList() { } //建立單鏈表 node *SingleList::CreatNode() { node *head, *p, *s; int num = 0; char name[128]; int cycle = 1; head = (node *)malloc(sizeof(node)); // 為頭結(jié)點分配內(nèi)存空間 head->next = nullptr; p = head; // p指向頭節(jié)點 while (cycle) { printf("\n請輸入學生的學號和姓名:"); scanf_s("%d%s", &num, name, 100); if (num != 0) { s = (node *)malloc(sizeof(node)); s->num = num; memcpy(s->name, name, 128); printf("%d%s", s->num, s->name); p->next = s; // 指向新插入的節(jié)點 p = s; // p指向當前節(jié)點 } else { cycle = 0; } } head = head->next; p->next = NULL; printf("頭節(jié)點學生信息為: %d%s\n", head->num, head->name); return head; } //單鏈表插入 node *SingleList::InsertNode(node *head, int num, char* name) { node *s, *p1, *p2 = NULL; p1 = head; s = (node *)malloc(sizeof(node)); s->num = num; strcpy_s(s->name, name); while ((s->num > p1->num) && p1->next != NULL) { p2 = p1; p1 = p1->next; } if (s->num <= p1->num) { if (head == p1) { // 插入首節(jié)點 s->next = p1; head = s; } else { // 插入中間節(jié)點 p2->next = s; s->next = p1; } } else { // 插入尾節(jié)點 p1->next = s; s->next = NULL; } return head; } // 計算單鏈表長度 int SingleList::GetLength(node *head) { int length = 0; node *p; p = head; while (p != NULL) { p = p->next; length++; } return length; } //單鏈表刪除某個元素 node *SingleList::DeleteNode(node *head, int num) { node *p1, *p2 = nullptr; p1 = head; while (num != p1->num && p1->next != NULL) { p2 = p1; p1 = p1->next; } if (num == p1->num) { if (p1 == head) { head = p1->next; } else { p2->next = p1->next; } free(p1); } else { printf("找不到學號為%d 的學生!\n", num); } return head; } //單鏈表逆序 node *SingleList::ReverseList(node *head) { // A->B->C->D node *old_head; // 原來鏈表的頭 node *new_head; // 新鏈表的頭 node *cur_head; // 獲得原來鏈表的頭 if (head == NULL || head->next == NULL) return head; new_head = head; // A cur_head = head->next; // B while (cur_head) { old_head = cur_head->next; // 將原來鏈表的頭取出,并將第二個節(jié)點作為頭節(jié)點 cur_head->next = new_head; // 將取出的頭設為新鏈表的頭 new_head = cur_head; // 新鏈表的頭就是目前新鏈表的頭 cur_head = old_head; // 接著處理 } head->next = NULL; head = new_head; return head; } //打印單鏈表 void SingleList::PrintList(node *head) { node *p; int n; n = GetLength(head); printf("\n打印出 %d 個學生的信息:\n", n); p = head; while (p != NULL) { printf("學號: %d ,姓名: %s\n", p->num, p->name); p = p->next; } }
SingleList.h:
#pragma once typedef struct student { int num; // 學號 char name[128]; // 姓名 struct student *next; }node; class SingleList { public: SingleList(); ~SingleList(); //建立單鏈表 node *CreatNode(); //單鏈表插入 node *InsertNode(node *head, int num, char* name); // 計算單鏈表長度 int GetLength(node *head); //單鏈表刪除某個元素 node *DeleteNode(node *head, int num); //單鏈表逆序 node *ReverseList(node *head); //打印單鏈表 void PrintList(node *head); };
關(guān)于逆序邏輯,研究了一下:
1、主要思路:
假設有單鏈表A->B->C->D,首先取出首節(jié)點A作為新逆序出來的鏈表
這樣,原鏈表就為:B->C->D,逆序后的新鏈表為:A
2. 按照上述方法,依次取出B、C、D放入新鏈表
2、圖形表示:
原始的單鏈表:
<!--[endif]-->
初始狀態(tài)時,單鏈表如上圖所示,head指向頭節(jié)點A。
1. 取出原始鏈表的第一個節(jié)點A,然后將該節(jié)點作為新鏈表的頭節(jié)點
原始鏈表:
<!--[endif]-->
新鏈表:
<!--[if !vml]--> <!--[endif]-->
<!--[if !supportLists]--> 2.然后同上處理:
原始鏈表:
<!--[if !vml]--> <!--[endif]-->
新鏈表:
<!--[if !vml]--> <!--[endif]-->
以上這篇C++ 單鏈表的基本操作(詳解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持我們。
欄 目:C語言
下一篇:Linux中使用VS Code編譯調(diào)試C++項目詳解
本文標題:C++ 單鏈表的基本操作(詳解)
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1953.html
您可能感興趣的文章
- 04-02c語言的正則匹配函數(shù) c語言正則表達式函數(shù)庫
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10c語言求1+2+...+n的解決方法
- 01-10求子數(shù)組最大和的解決方法詳解
- 01-10深入理解約瑟夫環(huán)的數(shù)學優(yōu)化方法
- 01-10深入二叉樹兩個結(jié)點的最低共同父結(jié)點的詳解
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設計- 解析最少換車次數(shù)的問題詳解
- 01-10c語言 跳臺階問題的解決方法


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