单向链表求助
  • 板块学术版
  • 楼主TARGETMINE
  • 当前回复4
  • 已保存回复4
  • 发布时间2023/10/1 18:13
  • 上次更新2023/11/2 16:44:00
查看原帖
单向链表求助
935263
TARGETMINE楼主2023/10/1 18:13

https://www.luogu.com.cn/problem/B3631

#include <iostream>

using namespace std;

int n;
int caozuo;

struct Node
{
	int data;
	
	Node* next;	
	Node(int x)
	{
		data = x;
		next = NULL;
	}
};

void insert(Node* head,int x, int y)
{
	
	while(head)
	{
		if (head->data==x)
		{
			Node* insert_ = new Node(y);
			insert_->next = head->next;
			head->next = insert_;
		}
		
		head = head -> next;
	}
	
	
}

void show(Node* head)
{
	while (head)
	{
		cout << head->data << " ";
		head = head->next;
	}
	cout << endl;
}

int serch(Node* head, int x)
{
	while (head)
	{
		if (head->next == NULL)return 0;
		if (head->data == x)
		{
			return head->next->data;
		}
		head = head->next;
	}
	
	return 0;
}

Node* del(Node* head, int x)
{
	if (head == NULL) return NULL;
	
	Node* p = head;

	while (p->next)
	{
		Node *q = p->next;
		if (p->next->next == NULL)
		{
			p->next = NULL;
			delete q;
			return head;
		}
		if (p->data == x)
		{
			cout << 'a';
			
			p->next->next = p->next;
			delete q;
			return head;
		}
		p = p->next;	
	}	
	
	return head;
}


int main()
{
	cin >> n;
	
	
	Node* head = new Node(1);
	Node* tail = head;
	tail->next = new Node(3);
	tail = tail->next;
	tail->next = new Node(6);
	tail = tail->next;
	tail->next = new Node(9);
	tail = tail->next;

	del(head, 3);
	
	show(head);
	
//	for (int i=0;i<n;i++)
//	{
//		cin >> caozuo;
//		if (caozuo == 1)
//		{
//			insert(head, x, y);
//			
//		}
//		
//		
//	}
	
	
	return 0;
}

删除那一块出错了,刚学,太乱了,希望大佬解释一下

2023/10/1 18:13
加载中...