博客前的感想:
大家好,我又来了,其实数据结构的博客我是想的比较单纯,就把平时的做的实验自己写的代码分享出来,现在我们要做的就是数据结构中比较基础的结构,链表
下面是代码块:
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
typedef struct node
{
int pwd;
int label;
node *next;
}Node,*no;
int n;
void create(no &h)
{
//int n;
no original;
cout << "请输入有几个人:" << endl;
cin >> n;
h = new node;
original = h;
cout << "请输入第" << 1 << "个人的密码:" << endl;
cin >> h->pwd;
h->label = 1;
for (int i = 1; i < n; i++)
{
h->next = new node;
h = h->next;
cout << "请输入第" << i + 1 << "个人的密码:" << endl;
cin >> h->pwd;
h->label = i + 1;
}
h->next = original;
//h = h->next;
}
int main()
{
no hwq;
no temp;
no pre;
//no t;
int m = 1;
create(hwq);
pre = hwq; //处于最后一个结构体
hwq = hwq->next;
//t = hwq;
for (int i = 0; i < n; i++)
{
no t=pre->next;
for (int j = 0; j < m-1; j++) //要比密码少循环一次
{
t = t->next;
pre = pre->next;
}
cout << t->label << " ";
pre->next = t->next;
m = t->pwd;
delete t;
}
cout << endl;
system("pause");
return 0;
}
相信大家对链表都掌握的比较熟悉,其中要注意的就是要构成循环链表。双向链表就在动态创建链表的时候加一个pre指针指向前一个,双向链表就形成了,你可以借助画图来加深理解。