⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lininsertsort.cpp

📁 数据结构c++-书的一些源代码
💻 CPP
字号:
#include <iostream.h>
#include "datatype.h"
struct Node
{
	datatype data;
	Node *next;
	Node(Node *p = NULL): next(p){}
	Node(datatype item, Node *p = NULL): data(item), next(p){}
};

void LinInsertSort (datatype *a, int n, Node *list)
//用链表插入法对a[0]--a[n-1]排序
{
	Node *curr, *pre, *q;
	for(int i = 0; i < n; i++)
	{
		curr = list->next;
		pre = list;
		q = new Node(a[i]);
		while(curr != NULL && curr->data.key <= a[i].key)
		{
			pre = curr;
			curr = curr->next;
		}
 		q->next = pre->next;
		pre->next = q;
	}
}

void main(void)
{
	datatype test[6]={64,6,7,89,6,24};
	int n = 6, i;
	Node *list = new Node;
	LinInsertSort(test, n, list);
	Node *p = list->next;
	while(p != NULL)
	{
		cout << p->data.key << "  ";
		p = p->next;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -