C++学习之链表

1.1创建与遍历链表

#include<iostream>
using namespace std;

struct Student {
	long number;
	float score;
	Student*next;
};

Student * head;//定义头结点

Student*Create() {
	Student*pS;
	Student*pEnd;
	pS = new Student;
	cin >> pS->number >> pS->score;
	head = NULL;
	pEnd = pS;

	while (pS->number != 0) {
		if (head == NULL)
			head = pS;//如果头结点内容为空,则将其指针指向首结点
		else
			pEnd->next = pS;
		pEnd=pS;
		pS = new Student;
		cin >> pS->number >> pS->score;
	}
	pEnd->next = NULL;
	delete pS;
	return head;
}
void ShowList(Student*head) {
	cout << "now the items of list are.\n";
	while (head) {
		cout << head->number << "," << head->score << endl;
		head = head->next;
	}
}
int main() {
	ShowList(Create());
}

创建三个结构体指针:head, pEnd, pS ,用来表示头结点,和两个用于传递和连接的结点指针。当 pS 指向的结点被赋值之后, pEnd 便会将目前指向的结点的尾指针指向 pS ,之后再将自身指向 pS 用于取代,之后 pS 重新分配空间,指向下一个将要被赋值的地方,完成传递。

1.2 删除链表结点

#include<iostream>
using namespace std;

struct Student {
	long number;
	float score;
	Student*next;
};

Student * head;//定义头结点

Student*Create() {
	Student*pS;
	Student*pEnd;
	pS = new Student;
	cin >> pS->number >> pS->score;
	head = NULL;
	pEnd = pS;

	while (pS->number != 0) {
		if (head == NULL)
			head = pS;
		else
			pEnd->next = pS;
		pEnd=pS;
		pS = new Student;
		cin >> pS->number >> pS->score;
	}
	pEnd->next = NULL;
	delete pS;
	return head;
}
void ShowList(Student*head) {
	cout << "now the items of list are.\n";
	while (head) {
		cout << head->number << "," << head->score << endl;
		head = head->next;
	}
}

void Delete(Student*head, long number) {
	Student*p;
	if (!head) {
		cout << "\nList null!\n";
		return;
	}
	if (head->number == number) {
		p = head;
		head = head->next;
		delete p;
		cout << number << " the head of list have been deleted.\n";
		return;
	}
	for (Student*pGuard = head; pGuard->next; pGuard = pGuard->next) {
		if (pGuard->next->number == number) {
			p = pGuard->next;
			pGuard->next = p->next;
			delete p;
			cout << number << " have been deleted.\n";
			return;
		}
	}
	cout << number << " is not found!\n";
}

int main() {
	Create();
	Delete(head, 54);
	ShowList(head);
}

1.3插入链表结点

#include<iostream>
using namespace std;

struct Student {
	long number;
	float score;
	Student*next;
};

Student * head;//定义头结点

Student*Create() {
	Student*pS;
	Student*pEnd;
	pS = new Student;
	cin >> pS->number >> pS->score;
	head = NULL;
	pEnd = pS;

	while (pS->number != 0) {
		if (head == NULL)
			head = pS;
		else
			pEnd->next = pS;
		pEnd=pS;
		pS = new Student;
		cin >> pS->number >> pS->score;
	}
	pEnd->next = NULL;
	delete pS;
	return head;
}
void ShowList(Student*head) {
	cout << "now the items of list are.\n";
	while (head) {
		cout << head->number << "," << head->score << endl;
		head = head->next;
	}
}

void Insert(Student*head, Student*stud) //在原链表按数据从小到大排列的情况下依次序插入数据
{
	if (head == NULL) {
		head = stud;
		stud->next = NULL;
		return;
	}
	if (head->number > stud->number) {
		stud->next = head;
		head = stud;
		return;
	}
	Student*pGuard = head;
	while (pGuard->next&&pGuard->next->number < stud->number)
		pGuard = pGuard->next;
	stud->next = pGuard->next;
	pGuard->next = stud;

}

int main() {
	Student ps;
	ps.number = 36;
	ps.score = 3.8;
	head=Create();
	Insert(head,&ps);//head是全局变量
	ShowList(head);
}

1.4 Josephus 问题

// Josephus 问题
#include<iostream>
#include<iomanip>
using namespace std;
struct jose {
	int code;
	jose*next;
};
int main() {
	int numOfBoys, interval;
	cout << "Please ubput the number of boys,\n"//小孩数
		<< "         interval of counting:\n";//数小孩个数
	cin >> numOfBoys >> interval;
	//建立小孩结构数组
	jose*pJose = new jose[numOfBoys];
	jose*pCurrent = pJose;//当前结点指针(第一个数组元素pJose[0])
	//初始化结构数组:构成环链、小孩编号、输出编号
	int itemsInLine = 0;//输出项数
	for (int i = 1; i <= numOfBoys; i++) {
		pCurrent->next = pJose + i % numOfBoys;
		pCurrent->code = i;
		pCurrent = pCurrent->next;//循环给数组赋值,并且链接,构成环形链表
		if (itemsInLine++ % 10 == 0)//输出格式:每10个数字换行
			cout << endl;
		cout << setw(4) << i;//输出格式,4个字符靠右显示
	}
	itemsInLine = 0;//格式
	jose*pivot;//定义哨兵指针
	pCurrent = &pJose[numOfBoys - 1];//做好数的准备,若以第一个元素为起始
	while (pCurrent->next != pCurrent) {//若只剩最后一个元素,即获胜者,括号中判定为false,环中只一个元素,则尾指针指向首
		for (int j = 0; j < interval; j++) {
			pivot = pCurrent;
			pCurrent = pivot->next;//传递
		}
		if (itemsInLine++ % 10 == 0)
			cout << endl;
		cout << setw(4) << pCurrent->code;
		pivot->next = pCurrent->next;
		pCurrent = pivot;//点到的小孩脱链
	}
	cout << "\n\nthe winner is "
		<< pCurrent->code << endl;
	delete[]pJose;
	return 0;
}

在C++中,setw(int n)用来控制输出间隔。//转

设置输出几个字符的格式(靠右),比如int i = 10,你要输出i如果setw(20),那么 i 的前面有18个空格。

例如:cout<<'s'<<setw(8)<<'a'<<endl;则在屏幕显示sa//s与a之间有7个空格,setw()只对其后面紧跟的输出产生作用,如上例中,表示'a'共占8个位置,不足的用空格填充。

若输入的内容超过setw()设置的长度,则按实际长度输出。setw()默认填充的内容为空格,可以setfill()配合使用设置其他字符填充。

cout<<setfill('*')<<setw(5)<<'a'<<endl;则输出:****a //4个*和字符a共占5个位置。

猜你喜欢

转载自blog.csdn.net/Perce_Issac/article/details/82820612
今日推荐