#include <iostream>
#include <vector>
#include <math.h>
#include <string>
using namespace std;
vector<string> res;
vector<int> nums;
vector<int> eles;
/* Project 1
定义一个单向链表node, 每个节点值为int.
定义函数void create_linklist(…, int n)来生成链表,链表长度由参数n传入,生成的链表要作为参数返回,而不是当初函数返回值返回,链表节点值为1,2,3...n。
定义函数reverse_linklist(...), 对参数传入的链表进行逆转。
定义函数insert_node(node *pHead, int Index, int Value ), 在指定链表位置Index插入指定Value
定义函数delete_node(node *pHead, int Index),删除链表位置Index所在的节点
定义函数delete_linklist(), 删除整个链表
main()函数:
1.调用create_linklist()生成链表
2.调用insert_node()插入节点
3.调用delete_node()删除节点
4.调用reverse_linklist()逆转
5.打印链表所有值
6.结束程序工作
*/
struct node
{
int v;
node* n;
};
/* 链表长度由参数n传入,生成的链表要作为参数返回,而不是当初函数返回值返回,
链表节点值为1,2,3...n
*/
void create_linklist(node **pHead, int n)
{
node *head = NULL, *p, *tail = NULL;
int number = 0;
while(number < n)
{
number++;
p = (node *)malloc(sizeof(node));
if(head == NULL)
head = p;
else
tail->n = p;
p->v = number;
p->n = NULL;
tail = p;
}
*pHead = head;
}
//对参数传入的链表进行逆转
void reverse_linklist(node **pHead)
{
node* pPrevNode;
node* pNextNode;
if(NULL == pHead || NULL == *pHead)
return ;
pNextNode = (*pHead)->n;
(*pHead) ->n = NULL;
while(pNextNode){
pPrevNode = pNextNode;
pNextNode = pNextNode->n;
pPrevNode->n = *pHead;
*pHead = pPrevNode;
}
}
//在指定链表位置Index插入指定Value
// Index 为 1 ~ n 代表 1 ~ n 个节点
// Index = 0 即为头添加
void insert_node(node **pHead, int Index, int Value)
{
node *p,*p_of_ind;
node *newNode;
int j;
for(j = 1, p_of_ind = *pHead; NULL != p_of_ind && j < Index; j++, p_of_ind = p_of_ind->n);
p = j == Index ? p_of_ind : NULL;
if(NULL == p)
{
printf("Error insert index !\n");
}
newNode = (node *)malloc(sizeof(node));
newNode->v = Value;
newNode->n = p->n;
p->n = newNode;
}
void delete_node(node **pHead, int Index)
{
node *p = *pHead,*last_p;
int j = 1;
while (p->n && j < Index)
{
last_p = p;
p = p->n;
++j;
}
if (p == NULL || j > Index)
{
printf("Position Error\n");
}
node *temp = last_p->n;
last_p->n = temp->n;
free(p);
}
//删除整个链表
void delete_linklist(node **pHead)
{
node *header = *pHead;
node *p = NULL;
while(NULL != header)
{
p = header;
header = p->n;
free(p);
}
free(header);
pHead = NULL;
}
/* Project 2
编写一个函数int Calculate(node *pHead, int k);在链表数字之间插入+ 、- 或者什么都不插入,使得计算结果等于
给定的整数 K,返回所有可能性的个数。例如链表有数字 1, 2, 3, ..., 9 并且给
定的整数为 100,那么其中的一种可能性是: 1 + 2 + 34 – 5 + 67 – 8 + 9 = 100。
(不允许开辟额外的字符串空间,假设链表所有数字都大于 0 并且小于 10)
*/
void compute(vector<int> vec, int index, int target, string &s)
{
if (index == vec.size())
{
if (0 == target)
res.push_back(s + "=100");
return;
}
for (int i = 0; i < 2; i++)
{
if (i == 0)
{
string tempStr = s + "+" + to_string(vec[index]);
compute(vec, index + 1, target - vec[index], tempStr);
}
else if (i == 1)
{
string tempStr = s + "-" + to_string(vec[index]);
compute(vec, index + 1, target + vec[index], tempStr);
}
}
return;
}
void recursion(int index, int target)
{
if (index == 9)
{
string s = to_string(eles[0]);
compute(eles, 1, target - eles[0], s);
return;
}
for (int i = 1; i <= 9; i++)
{
if (index + i > 9)
break;
int temp = 0;
for (int j = 0; j < i; j++)
{
temp += nums[index + j] * pow(10, i - j - 1);
}
eles.push_back(temp);
recursion(index + i, target);
eles.pop_back();
}
return;
}
int Calculate(node *pHead, int k)
{
node *p;
int i;
for(i = 0, p = pHead; NULL != p ; i++, p = p->n)
{
nums.push_back(p->v);
}
recursion(0,k);
return res.size();
}
/* Project 3
定义一个聚类函数 cluster(int data[], int len, int radius);
data中的值没有重复,长度为len,
把按照数值的聚类进行分为n组,
对于组G中任意一个数值a,总是能在本组G中找到一个数值b, 使 |a-b| < radius .
在函数内部打印出所有n个组成员,分成n行输出
(要求:不能使用数组排序操作)
例如:
输入 data[] = { 1, 20, 89, 22, 72, 2,39, 3,56,86, 5, 93,13, 15, 18, 73, 79, 81, 25, 38, 43, 83,48, 52, 59,92,84,95,87 };
正确的屏幕输出为组及成员为(每行为一组,行之间顺序随意,组内成员顺序随意):
1, 2, 3, 5,
13, 15, 18, 20, 22, 25,
39, 38, 43, 48, 52, 56, 59,
73, 72,
79, 89, 92, 84, 95,87,86, 93,81, 83,
*/
void cluster(int data[], int len, int radius)
{
vector<int>hash;
int min = data[0];
for (int i = 0; i < len; i++)
{
if (data[i] < min)
min = data[i];
}
int offset = 0;
if (min < 0)
offset = -min;
for (int i = 0; i < len; i++)
{
int value = data[i]+offset;
if (value >= hash.size())
while (hash.size() <= value) hash.push_back(0);
hash[value] = 1;
}
vector<int>vec;
vector<vector<int>>vec_vec;
int num_0 = 0;
for (int i =0;i<hash.size();i++)
{
if (hash[i] == 0)
{
num_0++;
if (num_0 >= radius)
{
vec_vec.push_back(vec);
vec.clear();
num_0 = 0;
}
}
if (hash[i])
{
vec.push_back(i-offset);
num_0 = 0;
}
}
vec_vec.push_back(vec);
for (auto temp : vec_vec)
{
for (int i = 0; i < temp.size(); i++)
{
if (i == temp.size() - 1)
cout << temp[i] << endl;
else
cout << temp[i] << ',';
}
}
}
int main()
{
// Project 1
// 可将头尾指针及节点个数封装结构体,更方便操作
cout << "==========Project1=================" << endl;
node *pHead = nullptr;
// 1.调用create_linklist()生成链表
create_linklist(&pHead,9);
// 2.调用insert_node()插入节点
insert_node(&pHead,9,10);
// 3.调用delete_node()删除节点
delete_node(&pHead,10);
// 4.调用reverse_linklist()逆转
reverse_linklist(&pHead);
node *pTemp = pHead;
while (pTemp)
{
std::cout << pTemp->v << " ";
pTemp = pTemp->n;
}
cout << endl;
cout << "==========Project1=================" << endl;
cout << endl;
// Project 2
cout << "==========Project2=================" << endl;
delete_linklist(&pHead);
create_linklist(&pHead,9);
cout << Calculate(pHead,100) << endl;
cout << "==========Project2=================" << endl;
cout << endl;
// Project 3
cout << "==========Project3=================" << endl;
int data[] = { 1, 20, 89, 22, 72, 2,39, 3,56,86, 5, 93,13, 15, 18, 73, 79, 81, 25, 38, 43, 83,48, 52, 59,92,84,95,87 };
cluster(data, sizeof(data)/sizeof(int), 5);
/*
正确的输出为组及成员为(组内成员顺序随意):
1, 2, 3, 5,
13, 15, 18, 20, 22, 25,
39, 38, 43, 48, 52, 56, 59,
73, 72,
79, 89, 92, 84, 95,87,86, 93,81, 83,
*/
cout << "==========Project3=================" << endl;
return 0;
}
链表题
最新推荐文章于 2024-05-01 13:46:38 发布