数据结构实验报告

实验一 顺序表的插入和删除

实验目的

1、掌握使用Turbo C上机调试线性表的基本方法;

2、掌握线性表的基本操作:插入、删除在顺序存储结构上的运算。

实验要求

1、认真阅读和掌握本实验的给定的程序;

2、按照你的操作需要,编制程序、上机调试运行程序;

3、保存程序的运行结果,并结合程序进行分析。

实验内容

利用顺序表完成线性表信息的管理。要求首先建立并初始化线性表,并实现增加、删除等功能。

(1)    对给定字母A C D E建立顺序表

(2)    在给定位置插入字母B,形成顺序表A B C D E

(3)    删除字母D,形成表A B C D


实验代码

/* JList.h */

#define _JLLIST_H_

#ifndef BUFFER

#define BUFFER 256

#endif

class JList

{

public:

    typedef char* Position;

    JList();

    bool isEmpty(void);

    int length(void);

    void insertElementById(int, char);

    void append(char);

    void rmElementById(int);

//  char getElementById(int);

    char* toString(void);

    ~JList();

private:

    int LENGTH;

    int cap; // Capacity

    int maxLength;

    Position HEAD;

    char *cell;

    void reload(void);

};


/* JList.cpp */

#include "JList.h"

#ifndef NULL

#define NULL 0

#endif

#ifndef BUFFER

#define BUFFER 256

#endif

JList::JList()

{

     cell = new char[BUFFER];

     LENGTH = 0;

     HEAD = cell;

     cap = 1;

     maxLength = cap * BUFFER;

}

int JList::length(void)

{

     return LENGTH;

}

void JList::append(char input)

{

    if (LENGTH < maxLength)

    {

       cell[LENGTH] = input;

    }

    else

    {

       reload();

       cell[LENGTH] = input;

    }

    ++LENGTH;

}

void JList::reload(void)

{

    char *newCell;

    char *temp;

    int ctrlLoop;

    ++cap;

    maxLength = cap * BUFFER;

    newCell = new char[maxLength];

    for (ctrlLoop = 0; ctrlLoop < LENGTH; ++ctrlLoop)

    {

       newCell[ctrlLoop] = cell[ctrlLoop];

    }

    temp = cell;

    cell = newCell;

    newCell = NULL;

    delete(temp);

}

char* JList::toString(void)

{

    int ctrlLoop;

    char* newStr;

    newStr = new char[LENGTH + 1];

    for (ctrlLoop = 0; ctrlLoop < LENGTH; ++ctrlLoop)

    {

       newStr[ctrlLoop] = cell[ctrlLoop];

    }

    newStr[LENGTH] = '\0';

    return newStr;

}

void JList::insertElementById(int position, char input)

{

    int newLength;

    int cellPos;

    int ctrlLoop;

    newLength = LENGTH + 1;

    if (newLength > maxLength)

    {

       reload();

    }

    for (ctrlLoop = LENGTH - 1; ctrlLoop >= position; --ctrlLoop)

    {

       cell[ctrlLoop + 1] = cell[ctrlLoop];

    }

    ++LENGTH;

    cell[position] = input;

}

void JList::rmElementById(int position)

{

    int ctrlLoop;

    int newLength;

    char cache;

    newLength = LENGTH - 1;

    for (ctrlLoop = position; ctrlLoop < LENGTH; ++ctrlLoop)

    {

       cell[ctrlLoop] = cell[ctrlLoop + 1];

       cache = cell[position];

    }

    cell[LENGTH - 1] = NULL;

    --LENGTH;

}

JList::~JList()

{

    delete(cell);

}


/* example.cpp – this is the main document of the App */

#include <iostream>

#ifndef _JLIST_H_

#include "JList.h"

#endif

void output(JList *);

void genAxis(int);

void drawLine(int);

void splash(void);

int main(void)

{

    JList L;

    char cInput;

    int elementId;

    splash();

    std::cout << " Give me a string to init the list." << std::endl;

    std::cout << " String goes here: ";

    cInput = getchar();

    while (cInput != '\n')

    {

       L.append(cInput);

       cInput = getchar();

    }

    output(&L);

    std::cout << " What do U want to insert?" << std::endl;

    std::cout << " Give me a character: ";

    std::cin >> cInput;

    putchar('\n');

    std::cout << " Where do U want to insert?\nGive me the place where U want to insert after." << std::endl;

    std::cout << " Give me the element ID: ";

    std::cin >> elementId;

    L.insertElementById(elementId - 1, cInput);

    output(&L);

    std::cout << " Which element do you want to remove from the list?" << std::endl;

    std::cout << " Give me the element ID: ";

    std::cin >> elementId;

    L.rmElementById(elementId - 1);

    output(&L);

    system("pause");

    return 0;

}

void output(JList* list)

{

    int length;

    char *str;

    char *axis;

    length = list->length();

    str = list->toString();

   

    drawLine(length);

    genAxis(length);

    drawLine(length);

    std::cout << ' ' << str << std::endl;

    drawLine(41);

    putchar('\n');

}

void genAxis(int length)

{

    int ctrl_loop;

    putchar(' ');

    for (ctrl_loop = 0; ctrl_loop < length; ++ctrl_loop)

    {

       std::cout << ctrl_loop + 1;

    }

    putchar('\n');

}

void drawLine(int length)

{

    int ctrl_loop;

    putchar(' ');

    for (ctrl_loop = 0; ctrl_loop < length; ++ctrl_loop)

       putchar('-');

    putchar('\n');

}

void splash(void)

{

    std::cout << " Copyright (C) 20## Spinel Studio" << std::endl;

    std::cout << " By Andrew Junzki" << std::endl;

    putchar('\n');

}


实验二 链表的插入和删除

实验目的

1、掌握使用Turbo C上机调试线性表的基本方法;

2、掌握链表的基本操作:插入、删除和游历的操作。

实验要求

1、认真阅读和掌握本实验的给定的程序;

2、按照你的操作需要,编制程序、上机调试运行程序;

3、保存程序的运行结果,并结合程序进行分析。

实验内容

利用顺序表完成线性表信息的管理。要求首先建立并初始化线性表,并实现增加、删除和游历等功能。

(1)    对给定字母A C D E建立顺序表

(2)    在给定位置插入字母B,形成顺序表A B C D E

(3)    删除字母D,形成表A B C E


实验代码

/* JList.h */

#define _JLIST_H_

class JList

{

private:

    class Node

    {

    public:

       char data;

       Node* next;

       Node* previous;

    };

    typedef Node* pNode;

    typedef pNode List;

    typedef pNode Position;

public:

    JList();

    // Check if the list is empty.

    bool isEmpty(void);

    // Add an element to the list

    void append(char);

    // Insert a element ahead of or behind the element got by id.

    void insertAfterById(int, char);

    // Remove the element got by id.

    void rmElementById(int);

   

    // Get a element by id.

    Position getElementById(int);

    // Get the length of the list.

    int length(void);

    char* toString(void);

    ~JList();

private:

    int LENGTH;

    List HEAD;

    Position tail;

    // Check if the pointer is at the head or tail of the list.

    bool isHead(Position);

    bool isTail(Position);

};


/* JList.cpp */

#include "JList.h"

#include <iostream>

JList::JList()

{

    HEAD = new Node;

    LENGTH = 0;

    HEAD->next = NULL;

    HEAD->previous = NULL;

    tail = HEAD;

}

bool JList::isEmpty(void)

{

    if (HEAD->next == NULL || HEAD->previous == NULL)

       return true;

    else

       return false;

}

bool JList::isHead(Position P)

{

    if (P->previous == NULL)

       return true;

    else

       return false;

}

bool JList::isTail(Position P)

{

    if (P->next == NULL)

       return true;

    else

       return false;

}

void JList::append(char input)

{

    Position new_item;

    new_item = new Node;

    new_item->data = input;

    new_item->next = NULL;

    new_item->previous = tail;

    tail->next = new_item;

    tail = tail->next;

    ++LENGTH;

}

JList::Position JList::getElementById(int id)

{

    int ctrl_loop;

    Position P;

    P = HEAD;

    if (id > LENGTH)

       return NULL;

    for (ctrl_loop = 0; ctrl_loop < id; ++ctrl_loop)

    {

       P = P->next;

    }

    return P;

}

void JList::insertAfterById(int id, char insert)

{

    Position new_node;

    Position TARGET;

    TARGET = getElementById(id);

    if (TARGET == NULL)

       exit(1);

    if (isTail(TARGET))

    {

       append(insert);

       return;

    }

    new_node = new Node;

    new_node->data = insert;

    new_node->next = TARGET->next;

    new_node->previous = TARGET;

    TARGET->next = new_node;

    ++LENGTH;

}

void JList::rmElementById(int id)

{

    Position TARGET;

    Position prev;

    Position next;

    TARGET = getElementById(id);

    prev = TARGET->previous;

    next = TARGET->next;

    prev->next = next;

    next->previous = prev;

    delete(TARGET);

}

char* JList::toString(void)

{

    Position ptr;

    char *str;

    int ctrl_loop;

    ctrl_loop = 0;

    ptr = HEAD->next;

    str = new char[LENGTH + 1];

    while (ptr != NULL)

    {

       str[ctrl_loop] = ptr->data;

       ptr = ptr->next;

       ++ctrl_loop;

    }

    str[ctrl_loop] = '\0';

    return str;

}

int JList::length(void)

{

    return LENGTH;

}

JList::~JList()

{

    while (tail->previous != NULL)

    {

       Position cache;

       cache = tail;

       tail = tail->previous;

       delete(cache);

    }

    delete(tail);

    tail = NULL;

}


/* example.cpp – this is the main document of the App */

#include <iostream>

#ifndef _JLIST_H_

#include "JList.h"

#endif

void output(JList *);

void genAxis(int);

void drawLine(int);

void splash(void);

int main(void)

{

    JList L;

    char cInput;

    int elementId;

    splash();

    std::cout << " Give me a string to init the list." << std::endl;

    std::cout << " String goes here: ";

    cInput = getchar();

    while (cInput != '\n')

    {

       L.append(cInput);

       cInput = getchar();

    }

    output(&L);

    std::cout << " What do U want to insert?" << std::endl;

    std::cout << " Give me a character: ";

    std::cin >> cInput;

    putchar('\n');

    std::cout << " Where do U want to insert?\nGive me the place where U want to insert after." << std::endl;

    std::cout << " Give me the element ID: ";

    std::cin >> elementId;

    L.insertAfterById(elementId, cInput);

    output(&L);

    std::cout << " Which element do you want to remove from the list?" << std::endl;

    std::cout << " Give me the element ID: ";

    std::cin >> elementId;

    L.rmElementById(elementId);

    output(&L);

    system("pause");

    return 0;

}

void output(JList* list)

{

    int length;

    char *str;

    char *axis;

    length = list->length();

    str = list->toString();

   

    drawLine(length);

    genAxis(length);

    drawLine(length);

    std::cout << ' ' << str << std::endl;

    drawLine(41);

    putchar('\n');

}

void genAxis(int length)

{

    int ctrl_loop;

    putchar(' ');

    for (ctrl_loop = 0; ctrl_loop < length; ++ctrl_loop)

    {

       std::cout << ctrl_loop + 1;

    }

    putchar('\n');

}

void drawLine(int length)

{

    int ctrl_loop;

    putchar(' ');

    for (ctrl_loop = 0; ctrl_loop < length; ++ctrl_loop)

       putchar('-');

    putchar('\n');

}

void splash(void)

{

    std::cout << " Copyright (C) 20## Spinel Studio" << std::endl;

    std::cout << " By Andrew Junzki" << std::endl;

    putchar('\n');

}


实验三 二叉树的建立

实验目的

1.        掌握二叉树的原理;

2.        掌握二叉树的构造方法。

实验要求

1.认真思考,编制本实验的程序。

2.上机调试、运行本程序。

3.保存程序的运行结果,并结合程序进行分析。

实验内容

实现用字符A,B,C,D,E,F构成的如下二叉树。


实验四 二叉树的遍历

实验目的

1、掌握二叉树的各种遍历算法的原理;

2、掌握中历算法的编程。

实验要求

1、按照你的操作需要,编制程序、上机调试运行程序;

2、保存程序的运行结果,并结合程序进行分析。

实验内容

对实验三给出的二叉树,编程中历的方法把每个元素显示出来


实验代码

/* JBinTree.h */

#pragma once

#define _JBINTREE_H

typedef char DATA;

class JBinTree

{

private:

    class Node {

    public:

       Node* left;

       Node* right;

       Node* up;

       DATA data;

    };

    typedef Node* pNode;

    typedef pNode Position;

public:

    JBinTree(char);

    Position getNodeByKey(Position, DATA);

    bool appendToNode(char, DATA);

    bool isTail(Position);

    bool getNextNode(char);

    void resetPtr(void);

    char* toString(void);   

    void output();

   

    ~JBinTree();

private:

    Position HEAD;

    Position ptr;

    int nodeNum;

    void lookup(Position);

};

typedef JBinTree* pTree;


/* JBinList.cpp */

#include "JBinTree.h"

#include <iostream>

JBinTree::JBinTree(char data)

{

    HEAD = new Node;

    HEAD->data = data;

    HEAD->up = NULL;

    HEAD->left = NULL;

    HEAD->right = NULL;

    ptr = HEAD;

    nodeNum = 0;

}

bool JBinTree::appendToNode(char turn, DATA data)

{

    Position newNode;

    if (turn == 'L')

    {

       if (ptr->left == NULL)

       {

           newNode = new Node;

           ptr->left = newNode;

       }

       else

           return false;

    }

    else if (turn == 'R')

    {

       if (ptr->right == NULL)

       {

           newNode = new Node;

           ptr->right = newNode;

       }

       else

           return false;

    }

    else

       return false;

    newNode->up = ptr;

    newNode->data = data;

    newNode->left = NULL;

    newNode->right = NULL;

    ++nodeNum;

    return true;

}

JBinTree::Position JBinTree::getNodeByKey(Position P, DATA key)

{

    if (P->data == key)

       return P;

    if (isTail(P))

       return NULL;

    if (P->left != NULL)

    {

       Position pos;

       pos = getNodeByKey(P->left, key);

       if (pos != NULL)

           return pos;

    }

    if (P->right != NULL)

    {

       Position pos;

       pos = getNodeByKey(P->right, key);

       if (pos != NULL)

           return pos;

    }

    return NULL;

}

bool JBinTree::getNextNode(char turn)

{

    if (turn == 'L')

       ptr = ptr->left;

    else if (turn == 'R')

       ptr = ptr->right;

    else

       return false;

    return true;

}

bool JBinTree::isTail(Position P)

{

    if (P->left == NULL && P->right == NULL)

       return true;

    else

        return false;

}

void JBinTree::resetPtr(void)

{

    ptr = HEAD;

}

void JBinTree::output(void)

{

    Position P;

    P = HEAD;

    lookup(P);

}

void JBinTree::lookup(Position P)

{

    std::cout << P->data << ' ';

    if (isTail(P))

       return;

    if (P->left != NULL)

    {

       lookup(P->left);

    }

    if (P->right != NULL)

    {

       lookup(P->right);

    }

}

JBinTree::~JBinTree()

{

}


/* example.h */

#include <iostream>

#ifndef _JBINLIST_H

#include "JBinTree.h"

#endif

int main(void)

{

    pTree Tree;

    Tree = new JBinTree('A');

   

    Tree->appendToNode('L', 'B');

    Tree->appendToNode('R', 'C');

    Tree->getNextNode('L');

    Tree->appendToNode('L', 'D');

    Tree->appendToNode('R', 'E');

    Tree->resetPtr();

    Tree->getNextNode('R');

    Tree->appendToNode('L', 'F');

    Tree->output();

    system("pause");

    return 0;

}

相关推荐