課程
/后端開發(fā)
/C++
/數(shù)據(jù)結(jié)構(gòu)探險(xiǎn)之線性表篇
可以運(yùn)行的源碼即可,我編寫的莫名其妙有錯(cuò)誤,請(qǐng)大佬們發(fā)下源碼
2017-08-16
源自:數(shù)據(jù)結(jié)構(gòu)探險(xiǎn)之線性表篇 2-9
正在回答
MyList.h
#include"stdafx.h"
#ifndef LIST_H
#define LIST_H
#include"Coordinate.h"
class List
{
public:
List(int size);
~List();
void ClearList();
bool ListEmpty();
int ListLength();
bool GetElem(int i, Coordinate *e);
int LocateElem(Coordinate ?*e);
bool PriorElem(Coordinate*currentElem, Coordinate*preElem);
bool NextElem(Coordinate *currentElem, Coordinate *nextElem);
bool ListInsert(int i, Coordinate *e);
bool ListDelete(int i, Coordinate*e);
void ListTraverse();
//C語言描述 ?順序表
/*bool InitList(List **list);
void DestoryList(List *list);
void ClearList(List *list);
bool ListEmpty(List *list);
int ListLength(List *list);
bool GetElem(List *list, init i, Elem *e);
int LocateElem(List*list, Elem *e);
bool PriorElem(List *list, Elem*currentElem, Elem*preElem);
bool NextElem(List *list, Elem *currentElem, Elem*nextElem);
bool ListInsert(List *list, int i, Elem *e);
bool ListDelete(List *list, int i, Elem *e);
void ListTraverse(List *list);*/
private:
Coordinate *m_pList;
int m_iSize;
int m_iLength;
};
#endif
Coordinate.cpp
using namespace std;
Coordinate::Coordinate(int x, int y)
m_iX = x;
m_iY = y;
}
void Coordinate::printCoordinate()
cout << "(" << m_iX << "," << m_iY << ")" << endl;
ostream &operator<<(ostream &out, Coordinate &coor)
out << "(" << coor.m_iX << "," << coor.m_iY << ")" << endl;
return out;
bool Coordinate::operator==(Coordinate &coor)
if (this->m_iX == coor.m_iX&&this->m_iY == coor.m_iY)
return true;
return false;
Coordinate.h
#include"iostream"
#include"ostream"
#ifndef COORDINATE_H
#define COORDINATE_H
class Coordinate
friend ostream &operator<<(ostream &out, Coordinate &coor);
Coordinate(int x = 0, int y = 0);
void printCoordinate();
bool operator==(Coordinate &coor);
int m_iX;
int m_iY;
MyList.cpp
#include"MyList.h"
List::List(int size)
m_iSize = size;
m_pList = new Coordinate[m_iSize];
m_iLength = 0;
List::~List()
delete[]m_pList;
m_pList = 0;
void List::ClearList()
bool List::ListEmpty()
if (m_iLength == 0)
int List::ListLength()
return m_iLength;
bool List::GetElem( int i, Coordinate*e)
if (i < 0 || i >= m_iSize)
*e = m_pList[i];
int List::LocateElem(Coordinate *e)
for (int i = 0; i < m_iLength; i++)
if (m_pList[i] == *e)
return i;
return -1;
bool List::PriorElem(Coordinate*currentElem, Coordinate*preElem)
int temp = LocateElem(currentElem);
if (temp == -1)
else
if (temp == 0)
*preElem = m_pList[temp - 1];
bool List::NextElem(Coordinate *currentElem, Coordinate *nextElem)
if (temp == m_iLength-1)
*nextElem = m_pList[temp+1];
bool List::ListInsert(int i, Coordinate *e)
//int temp;
if (i<0 || i>m_iLength)
for (int k =m_iLength-1; k>=i; k--)
m_pList[k + 1] = m_pList[k];
m_pList[i] = *e;
m_iLength++;
bool List::ListDelete(int i, Coordinate*e)
if (i<0 || i >m_iLength)
for (int k = i+1; k < m_iLength - 1; k++)
m_pList[k - 1] = m_pList[k];
m_iLength--;
void List::ListTraverse()
cout << m_pList[i] << endl;
//m_pList[i].printCoordinat();
// listtest.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
Coordinate e1(3,5);
Coordinate e2(5,7);
Coordinate e3(6,8);
/*Coordinate e4 = 2;
Coordinate e5 = 9;
Coordinatee6 = 1;
Coordinate e7 = 8;*/
Coordinate temp(0,0);
List *p = new List(10);
//cout << "length:" << p->ListLength() << endl;
//p->ListInsert(0, &e1);
p->ListInsert(0, &e1);
p->ListInsert(1, &e2);
p->ListInsert(2, &e3);
/*p->ListInsert(3, &e4);
p->ListInsert(4, &e5);
p->ListInsert(5, &e6);#include"stdafx.h"
main.cpp
p->ListInsert(6, &e7);*/
p->ListTraverse();
//p->PriorElem(&e4, &temp);
//cout << "temp:" << temp << endl;
//p->NextElem(&e4, &temp);
/*p->GetElem(0, &temp);
cout << "#" << temp << endl;
cout << p->LocateElem(&temp);*/
/*p->ListDelete(0, &temp);
if (!p->ListEmpty())
cout << "not empty" << endl;
p->ClearList();
if (p->ListEmpty())
cout << "empty" << endl;
cout << "#" << temp << endl;*/
delete p;
system("pause");
return 0;
C10H16N5O13P3 提問者
舉報(bào)
線性表的主體順序表和鏈表,讓學(xué)員能夠?qū)⒅R(shí)融會(huì)貫通學(xué)以致用
1 回答請(qǐng)問這個(gè)有源碼嗎
2 回答哪里可以下載課件和代碼
1 回答關(guān)于課程的源碼
2 回答請(qǐng)問源代碼有嗎
2 回答可以這樣寫嗎
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2017-08-17
MyList.h
#include"stdafx.h"
#ifndef LIST_H
#define LIST_H
#include"Coordinate.h"
class List
{
public:
List(int size);
~List();
void ClearList();
bool ListEmpty();
int ListLength();
bool GetElem(int i, Coordinate *e);
int LocateElem(Coordinate ?*e);
bool PriorElem(Coordinate*currentElem, Coordinate*preElem);
bool NextElem(Coordinate *currentElem, Coordinate *nextElem);
bool ListInsert(int i, Coordinate *e);
bool ListDelete(int i, Coordinate*e);
void ListTraverse();
//C語言描述 ?順序表
/*bool InitList(List **list);
void DestoryList(List *list);
void ClearList(List *list);
bool ListEmpty(List *list);
int ListLength(List *list);
bool GetElem(List *list, init i, Elem *e);
int LocateElem(List*list, Elem *e);
bool PriorElem(List *list, Elem*currentElem, Elem*preElem);
bool NextElem(List *list, Elem *currentElem, Elem*nextElem);
bool ListInsert(List *list, int i, Elem *e);
bool ListDelete(List *list, int i, Elem *e);
void ListTraverse(List *list);*/
private:
Coordinate *m_pList;
int m_iSize;
int m_iLength;
};
#endif
#include"stdafx.h"
#include"Coordinate.h"
using namespace std;
Coordinate::Coordinate(int x, int y)
{
m_iX = x;
m_iY = y;
}
void Coordinate::printCoordinate()
{
cout << "(" << m_iX << "," << m_iY << ")" << endl;
}
ostream &operator<<(ostream &out, Coordinate &coor)
{
out << "(" << coor.m_iX << "," << coor.m_iY << ")" << endl;
return out;
}
bool Coordinate::operator==(Coordinate &coor)
{
if (this->m_iX == coor.m_iX&&this->m_iY == coor.m_iY)
{
return true;
}
return false;
}
Coordinate.h
#include"stdafx.h"
#include"iostream"
#include"ostream"
using namespace std;
#ifndef COORDINATE_H
#define COORDINATE_H
class Coordinate
{
friend ostream &operator<<(ostream &out, Coordinate &coor);
public:
Coordinate(int x = 0, int y = 0);
void printCoordinate();
bool operator==(Coordinate &coor);
private:
int m_iX;
int m_iY;
};
#endif
MyList.cpp
#include"stdafx.h"
#include"MyList.h"
#include"iostream"
using namespace std;
List::List(int size)
{
m_iSize = size;
m_pList = new Coordinate[m_iSize];
m_iLength = 0;
}
List::~List()
{
delete[]m_pList;
m_pList = 0;
}
void List::ClearList()
{
m_iLength = 0;
}
bool List::ListEmpty()
{
if (m_iLength == 0)
{
return true;
}
return false;
}
int List::ListLength()
{
return m_iLength;
}
bool List::GetElem( int i, Coordinate*e)
{
if (i < 0 || i >= m_iSize)
{
return false;
}
*e = m_pList[i];
return true;
}
int List::LocateElem(Coordinate *e)
{
for (int i = 0; i < m_iLength; i++)
{
if (m_pList[i] == *e)
{
return i;
}
}
return -1;
}
bool List::PriorElem(Coordinate*currentElem, Coordinate*preElem)
{
int temp = LocateElem(currentElem);
if (temp == -1)
{
return false;
}
else
{
if (temp == 0)
{
return false;
}
else
{
*preElem = m_pList[temp - 1];
return true;
}
}
}
bool List::NextElem(Coordinate *currentElem, Coordinate *nextElem)
{
int temp = LocateElem(currentElem);
if (temp == -1)
{
return false;
}
else
{
if (temp == m_iLength-1)
{
return false;
}
else
{
*nextElem = m_pList[temp+1];
return true;
}
}
}
bool List::ListInsert(int i, Coordinate *e)
{
//int temp;
if (i<0 || i>m_iLength)
{
return false;
}
for (int k =m_iLength-1; k>=i; k--)
{
m_pList[k + 1] = m_pList[k];
}
m_pList[i] = *e;
m_iLength++;
return true;
}
bool List::ListDelete(int i, Coordinate*e)
{
if (i<0 || i >m_iLength)
{
return false;
}
*e = m_pList[i];
for (int k = i+1; k < m_iLength - 1; k++)
{
m_pList[k - 1] = m_pList[k];
}
m_iLength--;
return true;
}
void List::ListTraverse()
{
for (int i = 0; i < m_iLength; i++)
{
cout << m_pList[i] << endl;
//m_pList[i].printCoordinat();
}
}
// listtest.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
#include"iostream"
#include"MyList.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Coordinate e1(3,5);
Coordinate e2(5,7);
Coordinate e3(6,8);
/*Coordinate e4 = 2;
Coordinate e5 = 9;
Coordinatee6 = 1;
Coordinate e7 = 8;*/
Coordinate temp(0,0);
List *p = new List(10);
//cout << "length:" << p->ListLength() << endl;
//p->ListInsert(0, &e1);
//cout << "length:" << p->ListLength() << endl;
p->ListInsert(0, &e1);
p->ListInsert(1, &e2);
p->ListInsert(2, &e3);
/*p->ListInsert(3, &e4);
p->ListInsert(4, &e5);
p->ListInsert(5, &e6);#include"stdafx.h"
#include"MyList.h"
#include"iostream"
using namespace std;
List::List(int size)
{
m_iSize = size;
m_pList = new Coordinate[m_iSize];
m_iLength = 0;
}
List::~List()
{
delete[]m_pList;
m_pList = 0;
}
void List::ClearList()
{
m_iLength = 0;
}
bool List::ListEmpty()
{
if (m_iLength == 0)
{
return true;
}
return false;
}
int List::ListLength()
{
return m_iLength;
}
bool List::GetElem( int i, Coordinate*e)
{
if (i < 0 || i >= m_iSize)
{
return false;
}
*e = m_pList[i];
return true;
}
int List::LocateElem(Coordinate *e)
{
for (int i = 0; i < m_iLength; i++)
{
if (m_pList[i] == *e)
{
return i;
}
}
return -1;
}
bool List::PriorElem(Coordinate*currentElem, Coordinate*preElem)
{
int temp = LocateElem(currentElem);
if (temp == -1)
{
return false;
}
else
{
if (temp == 0)
{
return false;
}
else
{
*preElem = m_pList[temp - 1];
return true;
}
}
}
bool List::NextElem(Coordinate *currentElem, Coordinate *nextElem)
{
int temp = LocateElem(currentElem);
if (temp == -1)
{
return false;
}
else
{
if (temp == m_iLength-1)
{
return false;
}
else
{
*nextElem = m_pList[temp+1];
return true;
}
}
}
bool List::ListInsert(int i, Coordinate *e)
{
//int temp;
if (i<0 || i>m_iLength)
{
return false;
}
for (int k =m_iLength-1; k>=i; k--)
{
m_pList[k + 1] = m_pList[k];
}
m_pList[i] = *e;
m_iLength++;
return true;
}
bool List::ListDelete(int i, Coordinate*e)
{
if (i<0 || i >m_iLength)
{
return false;
}
*e = m_pList[i];
for (int k = i+1; k < m_iLength - 1; k++)
{
m_pList[k - 1] = m_pList[k];
}
m_iLength--;
return true;
}
void List::ListTraverse()
{
for (int i = 0; i < m_iLength; i++)
{
cout << m_pList[i] << endl;
//m_pList[i].printCoordinat();
}
}
main.cpp
// listtest.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
#include"iostream"
#include"MyList.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Coordinate e1(3,5);
Coordinate e2(5,7);
Coordinate e3(6,8);
/*Coordinate e4 = 2;
Coordinate e5 = 9;
Coordinatee6 = 1;
Coordinate e7 = 8;*/
Coordinate temp(0,0);
List *p = new List(10);
//cout << "length:" << p->ListLength() << endl;
//p->ListInsert(0, &e1);
//cout << "length:" << p->ListLength() << endl;
p->ListInsert(0, &e1);
p->ListInsert(1, &e2);
p->ListInsert(2, &e3);
/*p->ListInsert(3, &e4);
p->ListInsert(4, &e5);
p->ListInsert(5, &e6);#include"stdafx.h"
#include"MyList.h"
#include"iostream"
using namespace std;
List::List(int size)
{
m_iSize = size;
m_pList = new Coordinate[m_iSize];
m_iLength = 0;
}
List::~List()
{
delete[]m_pList;
m_pList = 0;
}
void List::ClearList()
{
m_iLength = 0;
}
bool List::ListEmpty()
{
if (m_iLength == 0)
{
return true;
}
return false;
}
int List::ListLength()
{
return m_iLength;
}
bool List::GetElem( int i, Coordinate*e)
{
if (i < 0 || i >= m_iSize)
{
return false;
}
*e = m_pList[i];
return true;
}
int List::LocateElem(Coordinate *e)
{
for (int i = 0; i < m_iLength; i++)
{
if (m_pList[i] == *e)
{
return i;
}
}
return -1;
}
bool List::PriorElem(Coordinate*currentElem, Coordinate*preElem)
{
int temp = LocateElem(currentElem);
if (temp == -1)
{
return false;
}
else
{
if (temp == 0)
{
return false;
}
else
{
*preElem = m_pList[temp - 1];
return true;
}
}
}
bool List::NextElem(Coordinate *currentElem, Coordinate *nextElem)
{
int temp = LocateElem(currentElem);
if (temp == -1)
{
return false;
}
else
{
if (temp == m_iLength-1)
{
return false;
}
else
{
*nextElem = m_pList[temp+1];
return true;
}
}
}
bool List::ListInsert(int i, Coordinate *e)
{
//int temp;
if (i<0 || i>m_iLength)
{
return false;
}
for (int k =m_iLength-1; k>=i; k--)
{
m_pList[k + 1] = m_pList[k];
}
m_pList[i] = *e;
m_iLength++;
return true;
}
bool List::ListDelete(int i, Coordinate*e)
{
if (i<0 || i >m_iLength)
{
return false;
}
*e = m_pList[i];
for (int k = i+1; k < m_iLength - 1; k++)
{
m_pList[k - 1] = m_pList[k];
}
m_iLength--;
return true;
}
void List::ListTraverse()
{
for (int i = 0; i < m_iLength; i++)
{
cout << m_pList[i] << endl;
//m_pList[i].printCoordinat();
}
}
p->ListInsert(6, &e7);*/
p->ListTraverse();
//p->PriorElem(&e4, &temp);
//cout << "temp:" << temp << endl;
//p->NextElem(&e4, &temp);
//cout << "temp:" << temp << endl;
/*p->GetElem(0, &temp);
cout << "#" << temp << endl;
cout << p->LocateElem(&temp);*/
/*p->ListDelete(0, &temp);
if (!p->ListEmpty())
{
cout << "not empty" << endl;
}
p->ClearList();
p->ListTraverse();
if (p->ListEmpty())
{
cout << "empty" << endl;
}
cout << "#" << temp << endl;*/
delete p;
system("pause");
return 0;
}