C++循环链表删除,计数从下一个节点开始

C++ circular linked list deletion, counts start at the next node

本文关键字:下一个 节点 开始 循环链表 删除 C++      更新时间:2023-10-16

我不知道如何在循环链接列表中删除。例如,头部是B,因此列表将从"B,C,D,E,A"开始。第一个节点将始终从 1-5 中选择一个数字,我使用计数器不断减少该数字,因此例如,如果"B"选择了 3,计数将开始到下一个节点"C",因此从"C"开始计数,我们将不得不消除"E",一旦"E"被消除。

新的头又名选择器将启动到消除节点之后的下一个节点,因此下一组节点将变为"A,B,C,D",此功能必须重复,直到只有 1 个最后一个站立节点。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include <string>
#include <ctime>
using namespace std;
/*
* Node Declaration
*/
struct node
{
string name;
struct node *next;
};
node *t, *head;
node *ex;
int paper;
int ctr = 5;
int num;
void create(string sname)
{
node *n = new node;
n->name = sname;
if (head == NULL)
{
head = n;
t = n;
}
else
{
t->next = n; // connects the nodes
t = t->next; // moves the connecter to the t= last
}
t->next = head;
}

/*
* Deletion of element from the list
*/
void delete_element(string value)
{
}
//Display Circular Link List
void display()
{
node *temp = new node;
temp = head;
if ((head == NULL) && (t == NULL))
{
}
for (int j = 1; j <= 5; j++)
{
cout << temp->name << "n";
temp = temp->next;
}
}
void firstpic()
{
srand(time(NULL));
paper = rand() % 5 + 1;
int fctr = 5;
bool p1 = 0, p2 = 0, p3 = 0, p4 = 0, p5 = 0;
if (paper == 1)
{
create("A");//1
fctr--;
}
else if (paper == 2)
{
create("B");//2
p2 = 1;
}
else if (paper == 3)
{
create("C");//2
p3 = 1;
}
else if (paper == 4)
{
create("D");//2
p4 = 1;
}
else if (paper == 5)
{
create("E");//2
p5 = 1;
}
if (p1) 
{
create("B");
create("C");
create("D");
create("E");
}
else if (p2) 
{
create("C");
create("D");
create("E");
create("A");
}
else if (p3) 
{
create("D");
create("E");
create("A");
create("B");
}
else if (p4) 
{
create("E");
create("A");
create("B");
create("D");
}
else if (p5) 
{
create("A");
create("B");
create("C");
create("D");
}
}
void drawn()
{
node *holder = head;
ex = holder->next;
cout << holder->name << " has drawn: " <<num <<endl;
}
int main()
{
head == NULL;
t == NULL;
srand(time(NULL));
firstpic();
display();
num = rand() % ctr + 1;
drawn();
system("pause>nul");
return 0;
}

以下是可能适合您需求的内容:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;
/*
* Node Declaration
*/
struct node
{
string name;
struct node *next;
};
node *tail, *head;
void addNode(string sname)
{
node *n = new node;
n->name = sname;
if (head == NULL) {
head = n;
tail = n;
} else {
tail->next = n;    // connects the nodes
tail = tail->next; // moves the connecter to the t= last
}
tail->next = head;
}

/*
* Deletion of element from the list
*/
void removeNode(string value)
{
// no elements
if (head == NULL) {
return;
}
node *n = head;
node *prev = tail;
// 1 element
if(n == prev) {
if(n->name == value) {
delete n;
head = tail = NULL;
}
return;
}
bool found = false;
// search
do {
if(n->name == value) {
found = true;
break;
}
prev = n;
n = n->next;
} while (n != head);
// no such element
if(!found) {
return;
}
prev->next = n->next;
if(n == head) {
head = n->next;
} else if(n == tail) {
tail = prev;
}
delete n;
}
void displayList()
{
if (head == NULL) {
cout << "empty!" << endl;
return;
}
node *n = head;
do {
cout << n->name << "n";
n = n->next;
} while (n != head);
cout << endl;
}
void createList()
{
const int count = 5;
std::string names[count] = {"A", "B", "C", "D", "E"};
int nameIndex = rand() % count;
for(int i = 0; i<count; ++i) {
nameIndex += 1;
nameIndex %= count;
addNode(names[nameIndex]);
}
}
int main()
{
srand(time(NULL));
head = NULL;
tail = NULL;
createList();
displayList();
removeNode("A");
displayList();
removeNode("A");
displayList();
removeNode("E");
displayList();
removeNode("B");
displayList();
removeNode("C");
displayList();
removeNode("D");
displayList();
system("pause>nul");
return 0;
}

请注意,在节点删除期间,应处理以下情况:

  • 无元素案例
  • 要删除的最后 1 个元素
  • 头部删除
  • 尾部删除

此外,永远不要像你在firstpic()职能中那样做事:这是一种痛苦的方式。drawn()函数似乎没有做任何有意义的事情,但它超出了问题的范围。