C++ 链表插入/删除 //ConsoleApplication1.cpp:定义控制台应用程序的入口点。 // #包括“stdafx.h” #包括 使用名称空间std; 结构节点{ int数据; 节点*下一步; }; Node*head=NULL; 整数大小; Node*tail=NULL; void printLinkedList(){ 节点*搜索=头部; if(head==NULL){ 下一步; 尺寸=尺寸+1; } cout-next=NULL; if(head==NULL) { 头=新节点; 返回; } 否则{ 节点*电流=头部; while(当前->下一步!=NULL){ 当前=当前->下一步; } 当前->下一步=新节点; } } void insertNode(int n,int位置){ Node*newNode=新节点; newNode->data=n; newNode->next=NULL; int size=sizelinekedlist(); 如果(位置=0){ if(head==NULL){ 头=新节点; } 否则{ 新建节点->下一步=头部; 头=新节点; } } else if(位置==大小){ 附加节点(n); } 否则{ 节点*prevNode=getNode(位置-1); Node*nextNode=getNode(位置); prevNode->next=newNode; newNode->next=nextNode; } } void deleteNode(int位置){ 节点*当前节点; int size=sizelinekedlist(); 如果(大小==0){ 返回; } 如果(位置==0){ 当前节点=头部->下一步; head=当前节点; } 否则如果(位置==尺寸-1){ getNode(位置-1)->next=NULL; 删除getNode(位置); } 否则{ getNode(位置-1)->next=getNode(位置+1); 删除getNode(位置); } } //在VC中仅通过指针创建动态数组++ void makeArray(){ int*m=NULL; int n; 库特 sizelkedList如果列表为空,则无法正常工作(它不能返回0) 在不同的范围内(在主范围内,以及在deleteNode内),您将size用作不同的变量。这很容易混淆,尽管并非完全错误
在C++ 链表插入/删除 //ConsoleApplication1.cpp:定义控制台应用程序的入口点。 // #包括“stdafx.h” #包括 使用名称空间std; 结构节点{ int数据; 节点*下一步; }; Node*head=NULL; 整数大小; Node*tail=NULL; void printLinkedList(){ 节点*搜索=头部; if(head==NULL){ 下一步; 尺寸=尺寸+1; } cout-next=NULL; if(head==NULL) { 头=新节点; 返回; } 否则{ 节点*电流=头部; while(当前->下一步!=NULL){ 当前=当前->下一步; } 当前->下一步=新节点; } } void insertNode(int n,int位置){ Node*newNode=新节点; newNode->data=n; newNode->next=NULL; int size=sizelinekedlist(); 如果(位置=0){ if(head==NULL){ 头=新节点; } 否则{ 新建节点->下一步=头部; 头=新节点; } } else if(位置==大小){ 附加节点(n); } 否则{ 节点*prevNode=getNode(位置-1); Node*nextNode=getNode(位置); prevNode->next=newNode; newNode->next=nextNode; } } void deleteNode(int位置){ 节点*当前节点; int size=sizelinekedlist(); 如果(大小==0){ 返回; } 如果(位置==0){ 当前节点=头部->下一步; head=当前节点; } 否则如果(位置==尺寸-1){ getNode(位置-1)->next=NULL; 删除getNode(位置); } 否则{ getNode(位置-1)->next=getNode(位置+1); 删除getNode(位置); } } //在VC中仅通过指针创建动态数组++ void makeArray(){ int*m=NULL; int n; 库特 sizelkedList如果列表为空,则无法正常工作(它不能返回0) 在不同的范围内(在主范围内,以及在deleteNode内),您将size用作不同的变量。这很容易混淆,尽管并非完全错误,c++,data-structures,singly-linked-list,C++,Data Structures,Singly Linked List,在deleteNode中,此序列不起作用: // ConsoleApplication1.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; struct Node { int data; Node* next; }; Node* h
deleteNode
中,此序列不起作用:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* head = NULL;
int size;
Node* tail = NULL;
void printLinkedList() {
Node *search = head;
if (head == NULL) {
cout << "linkedlist is empty" << endl;
}
else {
while (search != NULL){
cout << search->data << endl;
search = search->next;
}
}
}
int sizeLinkedList() {
size = 1;
Node* current = head;
while (current->next != NULL) {
current = current->next;
size = size + 1;
}
cout << size << endl;
return size;
}
Node *getNode(int position){
Node *current = head;
for (int i = 0; i<position; i++)
{
current = current->next;
}
return current;
}
void appendNode(int n) {
Node *newNode = new Node; //creating new node
newNode->data = n;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
return;
}
else {
Node *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void insertNode(int n, int position) {
Node *newNode = new Node;
newNode->data = n;
newNode->next = NULL;
int size = sizeLinkedList();
if (position = 0){
if (head == NULL) {
head = newNode;
}
else{
newNode->next = head;
head = newNode;
}
}
else if (position == size) {
appendNode(n);
}
else {
Node *prevNode = getNode(position-1);
Node *nextNode = getNode(position);
prevNode->next = newNode;
newNode->next = nextNode;
}
}
void deleteNode(int position) {
Node *currentNode;
int size = sizeLinkedList();
if (size == 0) {
return;
}
if (position == 0) {
currentNode = head->next;
head = currentNode;
}
else if (position == size-1) {
getNode(position - 1)->next = NULL;
delete getNode(position);
}
else {
getNode(position - 1)->next = getNode(position+1);
delete getNode(position);
}
}
//making a dynamic array only via pointers in VC++
void makeArray() {
int* m = NULL;
int n;
cout << "how many entries are there?"<<endl;
cin >> n;
m = new int[n];
int temp;
for (int x = 0; x < n; x++){
cout << "enter item:"<< x+1<< endl;
cin >> temp;
*(m + x) = temp;
}
for (int x = 0; x < n; x++){
cout << x+1 + ":" << "There is item: "<<*(m+x) << endl;
}
delete[]m;
}
int main() {
int x;
//makeArray();
appendNode(1);
appendNode(2);
appendNode(32);
appendNode(55);
appendNode(66);
//insertNode(2, 0);
printLinkedList();
deleteNode(3);
printLinkedList();
sizeLinkedList();
cin >> x;
}
将position
之前的节点上的next
指针设置为NULL将干扰下一行中getNode(position)
的尝试,因为它基于next
遍历列表。解决方法是将这两行反向
deleteNode
中的最后一个序列将无法工作,因为您正在修改下一个指针:
else if (position == size-1) {
getNode(position - 1)->next = NULL;
delete getNode(position);
}
这里的解决方案如下:
else {
getNode(position - 1)->next = getNode(position+1);
delete getNode(position); // this list traversal will skip the node to delete!
}
insertNode
函数main
fixed中:
else {
currentNode = getNode(position);
getNode(position - 1)->next = getNode(position+1);
delete currentNode;
}
#包括
使用名称空间std;
结构节点{
int数据;
节点*下一步;
};
Node*head=NULL;
int size=0;
Node*tail=NULL;
void printLinkedList(){
节点*搜索=头部;
if(head==NULL){
cout next!=空){
当前=当前->下一步;
尺寸=尺寸+1;
}
}
cout-next=NULL;
大小++;
if(head==NULL)
{
头=新节点;
返回;
}
否则{
节点*电流=头部;
while(当前->下一步!=NULL){
当前=当前->下一步;
}
当前->下一步=新节点;
}
}
void insertNode(int n,int位置){
Node*newNode=新节点;
newNode->data=n;
newNode->next=NULL;
如果(位置==0){
新建节点->下一步=头部;
头=新节点;
}
else if(position==sizelinekedlist()){
附加节点(n);
}
否则{
节点*prevNode=getNode(位置-1);
Node*nextNode=getNode(位置);
prevNode->next=newNode;
newNode->next=nextNode;
}
}
void deleteNode(int位置){
节点*当前节点;
int my_size=sizelkedlist();
如果((我的大小==0)| |(位置>我的大小)){
返回;
}
如果(位置==0){
当前节点=头部->下一步;
head=当前节点;
}
否则如果(位置==尺寸-1){
删除getNode(位置);
getNode(位置-1)->next=NULL;
}
否则{
currentNode=getNode(位置);
getNode(位置-1)->next=getNode(位置+1);
删除当前节点;
}
}
//在VC中仅通过指针创建动态数组++
void makeArray(){
int*m=NULL;
int n;
coutif(position=0)
应该是if(position=0)
您错过了if(position=0)
如上面评论中所述。现已修复。我以前的更新尚未在main
中取消注释insertNode
,但与OP的代码匹配,并且insertNode
尚未修复。它现在似乎正在为OP的测试用例工作。
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* head = NULL;
int size = 0;
Node* tail = NULL;
void printLinkedList() {
Node *search = head;
if (head == NULL) {
cout << "linkedlist is empty" << endl;
}
else {
while (search != NULL){
cout << search->data << endl;
search = search->next;
}
}
}
int sizeLinkedList() {
size = 0;
if (head->next != NULL){
size = 1;
Node* current = head;
while (current->next != NULL) {
current = current->next;
size = size + 1;
}
}
cout << size << endl;
return size;
}
Node *getNode(int position){
Node *current = head;
for (int i = 0; i<position; i++)
{
current = current->next;
}
return current;
}
void appendNode(int n) {
Node *newNode = new Node; //creating new node
newNode->data = n;
newNode->next = NULL;
size++;
if (head == NULL)
{
head = newNode;
return;
}
else {
Node *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void insertNode(int n, int position) {
Node *newNode = new Node;
newNode->data = n;
newNode->next = NULL;
if (position == 0){
newNode->next = head;
head = newNode;
}
else if (position == sizeLinkedList()) {
appendNode(n);
}
else {
Node *prevNode = getNode(position-1);
Node *nextNode = getNode(position);
prevNode->next = newNode;
newNode->next = nextNode;
}
}
void deleteNode(int position) {
Node *currentNode;
int my_size = sizeLinkedList();
if ((my_size == 0) || (position > my_size)) {
return;
}
if (position == 0) {
currentNode = head->next;
head = currentNode;
}
else if (position == size-1) {
delete getNode(position);
getNode(position - 1)->next = NULL;
}
else {
currentNode = getNode(position);
getNode(position - 1)->next = getNode(position+1);
delete currentNode;
}
}
//making a dynamic array only via pointers in VC++
void makeArray() {
int* m = NULL;
int n;
cout << "how many entries are there?"<<endl;
cin >> n;
m = new int[n];
int temp;
for (int x = 0; x < n; x++){
cout << "enter item:"<< x+1<< endl;
cin >> temp;
*(m + x) = temp;
}
for (int x = 0; x < n; x++){
cout << x+1 + ":" << "There is item: "<<*(m+x) << endl;
}
delete[]m;
}
int main() {
int x;
//makeArray();
appendNode(1);
appendNode(2);
appendNode(32);
appendNode(55);
appendNode(66);
insertNode(2, 0);
printLinkedList();
deleteNode(3);
printLinkedList();
sizeLinkedList();
}