65,211
社区成员
发帖
与我相关
我的任务
分享
//EmpList.h
#pragma once
class EmpList
{
public:
EmpList(void);
~EmpList(void);
EmpList(int nID, char *name,char *sex,int age);
private:
Emp* EmpNode;
int m_length;
public:
bool Init();
bool Destroy();
bool Insert(int postion,Emp *emp);
bool Clear();
void ShowList();
int GetLength();
bool AddItem(Emp *emp);
void Sort();
Emp GetItem(int position);
void Remove(int position);
};
//EmpList.cpp
#include "StdAfx.h"
#include "EmpList.h"
EmpList::EmpList(void)
: EmpNode(NULL)
, m_length(0)
{
Init();
m_length = 0;
}
EmpList::EmpList(int nID, char *name,char *sex,int age)
{
Init();
EmpNode->empID = nID;
EmpNode->name = name;
EmpNode->sex = sex;
EmpNode->age = age;
m_length = 0;
}
EmpList::~EmpList(void)
{
Destroy();
}
bool EmpList::Init()
{
if(!(EmpNode = new Emp))
return false;
EmpNode->next = NULL;
return true;
}
bool EmpList::Destroy()
{
if(!Clear())
return false;
return true;
}
int EmpList::GetLength()
{
return m_length;
}
bool EmpList::AddItem(Emp *emp)
{
if(EmpNode == NULL)
return false;
int nPos = 0;
Emp *pTemp = EmpNode;
while(nPos < m_length && EmpNode->next != NULL)
{
pTemp = pTemp->next;
nPos++;
}
pTemp->next = emp;
emp->next = NULL;
m_length++;
return true;
}
bool EmpList::Clear()
{
if(EmpNode == NULL)
{
return false;
}
Emp *pTemp;
int nPos = -1;
while(nPos < m_length && EmpNode->next != NULL)
{
pTemp = EmpNode;
EmpNode = EmpNode->next;
delete pTemp;
nPos++;
}
return true;
}
bool EmpList::Insert(int postion, Emp *emp)
{
if(EmpNode == NULL)
return false;
Emp*pTemp = EmpNode;
int nPos = -1;
while(nPos < m_length && pTemp->next)
{
if(nPos == postion -1)
{
emp->next = pTemp->next;
pTemp->next = emp;
m_length++;
}
nPos++;
pTemp = pTemp->next;
}
return true;
}
Emp EmpList::GetItem(int position)
{
//if(EmpNode == NULL)
//return NULL;
int nPos = -1;
Emp *pTemp = NULL;
Emp *pTemp1 = EmpNode;
while(nPos < m_length && pTemp1->next)
{
if(nPos == position-1)
{
pTemp = pTemp1;
break;
}
else
{
nPos++;
pTemp1 = pTemp1->next;
}
}
return *pTemp;
}
void EmpList::Sort()
{
if(!EmpNode)
return;
for(int i=0; i<m_length;i++)
{
int b = i;
for(int j=i; j>=0; j--)
{
if(GetItem(b).empID < GetItem(j).empID)
{
Insert(b,&GetItem(j));
Remove(j);
b++;
}
}
}
}
void EmpList::Remove(int position)
{
if(EmpNode == NULL)
return;
int nPos = -1;
Emp *pTemp = EmpNode;
while(nPos < m_length && pTemp->next)
{
if(nPos == position)
{
pTemp->next = pTemp->next->next;
m_length--;
}
nPos++;
pTemp = pTemp->next;
}
}
void EmpList::ShowList()
{
if(!EmpNode->next)
return;
int nPos = 0;
Emp *pTemp = EmpNode->next;
for(int i=0; i<m_length; i++)
{
cout<<"第"<<nPos+1<<"个员工的信息:"<<endl;
cout<<"编号:"<<pTemp->empID<<endl;
cout<<"姓名:"<<pTemp->name<<endl;
cout<<"性别:"<<pTemp->sex<<endl;
cout<<"年龄:"<<pTemp->age<<endl;
pTemp = pTemp->next;
nPos++;
}
}
//StdAfx.h
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#ifndef _WIN32_WINNT // 允许使用特定于Windows XP 或更高版本的功能。
#define _WIN32_WINNT 0x0501 // 将此值更改为相应的值,以适用于Windows 的其他版本。
#endif
#include <stdio.h>
#include <tchar.h>
#include <iostream>
using namespace std;
typedef struct _employ
{
int empID;
char *name;
char *sex;
int age;
struct _employ *next;
}Emp;
// TODO: 在此处引用程序需要的其他头文件
// HomeWork.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "EmpList.h"
int _tmain(int argc, _TCHAR* argv[])
{
EmpList L;
Emp emp1,emp2,emp3;
emp1.empID = 1;
emp1.name = "mzs";
emp1.sex = "男";
emp1.age = 24;
emp2.empID = 6;
emp2.name = "aaa";
emp2.sex = "男";
emp2.age = 24;
emp3.empID = 4;
emp3.name = "ccc";
emp3.sex = "男";
emp3.age = 24;
//Emp emp1(1,"mzs","男",24);
//Emp emp2(2,"cl","男",24);
//Emp emp3(3,"jw","男",25);
L.AddItem(&emp1);
L.AddItem(&emp2);
L.AddItem(&emp3);
cout<<"****************************排序前***********************"<<endl;
L.ShowList();
L.Sort();
cout<<"****************************排序后***********************"<<endl;
L.ShowList();
return 0;
}
bool EmpList::Insert(int postion, Emp& emp)
{
if(EmpNode == NULL)
return false;
Emp* pTemp = NULL;
if(postion - 1 >= 0)
{
if(!(GetItem(postion-1,&pTemp)))
return false;
Emp* newNode = new Emp;
newNode = &emp;
newNode->next = pTemp->next;
pTemp->next = newNode;
m_length++;
return true;
}
else
{
Emp* newNode = new Emp;
newNode->name = emp.name;
newNode->age = emp.age;
newNode->empID=emp.empID;
newNode->sex=emp.sex;
newNode->next = EmpNode;
EmpNode = newNode;
m_length++;
return true;
}
}
请注意:
第一、在insert的else语句中,那个传过来的emp只是一个临时对象,insert返回后就被析构了。所以得自己new一个!手动赋值!
第二、在delete的时候,不同于free,会调用struct的析构函数,所以堆struct还要加一个析构函数!毕竟,struct里还有指针嘛!
//StdAfx.h
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#ifndef _WIN32_WINNT // 允许使用特定于 Windows XP 或更高版本的功能。
#define _WIN32_WINNT 0x0501 // 将此值更改为相应的值,以适用于 Windows 的其他版本。
#endif
#include <stdio.h>
#include <tchar.h>
#include <iostream>
using namespace std;
typedef struct _employ
{
int empID;
char *name;
char *sex;
int age;
struct _employ *next;
}Emp;
// TODO: 在此处引用程序需要的其他头文件
//EmpList.h
#pragma once
class EmpList
{
public:
EmpList(void);
~EmpList(void);
EmpList(int nID, char *name,char *sex,int age);
private:
Emp* EmpNode;
int m_length;
public:
bool Init();
bool Destroy();
bool Insert(int postion,Emp emp);
bool Clear();
void ShowList();
int GetLength();
bool AddItem(Emp *emp);
void Sort();
bool GetItem(int position,Emp **emp);
void Remove(int position);
};
//EmpList.cpp
#include "StdAfx.h"
#include "EmpList.h"
EmpList::EmpList(void)
: EmpNode(NULL)
, m_length(0)
{
Init();
m_length = 1;
}
EmpList::EmpList(int nID, char *name,char *sex,int age)
{
Init();
EmpNode->empID = nID;
EmpNode->name = name;
EmpNode->sex = sex;
EmpNode->age = age;
m_length = 1;
}
EmpList::~EmpList(void)
{
Destroy();
}
bool EmpList::Init()
{
if(!(EmpNode = new Emp))
return false;
EmpNode->next = NULL;
return true;
}
bool EmpList::Destroy()
{
if(!Clear())
return false;
return true;
}
int EmpList::GetLength()
{
return m_length;
}
bool EmpList::AddItem(Emp *emp)
{
if(EmpNode == NULL)
return false;
int nPos = 1;
Emp *pTemp = EmpNode;
while(nPos < m_length && EmpNode->next != NULL)
{
pTemp = pTemp->next;
nPos++;
}
pTemp->next = emp;
emp->next = NULL;
m_length++;
return true;
}
bool EmpList::Clear()
{
if(EmpNode == NULL)
{
return false;
}
Emp *pTemp;
while(EmpNode->next != NULL)
{
pTemp = EmpNode->next;
EmpNode->next = pTemp->next;
delete pTemp;
}
m_length = 0;
return true;
}
bool EmpList::Insert(int postion, Emp emp)
{
if(EmpNode == NULL)
return false;
Emp* pTemp = NULL;
if(postion - 1 >= 0)
{
if(!(GetItem(postion-1,&pTemp)))
return false;
Emp* newNode = new Emp;
newNode = &emp;
newNode->next = pTemp->next;
pTemp->next = newNode;
m_length++;
return true;
}
else
{
Emp* newNode = &emp;
newNode->next = NULL;
newNode->next = EmpNode;
EmpNode = newNode;
m_length++;
return true;
}
}
bool EmpList::GetItem(int position,Emp **emp)
{
if(EmpNode == NULL)
return false;
int nPos = 0;
Emp *pTemp = NULL;
Emp *pTemp1 = EmpNode;
while(nPos < m_length && pTemp1->next)
{
if(nPos == position)
{
break;
}
else
{
nPos++;
pTemp1 = pTemp1->next;
}
if(nPos != position)
return false;
}
*emp = pTemp1;
return true;
}
void EmpList::Sort()
{
if(!EmpNode)
return;
Emp *emp1 = new Emp;
Emp *emp2 = new Emp;
for(int i=0; i<m_length;i++)
{
int b = i;
for(int j=i+1; j>=0; j--)
{
if(!(GetItem(b,&emp1)&&GetItem(j,&emp2)))
return;
if(emp1->empID > emp2->empID)
{
//emp2->next = NULL;
Insert(j-1,*emp2);
Remove(j+1);
b++;
}
}
}
}
void EmpList::Remove(int position)
{
if(position < 1 || position > m_length)
return;
Emp *pTemp = NULL;
if(!GetItem(position - 1,&pTemp))
return;
Emp *pDel = NULL;
pDel = pTemp->next;
pTemp->next = pDel->next;
delete pDel;
m_length--;
}
void EmpList::ShowList()
{
if(!EmpNode->next)
return;
int nPos = 0;
Emp *pTemp = EmpNode;
for(int i=0; i<m_length; i++)
{
cout<<"第"<<nPos+1<<"个员工的信息:"<<endl;
cout<<"编号:"<<pTemp->empID<<endl;
cout<<"姓名:"<<pTemp->name<<endl;
cout<<"性别:"<<pTemp->sex<<endl;
cout<<"年龄:"<<pTemp->age<<endl;
pTemp = pTemp->next;
nPos++;
}
}
// HomeWork.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "EmpList.h"
int _tmain(int argc, _TCHAR* argv[])
{
EmpList L(100,"马俑","男",24);
Emp emp1,emp2,emp3;
emp1.empID = 1;
emp1.name = "mzs";
emp1.sex = "男";
emp1.age = 24;
emp2.empID = 6;
emp2.name = "aaa";
emp2.sex = "男";
emp2.age = 24;
emp3.empID = 4;
emp3.name = "ccc";
emp3.sex = "男";
emp3.age = 24;
//Emp emp1(1,"mzs","男",24);
//Emp emp2(2,"cl","男",24);
//Emp emp3(3,"jw","男",25);
L.AddItem(&emp1);
L.AddItem(&emp2);
L.AddItem(&emp3);
cout<<"****************************排序前***********************"<<endl;
L.ShowList();
L.Sort();
cout<<"****************************排序后***********************"<<endl;
L.ShowList();
return 0;
}
//StdAfx.h
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#ifndef _WIN32_WINNT // 允许使用特定于 Windows XP 或更高版本的功能。
#define _WIN32_WINNT 0x0501 // 将此值更改为相应的值,以适用于 Windows 的其他版本。
#endif
#include <stdio.h>
#include <tchar.h>
#include <iostream>
using namespace std;
typedef struct _employ
{
inline _employ():next(NULL),name(NULL),sex(NULL),empID(-1),age(-1){};
int empID;
char *name;
char *sex;
int age;
struct _employ *next;
}Emp;
// TODO: 在此处引用程序需要的其他头文件
//EmpList.h
#pragma once
class EmpList
{
public:
EmpList(void);
~EmpList(void);
EmpList(Emp& emp);
private:
Emp* EmpNode;
int m_length;
public:
bool Init();
bool Destroy();
bool Insert(int postion,Emp& emp);
bool Clear();
void ShowList();
int GetLength();
bool AddItem(Emp* emp);
void Sort();
bool GetItem(int position,Emp* &emp);
void Remove(int position);
};
//EmpList.cpp
#include "StdAfx.h"
#include "EmpList.h"
EmpList::EmpList(void)
: EmpNode(NULL)
, m_length(0)
{
Init();
}
EmpList::EmpList(Emp& emp)
{
Init();
EmpNode->next = &emp;
m_length++;
}
EmpList::~EmpList(void)
{
Destroy();
}
bool EmpList::Init()
{
if(!(EmpNode = new Emp))
return false;
m_length = 1;
return true;
}
bool EmpList::Destroy()
{
if(!Clear())
return false;
delete EmpNode;
m_length = 0;
return true;
}
int EmpList::GetLength()
{
return m_length-1;
}
bool EmpList::AddItem(Emp* emp)
{
if(EmpNode == NULL)
return false;
Emp *pTemp = EmpNode;
while(pTemp->next)
{
pTemp = pTemp->next;
}
pTemp->next = emp;
emp->next = NULL;
m_length++;
return true;
}
bool EmpList::Clear()
{
if(EmpNode == NULL)
{
return false;
}
EmpNode->next = NULL;
m_length = 1;
return true;
}
bool EmpList::Insert(int postion, Emp& emp)
{
if(EmpNode == NULL)
return false;
Emp* pTemp = NULL;
if(postion - 1 >= 0)
{
if(!(GetItem(postion-1,pTemp)))
return false;
emp.next = pTemp->next;
pTemp->next = &emp;
m_length++;
return true;
}
else
{
emp.next = EmpNode->next;
EmpNode->next = &emp;
m_length++;
return true;
}
}
bool EmpList::GetItem(int position,Emp* &emp)
{
if((EmpNode == NULL) || (EmpNode->next == NULL))
return false;
Emp *pTemp = EmpNode;
if((position > 0) && (position < m_length))
{
for(int i=0; i<position; i++)
{
pTemp = pTemp->next;
}
}
else
{
return false;
}
emp = pTemp;
return true;
}
void EmpList::Sort()
{
if(!EmpNode)
return;
Emp* emp1p = EmpNode;
Emp* emp1 = 0;
Emp* emp2p = 0;
Emp* emp2 = 0;
Emp* temp = 0;
for(int i=1; i<m_length;i++)
{
for(int j=i; j<m_length-1; j++)
{
emp1 = emp1p->next;
if(!GetItem(j, emp2p))
return;
emp2 = emp2p->next;
if(emp1->empID > emp2->empID)
{
if(emp2 != emp1->next)
{
temp = emp1->next;
emp1->next = emp2->next;
emp2->next = temp;
emp1p->next = emp2;
emp2p->next = emp1;
}
else
{
temp = emp2->next;
emp1p->next = emp2;
emp2->next = emp1;
emp1->next = temp;
}
}
}
if(!GetItem(i, emp1p))
{
return;
}
}
}
void EmpList::Remove(int position)
{
if(position < 1 || position >= m_length)
return;
Emp *pTemp = NULL;
if(!GetItem(position - 1,pTemp))
return;
Emp *pDel = NULL;
pDel = pTemp->next;
pTemp->next = pDel->next;
m_length--;
}
void EmpList::ShowList()
{
if(!EmpNode->next)
return;
Emp *pTemp = EmpNode->next;
for(int i=1; i<m_length; i++)
{
cout<<"第"<<i<<"个员工的信息:"<<endl;
cout<<"编号:"<<pTemp->empID<<endl;
cout<<"姓名:"<<pTemp->name<<endl;
cout<<"性别:"<<pTemp->sex<<endl;
cout<<"年龄:"<<pTemp->age<<endl;
pTemp = pTemp->next;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Emp emp;
emp.empID = 100;
emp.name = "马俑";
emp.sex = "男";
emp.age = 24;
EmpList L(emp);
Emp emp1,emp2,emp3;
emp1.empID = 1;
emp1.name = "mzs";
emp1.sex = "男";
emp1.age = 24;
emp2.empID = 6;
emp2.name = "aaa";
emp2.sex = "男";
emp2.age = 24;
emp3.empID = 4;
emp3.name = "ccc";
emp3.sex = "男";
emp3.age = 24;
//Emp emp1(1,"mzs","男",24);
//Emp emp2(2,"cl","男",24);
//Emp emp3(3,"jw","男",25);
L.AddItem(&emp1);
L.AddItem(&emp2);
L.AddItem(&emp3);
cout<<"****************************排序前***********************"<<endl;
L.ShowList();
L.Sort();
cout<<"****************************排序后***********************"<<endl;
L.ShowList();
system("pause");
return 0;
}
void EmpList::Sort()
{
if(!EmpNode)
return;
for(int i=0; i<m_length;i++)
{
int b = i;
for(int j=i+1; j<m_length; j++)
{
if(GetItem(b).empID < GetItem(j).empID)
{
Insert(b,&GetItem(j));
Remove(j);
b++;
}
}
}
}