日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实验一 链式存储结构的基本操作

發(fā)布時(shí)間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实验一 链式存储结构的基本操作 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

廣州大學(xué)學(xué)生實(shí)驗(yàn)報(bào)告

開課實(shí)驗(yàn)室:計(jì)算機(jī)科學(xué)與工程實(shí)驗(yàn)(電子樓418A) 2019年4月27日
學(xué)院 計(jì)算機(jī)科學(xué)與教育軟件學(xué)院 年級(jí)、專業(yè)、班 計(jì)算機(jī)科學(xué)與技術(shù)172班 姓名 xxx 學(xué)號(hào) 170610xxxx
實(shí)驗(yàn)課程名稱 數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn) 成績
實(shí)驗(yàn)項(xiàng)目名稱 實(shí)驗(yàn)一 鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)的基本操作 指導(dǎo)老師 xxx
一、實(shí)驗(yàn)?zāi)康?br /> 掌握鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)的定義及基本操作
二、使用儀器、器材
微機(jī)一臺(tái)
操作系統(tǒng):Win10
編程軟件:C++
三、實(shí)驗(yàn)內(nèi)容及原理
線性表的鏈表實(shí)現(xiàn):遍歷、查找、插入、刪除、翻轉(zhuǎn)
棧的鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)實(shí)現(xiàn):入棧、出棧
隊(duì)列的鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)的實(shí)現(xiàn):入隊(duì)、出隊(duì)
棧和隊(duì)列的簡單應(yīng)用:編寫函數(shù),判斷給定的字符串是否回文。

四、實(shí)驗(yàn)過程原始數(shù)據(jù)記錄

1、線性表的鏈表實(shí)現(xiàn):
(1)用隨機(jī)函數(shù)生成10個(gè)3位整數(shù)(100~999),把這些整數(shù)存于鏈表中;
(2)輸出鏈表的內(nèi)容;
(3)讀入一個(gè)整數(shù),查看該整數(shù)是否在表中,若在,輸出其位置(首位置為1);
(4)讀入一個(gè)整數(shù),以及要插入的位置(如果選擇有序鏈表則不必指出插入位置),把該整數(shù)插入到鏈表中,輸出鏈表的內(nèi)容(要求判斷輸入的位置是否合理);
(5)讀入一個(gè)整數(shù),若該整數(shù)在鏈表里,刪除該整數(shù),輸出鏈表的內(nèi)容;
(6)把鏈表的內(nèi)容翻轉(zhuǎn),輸出鏈表的內(nèi)容。

所采取的方案:

  • 鏈?zhǔn)骄€性表帶表頭,非循環(huán)結(jié)構(gòu);
  • 鏈表內(nèi)容為無序;
  • 鏈表內(nèi)容(結(jié)點(diǎn)數(shù)值)可以重復(fù),也可以不重復(fù)
    Listmain.cpp
    #include “pch.h”
    #include
    #include"header.h"
    using namespace std;
  • int main()
    {

    LinkNode *Link; InitList(Link); time_t t; // 定義時(shí)間變量 srand((unsigned)time(&t)); //由時(shí)間確定隨機(jī)序列,執(zhí)行一次 int aa[10]; for (int i = 0; i <10; i++) {int a = rand()%900+100;aa[i]=a; } CreateListF(Link, aa, 10); cout << "數(shù)據(jù)插入成功"<<endl; cout<<"隨機(jī)生成的數(shù)組為:"<<endl; DispList(Link); bool flag; int i, e; cout << "如果要判斷一個(gè)數(shù)是否在鏈表中,請(qǐng)輸入任意非零數(shù)進(jìn)入判斷,否則跳過該判斷請(qǐng)輸入0:"; cin >> flag; while (flag) {cin.clear();cout<<"請(qǐng)輸入要查找的數(shù)據(jù):"<<endl;cin>>e;LocateElem(Link, i, e);if (i!=0&&i!=11){cout << "該查找數(shù)據(jù)的位置為:";cout << i;cout << endl;}else cout << "該查找數(shù)據(jù)不在生成數(shù)列里"<<endl;cout << "如果要判斷一個(gè)數(shù)是否在鏈表中,請(qǐng)輸入任意非零數(shù)進(jìn)入判斷,否則跳過該判斷請(qǐng)輸入0:";cin >> flag; } cout << "如果要插入一個(gè)數(shù)進(jìn)入鏈表中,請(qǐng)輸入任意非零數(shù)進(jìn)入插入,否則跳過該次插入請(qǐng)輸入0:"; cin >> flag; while (flag) {cin.clear();int ii, ee;cout<<"請(qǐng)輸入要插入數(shù)據(jù)的位置:" << endl;cin>>ii;cout<<"請(qǐng)輸入要插入數(shù)據(jù)的值:" << endl;cin>>ee;bool flag1=ListInsert(Link, ii, ee);if (flag1) {cout << "插入數(shù)據(jù)后的數(shù)組為:" << endl;DispList(Link);}else cout << "插入位置不正確" << endl;cout << "如果要插入一個(gè)數(shù)進(jìn)入鏈表中,請(qǐng)輸入任意非零數(shù)進(jìn)入插入,否則跳過該次插入請(qǐng)輸入0:";cin >> flag; } cout << "如果要?jiǎng)h除鏈表一個(gè)數(shù)據(jù),請(qǐng)輸入任意非零數(shù)進(jìn)入刪除,否則跳過該次刪除請(qǐng)輸入0:"; cin >> flag; while (flag) {cin.clear();int eee,iii;cout<<"請(qǐng)輸入要?jiǎng)h除的數(shù)據(jù)的值:" << endl;cin>>eee;LocateElem(Link, iii, eee);bool flag2= ListDelete(Link,iii, eee);if (flag2) {cout << "刪除成功!" << endl;cout << "刪除該數(shù)據(jù)后的數(shù)組顯示如下:" << endl;DispList(Link);cout << endl;}else cout << "要?jiǎng)h除的數(shù)據(jù)并不在數(shù)組內(nèi)"<<endl;cout << "如果要?jiǎng)h除鏈表一個(gè)數(shù)據(jù),請(qǐng)輸入任意非零數(shù)進(jìn)入刪除,否則跳過該次刪除請(qǐng)輸入0:" << endl;cin >> flag; } Rollback(Link); cout<<"翻轉(zhuǎn)后的鏈表內(nèi)容為:" << endl; DispList(Link); system("pause");

    }

    List.h
    #include <stdio.h>
    #include “time.h”
    #include “math.h”
    #include “stdlib.h”

    typedef struct LNode
    {
    int data;
    struct LNode *next;
    }LinkNode;
    void InitList(LinkNode *&L);
    void CreateListF(LinkNode *&L, int a[], int n);
    void DispList(LinkNode *L);
    int LocateElem(LinkNode *L, int & i, int e);
    bool ListInsert(LinkNode *&L, int i, int e);
    bool ListDelete(LinkNode *&L, int i, int &e);
    void Rollback(LinkNode *&L);

    List.cpp
    #include"pch.h"
    #include “header.h”
    void InitList(LinkNode *&L)
    {
    L = (LinkNode *)malloc(sizeof(LinkNode));
    L->next = NULL;
    }
    void CreateListF(LinkNode *& L, int a[], int n)
    {
    LinkNode * s;
    L = (LinkNode *)malloc(sizeof(LinkNode));
    L->next = NULL;
    for (int i = 0; i < n; i++)
    {
    s = (LinkNode *)malloc(sizeof(LinkNode));
    s->data = a[i];
    s->next = L->next;
    L->next = s;
    }
    }
    void DispList(LinkNode *L)
    {
    LinkNode *p = L->next;
    while (p != NULL)
    {
    printf("%d", p->data);
    printf("\n");
    p = p->next;
    }
    printf("\n");
    }
    int LocateElem(LinkNode *L, int & i, int e)
    {
    i = 1;
    LinkNode *p = L->next;
    while (p != NULL && p->data != e)
    {
    p = p->next;
    i++;
    }
    if (p == NULL)
    return(0);
    else
    return(i);

    }

    bool ListInsert(LinkNode *& L, int i, int e)
    {
    /if (i < 1 || i>10)
    {
    return false;
    }/
    int j = 0;
    LinkNode *p = L, *s;
    if (i <= 0) return false;
    while (j < i - 1 && p != NULL)
    {
    j++;
    p = p->next;
    }
    if (p == NULL)
    return false;
    else
    {
    s = (LinkNode *)malloc(sizeof(LinkNode));
    s->data = e;
    s->next = p->next;
    p->next = s;
    return true;
    }
    }

    bool ListDelete(LinkNode *& L,int i,int & e)
    {
    int j = 0;
    LinkNode *p = L, *q;
    while (p != NULL &&j<i-1)
    {
    j++;
    p = p->next;
    }
    if (pNULL)
    {
    return false;
    }
    else
    {
    q = p->next;
    if (qNULL)
    {
    return false;
    }
    e = q->data;
    p->next = q->next;
    free(q);
    return true;
    }
    }

    void Rollback(LinkNode &L)
    {
    LinkNode pre = NULL;
    LinkNode* pNext = NULL;
    LinkNode* pCur = L->next;
    while (pCur)
    {
    pNext = pCur->next;
    pCur->next = pre;
    pre = pCur;
    pCur = pNext;
    }
    L->next = pre;
    }

    2、棧的鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)實(shí)現(xiàn)
    (1)用隨機(jī)函數(shù)生成10個(gè)3位整數(shù)(100~999),把這些整數(shù)應(yīng)用入棧操作存于堆棧中,在入棧接口處設(shè)置斷點(diǎn)①,按“F5”啟動(dòng)調(diào)試,按“F10”逐句執(zhí)行,直到數(shù)據(jù)全部入棧。程序暫停時(shí)觀察棧頂數(shù)據(jù)和棧頂位置;
    (2)應(yīng)用出棧操作輸出堆棧的內(nèi)容,在出棧接口處設(shè)置斷點(diǎn)②,按“F5”啟動(dòng)調(diào)試,按“F10”逐句執(zhí)行,直到所有數(shù)據(jù)完全出棧,程序暫停時(shí)觀察棧頂數(shù)據(jù)和棧頂位置的變化;

    所采取的方案:
    4. 鏈?zhǔn)骄€性表帶表頭,非循環(huán)結(jié)構(gòu);
    5. 鏈表內(nèi)容為無序;
    6. 鏈表內(nèi)容(結(jié)點(diǎn)數(shù)值)可以重復(fù),也可以不重復(fù)

    Stackmain.cpp
    #include “pch.h”
    #include
    #include"header.h"

    int main()
    {
    LinkStNode *Stack;
    InitStack(Stack);
    time_t t; // 定義時(shí)間變量
    srand((unsigned)time(&t)); //由時(shí)間確定隨機(jī)序列,執(zhí)行一次
    for (int i = 0; i < 10; i++)
    {
    int a = rand()%900+100;
    Push(Stack, a);
    }
    for (int i = 0; i < 10; i++)
    {
    int aa;
    Pop(Stack,aa);
    printf("%d", aa);
    printf("\n");
    }
    system(“pause”);
    }

    Stack.h
    #include “malloc.h”
    #include"time.h"
    typedef struct linknode
    {
    int data;
    struct linknode * next;
    }LinkStNode;
    void InitStack(LinkStNode *&s);
    void DestroyStack(LinkStNode *&s);
    bool StackEmpty(LinkStNode *s);
    void Push(LinkStNode *&s, int e);
    bool Pop(LinkStNode *&s, int &e);
    bool GetTop(LinkStNode *s, int &e);

    stack.cpp
    #include"pch.h"
    #include “header.h”
    void InitStack(LinkStNode *& s)
    {
    s = (LinkStNode *)malloc(sizeof(LinkStNode));
    s->next = NULL;
    };

    void DestroyStack(LinkStNode *& s)
    {
    LinkStNode *pre=s,*p = s->next;
    while (p != NULL)
    {
    free(pre);
    pre = p;
    p = p->next;
    }
    free(pre); //s指向尾結(jié)點(diǎn),釋放其空間
    };

    bool StackEmpty(LinkStNode * s)
    {
    return(s->next == NULL);
    };

    void Push(LinkStNode *& s, int e)
    {
    LinkStNode *p;
    p = (LinkStNode *)malloc(sizeof(LinkStNode));
    p->data = e; //新建元素e對(duì)應(yīng)的結(jié)點(diǎn)p
    p->next = s->next; //插入p結(jié)點(diǎn)作為開始結(jié)點(diǎn)
    s->next = p;
    };

    bool Pop(LinkStNode *& s, int & e)
    {
    LinkStNode *p;
    if (s->next == NULL) //棧空的情況
    return false;
    p = s->next; //p指向開始結(jié)點(diǎn)
    e = p->data;
    s->next = p->next; //刪除p結(jié)點(diǎn)
    free§; //釋放p結(jié)點(diǎn)
    return true;
    };

    bool GetTop(LinkStNode * s, int & e)
    {
    LinkStNode *p;
    if (s->next == NULL) //棧空的情況
    return false;
    p = s->next; //p指向開始結(jié)點(diǎn)
    e = p->data;
    s->next = p->next; //刪除p結(jié)點(diǎn)
    free§; //釋放p結(jié)點(diǎn)
    return true;
    };
    3、隊(duì)列的鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)的實(shí)現(xiàn)
    (1)用隨機(jī)函數(shù)生成10個(gè)3位整數(shù)(100~999),把這些整數(shù)應(yīng)用入隊(duì)操作存于隊(duì)列中;
    (2)應(yīng)用遍歷操作輸出隊(duì)列的內(nèi)容;
    (3)把隊(duì)列的內(nèi)容翻轉(zhuǎn),應(yīng)用出隊(duì)操作輸出隊(duì)列的內(nèi)容。

    Queuemain.cpp
    #include “pch.h”
    #include
    #include"header.h"
    #include"time.h"

    int main()
    {
    LinkQuNode *QuNode;
    InitQueue(QuNode);
    time_t t; // 定義時(shí)間變量
    srand((unsigned)time(&t)); //由時(shí)間確定隨機(jī)序列,執(zhí)行一次
    for (int i = 0; i < 10; i++)
    {
    int a = rand() % 900 + 100;
    enQueue(QuNode, a);
    }
    printfo(QuNode->front);//應(yīng)用遍歷操作輸出隊(duì)列的內(nèi)容;
    printf("\n");
    printf("\n");
    printf("\n");
    printf("\n");
    QuNode->rear = QuNode->front;//在把隊(duì)列的內(nèi)容翻轉(zhuǎn)前,把鏈隊(duì)頭結(jié)點(diǎn)的隊(duì)尾結(jié)點(diǎn)改變
    Rollback(QuNode->front);//把隊(duì)列的內(nèi)容翻轉(zhuǎn),并且QuNode->front自動(dòng)指向數(shù)據(jù)結(jié)尾位置(不知道為什么)
    for (int i = 0; i < 10; i++)
    {
    int r;
    deQueue(QuNode, r);//把隊(duì)列的內(nèi)容翻轉(zhuǎn)后,應(yīng)用出隊(duì)操作輸出隊(duì)列的內(nèi)容
    printf("%d", r);
    printf("\n");
    printf("\n");
    }

    system("pause");

    }

    Queue.h
    #pragma once
    #include"pch.h"
    #include “malloc.h”
    #include
    typedef struct qnode
    {
    int data;
    struct qnode * next;
    }DataNode;
    typedef struct qlink
    {
    DataNode * front;
    DataNode * rear;
    }LinkQuNode;
    void InitQueue(LinkQuNode *&q);
    void DestoryQueue(LinkQuNode *&q);
    bool QueueEmpty(LinkQuNode *&q);
    void enQueue(LinkQuNode *&q, int e);
    bool deQueue(LinkQuNode *&q, int &e);
    void printfo(DataNode *p);
    DataNode * Rollback(DataNode *&L);
    Queue.cpp
    #include"pch.h"
    #include “header.h”

    void InitQueue(LinkQuNode *& q)
    {
    q = (LinkQuNode *)malloc(sizeof(LinkQuNode));
    q->front = q->rear = NULL;
    }

    void DestoryQueue(LinkQuNode *& q)
    {
    DataNode *pre = q->front, *p;
    if (pre!=NULL)
    {
    p = pre->next;
    while (p != NULL)
    {
    free(pre);
    pre = p; p = p->next;
    }
    free(q);
    }
    }

    bool QueueEmpty(LinkQuNode *& q)
    {
    return (q->rear==NULL);
    }

    void enQueue(LinkQuNode *& q, int e)
    {
    DataNode *p;
    p = (DataNode *)malloc(sizeof(DataNode));
    p->data = e;
    p->next = NULL;
    if (q->rear == NULL)
    q->front = q->rear = p;
    else
    {
    q->rear->next = p;
    q->rear = p;
    }
    }

    bool deQueue(LinkQuNode *& q, int & e)
    {
    DataNode *t;
    if (q->rear == NULL) //隊(duì)列為空
    return false;
    t = q->front; //t指向第一個(gè)數(shù)據(jù)結(jié)點(diǎn)
    if (q->front == q->rear) //隊(duì)列中只有一個(gè)結(jié)點(diǎn)時(shí)
    q->front = q->rear = NULL;
    else //隊(duì)列中有多個(gè)結(jié)點(diǎn)時(shí)
    q->front = q->front->next;
    e = t->data;
    free(t);
    return true;
    }

    void printfo(DataNode * p)
    {
    while § {
    int aa = p->data;
    printf("%d", aa);
    p = p->next;
    printf("\n");
    }
    }

    DataNode * Rollback(DataNode & L)
    {
    //ActList temp=new ActList;
    if (L == NULL || L->next == NULL) return L; //少于兩個(gè)節(jié)點(diǎn)沒有反轉(zhuǎn)的必要。
    DataNode * p;
    DataNode * q;
    DataNode * r;
    p = L;
    q = L->next;
    L->next = NULL; //舊的頭指針是新的尾指針,next需要指向NULL
    while (q) {
    r = q->next; //先保留下一個(gè)step要處理的指針
    q->next = p; //然后p q交替工作進(jìn)行反向
    p = q;
    q = r;
    }
    L = p; // 最后q必然指向NULL,所以返回了p作為新的頭指針
    return L;
    }

    4、棧和隊(duì)列的簡單應(yīng)用:
    編寫函數(shù),判斷給定的字符串是否回文。(可以設(shè)計(jì)多種不同算法實(shí)現(xiàn))
    回文數(shù)main.cpp
    #include “pch.h”
    #include"header.h"
    #include
    bool symmetry(ElemType str[])
    {
    int i; ElemType e;
    SqStack *st;
    InitStack(st); //初始化棧
    for (i = 0; str[i] != ‘\0’; i++) //將串所有元素進(jìn)棧
    Push(st, str[i]); //元素進(jìn)棧
    for (i = 0; str[i] != ‘\0’; i++)
    {
    Pop(st, e); //退棧元素e
    if (str[i] != e) //若e與當(dāng)前串元素不同則不是對(duì)稱串
    {
    DestroyStack(st); //銷毀棧
    return false;
    }
    }
    DestroyStack(st); //銷毀棧
    return true;
    }

    int main()
    {
    ElemType str[] = “12343fd21”;
    if (symmetry(str))
    printf("%s是回文串\n", str);
    else
    printf("%s不是回文串\n", str);
    return 1;
    }

    回文數(shù).h
    #pragma once
    //順序棧基本運(yùn)算算法
    #include <stdio.h>
    #include <malloc.h>
    #define MaxSize 100
    typedef char ElemType;
    typedef struct
    {
    ElemType data[MaxSize];
    int top; //棧指針
    } SqStack; //順序棧類型
    void InitStack(SqStack *&s);
    void DestroyStack(SqStack *&s);
    bool StackEmpty(SqStack *s);
    bool Push(SqStack *&s, ElemType e);
    bool Pop(SqStack *&s, ElemType &e);
    bool GetTop(SqStack *s, ElemType &e);
    回文數(shù).cpp
    #include"pch.h"
    #include"header.h"
    void InitStack(SqStack *&s)
    {
    s = (SqStack *)malloc(sizeof(SqStack));
    s->top = -1;
    }
    void DestroyStack(SqStack *&s)
    {
    free(s);
    }
    bool StackEmpty(SqStack *s)
    {
    return(s->top == -1);
    }
    bool Push(SqStack *&s, ElemType e)
    {
    if (s->top == MaxSize - 1) //棧滿的情況,即棧上溢出
    return false;
    s->top++;
    s->data[s->top] = e;
    return true;
    }
    bool Pop(SqStack *&s, ElemType &e)
    {
    if (s->top == -1) //棧為空的情況,即棧下溢出
    return false;
    e = s->data[s->top];
    s->top–;
    return true;
    }
    bool GetTop(SqStack *s, ElemType &e)
    {
    if (s->top == -1) //棧為空的情況,即棧下溢出
    return false;
    e = s->data[s->top];
    return true;
    }

    五、實(shí)驗(yàn)結(jié)果及分析
    1、線性表的鏈表實(shí)現(xiàn):

    數(shù)據(jù)插入成功后,輸入任意非零數(shù)進(jìn)入查詢數(shù)據(jù),輸入0進(jìn)入另一個(gè)操作。以下情況依此類推

    2、棧的鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)實(shí)現(xiàn)
    進(jìn)棧

    出棧

    3、隊(duì)列的鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)的實(shí)現(xiàn)

    4、棧和隊(duì)列的簡單應(yīng)用:

    總結(jié)

    以上是生活随笔為你收集整理的实验一 链式存储结构的基本操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。