大家好,我是【1+1=王】, 熱愛java的計算機(人工智能)渣碩研究生在讀,
如果你也對java、人工智能等技術感興趣,歡迎關注,抱團交流進大廠!!!
Good better best, never let it rest, until good is better, and better best.近期會把自己本科階段的一些課程設計、實驗報告等分享出來,供大家參考,希望對大家有幫助,
博客更新至專欄【課程設計實驗報告】:https://blog.csdn.net/weixin_43598687/category_11640051.html
一、 實驗目的
- 掌握線性表的鏈表表示;
- 實作單鏈表的插入操作
- 實作單鏈表的洗掉操作
- 實作雙向鏈表的插入操作
- 實作雙向鏈表的洗掉操作
二、 實驗內容
1. 實驗任務
a. 完成單鏈表的建立、插入和洗掉
b. 完成雙向鏈表的建立、插入和洗掉
2. 程式設計
1) 資料輸入(輸入哪些資料、個數、型別、來源、輸入方式)
節點個數:count;
節點元素值:temp;
要插入節點的位置和數值:num1、Data;
要洗掉節點的位置:num2;
2) 資料存盤(輸入資料在記憶體中的存盤)
動態分配記憶體(pNode pNew = (pNode)malloc(sizeof(Node));)
3) 資料處理(說明處理步驟,若不是非常簡單,需要繪制流程圖)




4) 資料輸出(貼圖:程式運行結果截圖,圖幅大小適當,不能太大)

三、 實驗環境
- 作業系統:WINDOWS 10
- 開發工具:VC++ 2013
- 實驗設備:PC
四、源代碼
1. 單鏈表的插入洗掉操作
- pNode CreatList(); //創建鏈表函式
- void TravelseList(pNode); //遍歷鏈表函式
- bool Insert_Node(pNode, int, int); //插入節點
- int Del_Node(pNode, int); //洗掉節點
#include<iostream>
using namespace std;
typedef struct node
{
int data;
struct node *pNext;
}Node, *pNode;
pNode CreatList(); //創建鏈表函式
void TravelseList(pNode); //遍歷鏈表函式
bool Insert_Node(pNode, int, int); //插入節點
int Del_Node(pNode, int); //洗掉節點
int main()
{
pNode pHead = NULL; //struct Node *pHead=NULL
int Data;
int num;
pHead = CreatList();
TravelseList(pHead);
cout << "輸入要插入的位置和資料:";
cin >> num >> Data;
Insert_Node(pHead, num, Data);
TravelseList(pHead);
cout << "輸入要洗掉的位置:";
cin >> num;
Del_Node(pHead, num);
TravelseList(pHead);
system("pause");
return 0;
}
//創建鏈表
pNode CreatList()
{
int count; //節點個數
int temp; //臨時存盤用戶輸入的節點的資料
pNode pHead = (pNode)malloc(sizeof(Node)); //不存放有效資料的頭結點
pNode pTail = pHead; //鏈表的最后一個節點
pTail->pNext = NULL; //最后一個節點指標置為空
cout << "輸入節點個數" << endl;
cin >> count;
for (int i = 0; i < count; i++)
{
cout << "輸入第" << i + 1 << "個節點的數值" << endl;
cin >> temp;
pNode pNew = (pNode)malloc(sizeof(Node)); //給新節點分配空間
pNew->data = temp;
pTail->pNext = pNew;
pNew->pNext = NULL;
pTail = pNew;
}
return pHead;
}
void TravelseList(pNode pHead)
{
cout << "鏈表的資料如下:";
pNode p = pHead->pNext;
while (p != NULL)
{
cout << p->data << " ";
p = p->pNext;
}
cout << endl;
return;
}
//插入
bool Insert_Node(pNode pHead, int front, int Data)
{
int i = 0;
pNode _node = pHead;
pNode pSwap; //交換指標
if ((front < 1) && (_node != NULL))
{
return false;
}
while (i < front - 1)
{
_node = _node->pNext;
++i;
}
pNode pNew = (pNode)malloc(sizeof(Node));
pNew->data = Data;
pSwap = _node->pNext;
pNew = _node->pNext;
pSwap = pNew->pNext;
return true;
}
//洗掉
int Del_Node(pNode pHead, int back)
{
int i = 0;
int Data;
pNode _node = pHead;
pNode pSwap;
if ((back < 1) && (NULL == _node->pNext))
{
printf("洗掉失敗!\n");
return 0;
}
while (i < back - 1)
{
_node = _node->pNext;
++i;
}
pSwap = _node->pNext;
Data = pSwap->data;
_node->pNext = _node->pNext->pNext;
free(pSwap);
return Data;
}
2. 雙向回圈鏈表的插入洗掉操作
頭檔案
#ifndef _DOUBLELINKLIST_H_
#define _DOUBLELINKLIST_H_
//
// 在此處包含 C 標準庫頭檔案
//
#include <stdio.h>
#include<malloc.h>
//
// 在此處包含其他頭檔案
//
//
// 在此處定義資料結構
//
typedef int ElemType; // 鏈表中元素的型別
typedef struct DuLNode {
ElemType data; // 資料域
struct DuLNode* prior; // 前趨指標
struct DuLNode* next; // 后繼指標
}DuLinkList;
//
// 在此處宣告函式
//
int InsertBefore(DuLinkList* pListHead, int i, ElemType Elem);
int Delete(DuLinkList* pListHead, int i, ElemType* pElem);
#endif /* _DOUBLELINKLIST_H_ */
cpp檔案
#include "DoubleLinkList.h"
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
int i;
ElemType Elem;
DuLinkList* pListHead; // 雙向回圈鏈表的表頭指標,指向表頭節點
DuLinkList* pListNode; // 雙向回圈鏈表節點指標
//
// 初始化雙向回圈鏈表的表頭節點
//
pListHead = (DuLinkList*)malloc(sizeof(DuLinkList));
pListHead->prior = pListHead;
pListHead->next = pListHead;
//
// 初始化雙向回圈鏈表的節點
//
for (i = 8; i>0; i--)
{
pListNode = (DuLinkList*)malloc(sizeof(DuLinkList));
pListNode->data = i;
pListNode->next = pListHead->next;
pListNode->prior = pListHead;
pListHead->next->prior = pListNode;
pListHead->next = pListNode;
}
//
// 在第 i 個節點之前插入一個節點
//
InsertBefore(pListHead, 3, 88);
InsertBefore(pListHead, 20, 15); // 插入位置非法,插入失敗,
//
// 洗掉第 i 個節點
//
Delete(pListHead, 3, &Elem);
Delete(pListHead, 20, &Elem); // 洗掉位置非法,洗掉失敗,
//
// 銷毀雙向回圈鏈表
//
while (pListHead->next != pListHead)
{
pListNode = pListHead->next;
pListHead->next = pListNode->next;
pListNode->next->prior = pListHead;
free(pListNode);
}
free(pListHead);
return 0;
}
/*
功能:
在第 i 個節點之前插入一個節點,
引數:
pListHead -- 雙向回圈鏈表的表頭指標
i -- 插入節點的位置,從 1 開始計數,
Elem -- 插入節點的值,
回傳值:
如果插入成功回傳 1
如果插入失敗回傳 0
*/
int InsertBefore(DuLinkList* pListHead, int i, ElemType Elem)
{
DuLinkList* pListNode=NULL; // 節點指標
//
// TODO: 在此添加代碼
//
if (i <= 0 && i > 8)
cout << "插入非法" << endl;
else
{
DuLinkList* s = (DuLinkList*)malloc(sizeof(DuLNode));
s->data = Elem;
s->prior = pListNode->prior;
pListNode->prior->next = s;
s->next = pListNode;
pListNode->prior = s;
}
return 0;
}
/*
功能:
洗掉第 i 個節點,
引數:
pListHead -- 雙向回圈鏈表的表頭指標
i -- 洗掉節點的位置,從 1 開始計數,
pElem -- 回傳被洗掉節點的值,
回傳值:
如果洗掉成功回傳 1
如果洗掉失敗回傳 0
*/
int Delete(DuLinkList* pListHead, int i, ElemType* pElem)
{
DuLinkList* pListNode; // 節點指標
//
// TODO: 在此添加代碼
//
if (i <= 0 && i > 8)
cout << "插入非法" << endl;
else
{
pElem = pListNode->data;
pListNode->prior->next = pListNode->next;
pListNode->next->prior = pListNode->prior;
free(pListNode);
}
return 0;
}
博客更新至專欄【課程設計實驗報告】:https://blog.csdn.net/weixin_43598687/category_11640051.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/435445.html
標籤:AI
