c++四种链表实现学生信息管理

学生信息管理

(1)单链表实现

#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::ostream;
using std::istream;


typedef struct student {
	int no;
	string name;
	char sex;
	int age;
	string major;
}Elemtype;

typedef struct Node {
	Elemtype data;
	Node *next;
}Node, *LinkList;

void copy(Elemtype &s1, const Elemtype &s2) {
	s1.no = s2.no;
	s1.name = s2.name;
	s1.sex = s2.sex;
	s1.age = s2.age;
	s1.major = s2.major;
}

void CreateList_L(LinkList &L, const Elemtype a[], const int &n) {
	L = new Node; L->next = NULL;
	LinkList s;
	for (int i = n - 1; i >= 0; --i) {
		s = new Node;
		copy(s->data, a[i]);
		s->next = L->next;
		L->next = s;
	}
}

const ostream & operator << (ostream & os, const  Elemtype &e) {
	os << e.no << " " << e.name << " "
		<< e.sex << " " << e.age << " "
		<< e.major << endl;
	return os;
}

void Show_L(const LinkList& L) {
	LinkList p = L->next;
	while (p) {
		cout << p->data;
		p = p->next;
	}
}

const int ListLength_L(const LinkList &L) {
	LinkList p = L->next;
	int length = 0;
	while (p) {
		p = p->next;
		length++;
	}
	return length;

}

const int LocateElem(const LinkList &L, const Elemtype &e) {
	LinkList p = L->next;
	int j = 1;
	while (p && p->data.no != e.no) {
		p = p->next;
		j++;
	}
	if (p) return j;
	else return 0;
}

LinkList GetElem(const LinkList &L, const Elemtype &e) {
	LinkList p = L->next;
	while (p && p->data.no != e.no) {
		p = p->next;
	}
	return p;
}

void ListInsert_L(LinkList &L, const int & i, const Elemtype &e) {
	LinkList p = L;
	int j = 1;
	while (p && j < i) {
		p = p->next;
		j++;
	}
	if (!p) { cout << "插入位置不合法!\n"; exit(1); }
	else {
		LinkList s = new Node;
		copy(s->data, e);
		s->next = p->next;
		p->next = s;
	}
}

void ListDelete_L(LinkList &L, const int &i) {
	LinkList p = L;
	int j = 1;
	while (p && j < i) {
		p = p->next;
		j++;
	}
	if (!p || !p->next) { cout << "删除位置不合法!\n"; exit(1); }
	else {
		LinkList q = p->next;
		p->next = q->next;
		delete q;
	}
}

void ListDestory(LinkList &L) {
	LinkList p = L, q;
	while (p) {
		q = p;
		p = p->next;
		delete q;
	}
	L = NULL;
}

const istream& operator>>(istream& is, Elemtype &e) {
	is >> e.no >> e.name >> e.sex >> e.age >> e.major;
	return is;
}

void Create(LinkList &L) {
	int n;
	cout << "请输入学生人数:";
	cin >> n;
	cout << "请输入每位学生的信息:\n";
	cout << "学号,姓名,性别,年龄,专业:\n";
	Elemtype Stu[100];
	for (int i = 0; i < n; i++) cin >> Stu[i];
	CreateList_L(L, Stu, n);
	cout << "学生信息如下:\n";
	Show_L(L);
}

void Locate(const LinkList &L) {
	Elemtype s;
	cout << "请输入需要查询学生的学号:";
	cin >> s.no;
	LinkList p = GetElem(L, s);
	if (!p) { cout << "没有此学号的学生!\n"; }
	else cout << p->data;
}

void Delete(LinkList &L) {
	Elemtype s;
	cout << "请输入需要删除学生的学号:";
	cin >> s.no;
	int index = LocateElem(L, s);
	if (index == 0) cout << "没有此学号的学生!\n";
	else {
		ListDelete_L(L, index);
		Show_L(L);
	}
}

void Insert(LinkList &L) {
	int no;
	cout << "请输入需要插入节点:";
	cin >> no;
	Elemtype s;
	cout << "请输入需插入学生的学号,姓名,性别,年龄,专业:\n";
	cin >> s;
	ListInsert_L(L, no, s);
	cout << "学生信息如下:\n";
	Show_L(L);
}

void Exit(LinkList &L) {
	if (L != NULL) ListDestory(L);
}

int main() {
	LinkList list = NULL;
	int choice = 0;
	do {
		cout << "*******************学生信息管理************************\n";
		cout << "*******************1------建立************************\n";
		cout << "*******************2------查询************************\n";
		cout << "*******************3------删除************************\n";
		cout << "*******************4------插入************************\n";
		cout << "*******************0------退出************************\n";
		cout << "请选择(0-4):";
		cin >> choice;
		switch (choice) {
		case 1: {Create(list); break; }
		case 2: {Locate(list); break; }
		case 3: {Delete(list); break; }
		case 4: {Insert(list); break; }
		case 0: {Exit(list); break; }
		default: cout << "输入错误!\n" << endl;

		}

	} while (choice);
	return 0;
}

(2) 循环链表

#include <iostream>
#include <ostream>


using std::cout;
using std::cin;
using std::endl;
using std::ostream;

typedef struct student {
	int no;
}Elemtype;

typedef struct Node {
	Elemtype data;
	Node *next;
}Node,*LinkList;

//创建空循环链表
int ListInit(LinkList &L) {
	L = new Node;
	if (L == NULL) return false;
	else {
		L->next = L;
		return true;
	}

}

//创建循环链表
void ListCreate(LinkList &L, const Elemtype a[], const int &n) {
	L = new Node;
	LinkList s;
	if (L == NULL) { cout << "申请内存失败!\n"; exit(1); }
	L->next = L;
	for (int i = n - 1; i >= 0; --i) {
		s = new Node;
		s->data = a[i];
		s->next = L->next;
		L->next = s;
	}
}

const ostream & operator << (ostream & os, const  Elemtype &e) {
	os << e.no << endl;
	return os;
}

void ListShow(const LinkList &L) {
	LinkList p = L->next;
	do { cout << p->data.no << " "; p = p->next; } while (p != L);
	cout << endl;
}

void Creat(LinkList &L) {
	int n;
	cout << "请输入数据个数:";
	cin >> n; Elemtype *a = new Elemtype[n];
	for (int i = 0; i < n; ++i) {
		cin >> a[i].no;
	}
	ListCreate(L, a, n);
	cout << "学生信息如下:\n" << endl;
	ListShow(L);
}

void ListIsempty(const LinkList &L) {
	if (L->next = L) cout << "该链表为空" << endl;
	else cout << "该链表不为空" << endl;
}

//插入节点
void ListInsert(LinkList &L, const int &i, const Elemtype &e) {
	LinkList p = L;
	int j = 1;
	while (p->next != L && j < i) {
		p = p->next;
		j++;
	}
	if (j < i && p->next == L) {
		cout << "插入位置不合法!" << endl; exit(1);
	}
	else {
		LinkList s = new Node;
		if (s == NULL) { cout << "分配内存失败\n"; exit(1); };
		s->data.no = e.no ;
		s->next = p->next;
		p->next = s;
		
	}
}

//删除节点
void ListDelete(LinkList &L, const int &i) {
	LinkList p = L;
	int j = 1;
	while (j < i && p->next->next != L) {
		j++;
		p = p->next;
	}
	if (j < i && p->next ->next != L) { cout << "删除位置不合法\n"; exit(1); }
	else {
		LinkList q = p->next;
		p->next = q->next;
		delete q;
	}
}

void Delete(LinkList &L) {
	int i;
	cout << "请输入需要删除的节点:";
	cin >> i;
	ListDelete(L, i);
	cout << "数据信息如下:\n";
	ListShow(L);
	cout << endl;
}

void Insert(LinkList &L) {
	cout << "请输入需要插入的位置:\n";
	int no; cin >> no;
	Elemtype e; cout << "请输入插入的数据:\n"; cin >> e.no;
	ListInsert(L, no, e);
	cout << "插入后的数据为:" << endl;
	ListShow(L);
	

}

void ListLocate(const LinkList &L, const int &i) {
	LinkList p = L->next;
	int j = 1;
	while (p->next != L && j < i) {
		j++;
		p = p->next;
	}
	if (j < i && p->next == L) { cout << "未找到该节点\n"; exit(1); }
	cout << "你所查询的数据如下:\n";
	cout << p->data.no << endl;
}

void Locate(const LinkList &L) {
	cout << "请输入想要查询的节点:";
	int i; cin >> i;
	ListLocate(L, i);
}

void ListDestory(LinkList &L) {
	LinkList p = L->next;
	while (p != L) {
		LinkList q = p;
		p = p->next;
		delete q;
	}
	L = NULL;
}

void Exit(LinkList &L) {
	ListDestory(L);
	exit(1);
}

int main(void) {
	LinkList list = NULL;
	int choice = 0;
	do {
		cout << "*******************学生信息管理************************\n";
		cout << "*******************1------建立************************\n";
		cout << "*******************2------查询************************\n";
		cout << "*******************3------删除************************\n";
		cout << "*******************4------插入************************\n";
		cout << "*******************0------退出************************\n";
		cout << "请选择(0-4):";
		cin >> choice;
		switch (choice) {
		case 1: {Creat(list); break; }
		case 2: {Locate(list); break; }
		case 3: {Delete(list); break; }
		case 4: {Insert(list); break; }
		case 0: {Exit(list); break; }
		default: cout << "输入错误!\n" << endl;

		}

	} while (choice);
	return 0;
	
}
















(3)双向链表

#include <iostream>


using std::cout;
using std::endl;
using std::cin;



typedef struct student {
	int no;
}Elemtype;

typedef struct Node {
	Elemtype data;
	Node *prior;
	Node *next;
}Node ,*LinkList;


void ListInit(LinkList &L) {
	L = new Node;
	if (L == NULL) { cout << "内存申请不成功!\n"; exit(1); }
	L->prior = L->next = NULL;
}

void ListShow(const LinkList &L) {
	LinkList p = L->next;
	cout << "链表数据为:\n";
	while (p) { cout << p->data.no << " "; p = p->next; }
	cout << endl;
}

void ListCreat(LinkList &L, Elemtype a[], const int &n) {
	L = new Node;
	if (L == NULL) { cout << "内存申请不成功!\n"; exit(1); }
	L->prior = L->next = NULL;
	LinkList s ;
	for (int i = n - 1; i >= 0; --i) {
		s = new Node;
		if (s == NULL) { cout << "内存申请不成功!\n"; exit(1); }
		s->data.no = a[i].no;
		s->next = L->next;
		s->prior = L;
		L->next = s;
	}
	
}

void Create(LinkList &L) {
	cout << "请输入数据个数:";
	int n; cin >> n;
	Elemtype *a = new Elemtype[n];
	cout << "请输入数据信息:\n";
	for (int i = 0; i < n; i++) {
		cin >> a[i].no;
	}
	ListCreat(L, a, n);
	ListShow(L);
}

void ListInsert(LinkList &L, const int &i, const Elemtype &e) {
	LinkList p = L;
	int j = 1;
	while (j < i && p ) {
		j++;
		p = p->next;
	}
	if ( !p ) { cout << "未找到该节点!\n"; return; }
	else {
		LinkList s = new Node;
		if (s == NULL) { cout << "内存申请不成功!\n"; return; }
		s->data.no = e.no;
		s->next = p->next;
		if (p->next) p->next->prior = s;
		s->prior = p;
		p->next = s;
	}
}

void Insert(LinkList &L) {
	cout << "请输入要插入的节点位置:";
	int i; cin >> i;
	cout << "请输入插入的数据:\n";
	Elemtype e;
	cin >> e.no;
	ListInsert(L, i, e);
	ListShow(L);
}

void ListDelete(LinkList &L, const int &i) {
	LinkList p = L;
	int j = 1;
	while (j < i && p) {
		j++;
		p = p->next;
	}
	if (!p && !p->next ) { cout << "没有找到该节点!\n"; exit(1); }
	else {
		LinkList s = p->next ;
		if(s->next) s->next->prior = p;
		p->next = s->next;
		delete s;
	}
}

void Delete(LinkList &L) {
	cout << "请输入要删除的节点位置:";
	int i; cin >> i;
	ListDelete(L, i);
	ListShow(L);
}

void ListLocate(const LinkList &L, const int &i) {
	LinkList p = L->next;
	int j = 1;
	while (j < i && p) {
		p = p->next;
		j++;
	}
	if (!p) { cout << "未找到该节点!\n"; exit(1); }
	else cout << p->data.no;
	cout << endl;
}

void Locate(const LinkList &L) {
	cout << "请输入要查询的节点位置:";
	int i; cin >> i;
	ListLocate(L, i);

}

void Exit(LinkList &L) {
	LinkList p = L->next,q;
	while (p) {
		q = p;
		p = p->next;
		delete q;
	}
	L = NULL;
}


int main() {
	LinkList list = NULL;
	int choice = 0;
	do {
		cout << "*******************学生信息管理************************\n";
		cout << "*******************1------建立************************\n";
		cout << "*******************2------查询************************\n";
		cout << "*******************3------删除************************\n";
		cout << "*******************4------插入************************\n";
		cout << "*******************0------退出************************\n";
		cout << "请选择(0-4):";
		cin >> choice;
		switch (choice) {
		case 1: {Create(list); break; }
		case 2: {Locate(list); break; }
		case 3: {Delete(list); break; }
		case 4: {Insert(list); break; }
		case 0: {Exit(list); break; }
		default: cout << "输入错误!\n" << endl;

		}

	} while (choice);
	return 0;
}

(4)双向循环链表

#include <iostream>


using std::cout;
using std::cin;
using std::endl;


typedef struct student {
	int no;
}Elemtype;

typedef struct Node {
	Elemtype data;
	Node *prior;
	Node *next;
}Node,*LinkList;

void LinkInit(LinkList &L) {
	L = new Node;
	if (L == NULL) { cout << "内存申请不成功!\n"; exit(1); }
	L->prior = L;
	L->next = L;
}

void LinkCreat(LinkList &L, const Elemtype a[], const int &n) {
	LinkList s;
	L = new Node;
	if (L == NULL) { cout << "内存申请不成功!\n"; exit(1); }
	L->next = L->prior = L;
	for (int i = n - 1; i >= 0; --i) {
		s = new Node;
		if (s == NULL) { cout << "内存申请不成功!\n"; exit(1); }
		s->data.no = a[i].no;
		s->prior = L;
		s->next = L->next;
		if (L->prior == L) L->prior = s;
		if(L->next != L) L->next->prior = s;
		L->next = s;


	}
}

void LinkShow(const LinkList &L) {
	cout << "数据信息如下:\n";
	LinkList p = L->next;
	while (p != L) {
		cout << p->data.no << " ";
		p = p->next;
	}
	cout << endl;
}

void Create(LinkList &L) {
	cout << "请输入数据个数:";
	int n; cin >> n;
	Elemtype *a = new Elemtype[n];
	for (int i = 0; i < n; ++i) cin >> a[i].no;
	LinkCreat(L, a, n);
	LinkShow(L);
	

}

void LinkInsert(LinkList &L, const int &i, const Elemtype &e) {
	LinkList p = L;
	int j = 1;
	while (j < i && p->next != L) {
		j++;
		p = p->next;
	}
	if (j < i && p->next == L) { cout << "未找到该节点!\n"; exit(1); }
	else {
		LinkList s = new Node;
		if (s == NULL) { cout << "内存申请不成功!\n"; exit(1); }
		s->data.no = e.no;
		s->next = p->next;
		s->prior = p;
		if (p->next != L) p->next->prior = s;
		p->next = s;
	}
}

void Insert(LinkList &L) {
	cout << "请输入需要插入的节点位置:";
	int i; cin >> i;
	cout << "请输入需要插入的信息:\n";
	Elemtype e;
	cin >> e.no;
	LinkInsert(L, i, e);
	LinkShow(L);
}

void LinkDelete(LinkList &L, const int &i) {
	LinkList p = L;
	int j = 1;
	while (j < i && p->next != L) {
		p = p->next;
		j++;
	}
	if (p->next == L) { cout << "删除位置不合法!\n"; exit(1); }
	else {
		LinkList q = p->next ;
		p->next = q->next;
		q->next->prior = p;
		delete q;
	}
}

void Delete(LinkList &L) {
	cout << "请输入要删除的节点位置:";
	int i; cin >> i;
	LinkDelete(L, i);
	LinkShow(L);
}

void Exit(LinkList &L) {
	LinkList p = L,q;
	do {
		q = p;
		p = p->next;
		delete q;
	} while (p != L);
	L = NULL;
}

void LinkLocate(const LinkList &L, const int &i) {
	int j = 1;
	LinkList p = L->next;
	while (j < i && p->next != L) {
		j++;
		p = p->next;
	}
	if (j < i && p->next == L) { cout << "未找到该节点!\n"; exit(1); }
	else { cout << p->data.no; }
	cout << endl;
}

void Locate(LinkList &L) {
	cout << "请输入要查询的节点:";
	int i; cin >> i;
	LinkLocate(L, i);
}

int main() {
	LinkList list = NULL;
	int choice = 0;
	do {
		cout << "*******************学生信息管理************************\n";
		cout << "*******************1------建立************************\n";
		cout << "*******************2------查询************************\n";
		cout << "*******************3------删除************************\n";
		cout << "*******************4------插入************************\n";
		cout << "*******************0------退出************************\n";
		cout << "请选择(0-4):";
		cin >> choice;
		switch (choice) {
		case 1: {Create(list); break; }
		case 2: {Locate(list); break; }
		case 3: {Delete(list); break; }
		case 4: {Insert(list); break; }
		case 0: {Exit(list); break; }
		default: cout << "输入错误!\n" << endl;

		}

	} while (choice);
	return 0;
}

阅读更多
想对作者说点什么? 我来说一句

c++链表 实现学生信息管理

2010年10月15日 3KB 下载

没有更多推荐了,返回首页

不良信息举报

c++四种链表实现学生信息管理

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭