学生信息管理(单链表、带头结点)(数据结构入门)

版权声明:转载请注明 https://blog.csdn.net/qq_33831360/article/details/88621475

要求: 自己设计学生基本信息表 具备功能:创建、清空、销毁、插入、删除、按值查找、更新某人信息、指定位置查找、遍历。

中文乱码Orz 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
 
using namespace std;
 
typedef struct Item {
	char name[10];
	float score;
	long long ID;
}STD;
 
typedef struct Node {
	STD data;
	struct Node *next;
	Node():next(NULL) {}              
} *LinkList;
 
int InitLinkList(LinkList &L) { 
    L = new Node;
    LinkList P = L;
	puts("Please fill in the student's name, student ID, and score:"); 
	while (1) {
	  P->next = new Node;	
	  P = P->next;
	  cin >> P->data.name >> P->data.ID >> P->data.score;
	  puts("Whether to continue? (y/n)");
	  char opt; cin >> opt;
	  if (opt == 'n') break; 
	  puts("Please enter the next student's information:");
	}
	puts("Done!"); 
	return 1; 
}
 
int ClearLinkList(LinkList &L) {
	LinkList P = L->next,tmp = P;
    while(P != NULL) {
    	tmp = P->next;
    	free(P);
    	P = tmp;
	}
	L->next = NULL;
	puts("Done!");
	return 1;
}
 
int DestroyLinkList(LinkList &L) {
	LinkList P = L;
    while(L != NULL) {
    	P = L->next;
    	free(L);
    	L = P;
	}
	puts("Done!\nEnter '0' to exit.");
	return 1;
} 
 
int InsertLinkList(LinkList L,int pos,STD x) {
	LinkList P = L,tmp;
	int i = 0;
	while (i<pos-1 && P->next!=NULL) {
		i++;
		P = P->next;
	}
	if (i != pos-1) {
		puts("Invalid location!");
		return 0;
	}
	tmp = new Node;
	tmp->data = x;
	tmp->next = P->next;
	P->next = tmp;
	puts("Done!");
	return 1;
}
 
int DeleteLinkList(LinkList L,int pos) {
	LinkList P = L,tmp;
	int i = 0;
	while (i<pos-1 && P->next!=NULL) {
		i++;
		P = P->next;
	}
	if (i != pos-1 || P->next == NULL) {
		puts("Invalid location!");
		return 0;
	}
	tmp = P->next;
    P->next = tmp->next;
    free(tmp);
	puts("Done!");
	return 1;
}
 
int FindValLinkList(LinkList L,int ID) {
	LinkList P = L;
	while (P->next != NULL) {
		P = P->next;
		if (P->data.ID == ID) {
	 	   cout << "The person with ID " << ID << " is" << P->data.name << endl;
	 	   return 1;
	    }
	}
	puts("No such person!");
	return 0; 
} 
 
int UpdataLinkList(LinkList L,int pos,STD x) {
	LinkList P = L;
	int i = 0;
	while (i<pos && P->next!=NULL) {
		i++;
		P = P->next;
	}
	if (i != pos || pos <= 0) {
		puts("Invalid location");
		return 0;
	}
    P->data = x;
    puts("Done!");
    return 1;
}
 
int FindPosLinkList(LinkList L,int pos) {
	LinkList P = L;
	int i = 0;
	while (i<pos && P->next!=NULL) {
		i++;
		P = P->next;
	}
	if (i != pos || pos <= 0) {
		puts("Not found!");
		return 0;
	}
	cout << "Name:" << P->data.name << " ID:" << P->data.ID << " Score:" << P->data.score << endl; 
	return 1; 
} 
 
int TraversalLinkList(LinkList L) {
	if (L->next == NULL) {puts("It is an empty list!");return 0;}
	puts("All the student's information is as follows:"); 
	LinkList P = L->next;
	while (P != NULL) {
	  cout << "Name:" << P->data.name << " ID:" << P->data.ID << " Score:" << P->data.score << endl;
	  P = P->next; 
	}
	puts("Done!");
	return 1;
}
 
int main() {
	printf("Enter the following number to complete the operation:\n"
	 "1: Create a Linklist\n"
	 "2: Clear the Linklist\n"
	 "3: Destroy the Linklist\n"
	 "4: Insert a Node\n" 
	 "5: Delete a Node\n"
	 "6: Find by Student ID\n"
	 "7: Update Someone's Information\n" 
	 "8: Find by Specific Location\n" 
	 "9: Traverse the Linklist\n"
	 "0: Exit\n");
	int opt,p,id;
	LinkList L;
	STD x;
    while (1) {
    	cin >> opt;
    	switch(opt) {
    		case 0: return 0;
    		case 1: InitLinkList(L);      break;
    		case 2: ClearLinkList(L);     break;
    		case 3: DestroyLinkList(L);   break;
    		case 4: puts("Please enter the insertion location and the new student's name, student ID, score:");
			        cin >> p >> x.name >> x.ID >> x.score;
    		        InsertLinkList(L,p,x);
    		        break;
    		case 5: puts("Please enter the location of the deleted student:"); 
    		        cin >> p; 
			        DeleteLinkList(L,p);  
					break;
    		case 6: puts("Please enter ID of the student you are looking for:");
			        cin >> id;
			        FindValLinkList(L,id);
			        break;
			case 7: puts("Please enter the update location and the new student's name, student ID, score:");
			        cin >> p >> x.name >> x.ID >> x.score;
			        UpdataLinkList(L,p,x);
			        break;
			case 8: puts("Please enter the location of the student you are looking for:");
			        cin >> p;
			        FindPosLinkList(L,p);
			        break;
			case 9: TraversalLinkList(L); break;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_33831360/article/details/88621475