山東大学実験「データ構造」III:リニア動作させるテーブル

実験的手術台トリリニア

まず、実験の目的

挿入、削除、検索:1、リニアテーブルの基本的な操作を習得。
2、リンクリスト歩行器の使用を習得。

第二に、実験的なコンテンツ

要素ノード0は、順序付けられたリストを作成するために、(0のノード要素値を作成しない)入力端面を表すように1、n個の入力がゼロの整数値ではありません。リスト全体の出力。
図2に示すように、整数を入力し、対応する位置に挿入された数は、順序付きリスト。リスト全体の出力。
3、、検索リストに、リスト内の出力位置を整数を入力します。0出力が存在しない場合。
図4に示すように、再度、検索リストに、リスト内の出力位置を整数を入力します。0出力が存在しない場合。
図5に示すように、再びゼロ以外の整数を入力するn個のノード要素の値として、0は、順序付けられたリストを作成するために、(0のノード要素値を作成しない)入力端面を表しています。リスト全体の出力。
図6に示すように、上記のリスト・トラバーサルの併用は、マージされたリストを出力し、2つの順序付けリスト内に実装されています。
7.ヒント:単一ノードテストのリストに注意を払います。

コードは以下の通りであります:

#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<string.h>
using namespace std;
struct chainNode{//结点类
    chainNode(){}
    chainNode(const int& element){this->element=element;}
    int element;
    chainNode *next;    
};
class chain{//链表类
public:
    chain(){firstNode=NULL;listSize=0;}
    void insert(const int& theE);//插入方法
    int indexOf(const int theE);//查找指定元素的位置
    void print();//打印链表
    friend chain combine(const chain c1,const chain c2);//合并两个链表,返回新链表

    int listSize;
    chainNode* firstNode;
};
void chain::insert(const int& theElement){
    chainNode* s=new chainNode(theElement);
    if(listSize==0){//在空链表中插入第一个结点 
        firstNode=s;
        s->next=NULL;
        listSize++;
        return;
    }
    chainNode* cur=firstNode;
    chainNode* p;
    int index=1;
    for(;index<listSize+1&&cur->element<theElement;index++){
        p=cur;//记录cur的前一个
        cur=cur->next; 
    }
    if(index==1){//插入头结点    
        s->next=cur;
        firstNode=s;
    }
    else{//插入中间的结点or尾 
        p->next=s;
        s->next=cur;
    }
    listSize++; 
}

int chain::indexOf(const int theElement){
    chainNode* cur=firstNode;
    int index=1;
    while(cur!=NULL&&cur->element!=theElement){
        cur=cur->next;
        index++;
    }
    if(cur==NULL) return 0;
    else return index;
}
void chain::print(){
    chainNode* p=firstNode;
    while(p->next!= NULL){
        cout<<p->element<<",";
        p=p->next;
    }
    cout<<p->element<<endl;
}
chain combine(const chain c1,const chain c2){
    chain c3;
    chainNode* p=c1.firstNode;
    chainNode* s;
    while(p!=NULL){
        c3.insert(p->element);
        s=p;
        p=p->next;
    }
    s=c2.firstNode;
    while(s!=NULL){
        c3.insert(s->element);
        s=s->next;
    }
    return c3;
}
int main(){
    chain c1,c2,c3;
    int temp;
    cout<<"Input1"<<endl;
    while(cin>>temp&&temp!=0)
        c1.insert(temp);
    cout<<"Output1"<<endl;
    c1.print();
    cout<<"Input2"<<endl;
    cin>>temp;
    c1.insert(temp);
    cout<<"Output2"<<endl;
    c1.print();
    cout<<"Input3"<<endl;
    cin>>temp;
    cout<<"Output3"<<endl;
    cout<<c1.indexOf(temp)<<endl;
    cout<<"Input4"<<endl;
    cin>>temp;
    cout<<"Output4"<<endl;
    cout<<c1.indexOf(temp)<<endl;
    cout<<"Input5"<<endl;
    while(cin>>temp&&temp!=0)
        c2.insert(temp);
    cout<<"Output5"<<endl;
    c2.print();
    c3=combine(c1,c2);
    c3.print();
    cout<<"End"<<endl;    
    return 0;
} 

結論分析と経験:

回顾了链表的插入,查找,和输出,在写合并方法时顺便复习了上学期C++课程关于友元函数的讲解。
公開された11元の記事 ウォンの賞賛0 ビュー36

おすすめ

転載: blog.csdn.net/weixin_43959421/article/details/103973878