Добрый день. Писал на паре код в феврале, а сейчас уже всё подзабыл. Помогите написать функцию с удалением 3 его элемента в односвязном списке, желательно с пояснениями, заранее спасибо.
Код:
#include <iostream>;
#include <conio.h>;
#include <stdio.h>
#include <tchar.h>
using namespace std;
int n;
struct Data
{
int a;
};
struct Node
{
Data d;
Node* next;
};
void Print(Node* head)
{
cout << "\n\t List << " << endl;
while (head)
{
cout << head->d.a << ' ';
head = head->next;
}
cout << "NULL" << endl;
}
void Print1(Node* head)
{
cout << "\n \t New List <<" << endl;
while (head)
{
cout << head->d.a << ' ';
head = head->next;
}
cout << "NULL" << endl;
}
void Add_end(Node* head, Data x)
{
Node* q = new Node;
q->d.a = x.a;
Node* t = head;
while (t->next) t = t->next;
t->next = q;
q->next = NULL;
}
void Init_List(Node*& head, Data& d)
{
cout << "Enter the number of elements in List ->" << ' ';
cin >> n;
cout << "\nEnter" << ' ' << 1 << ' ' << "elements ->" << ' ';
head = new Node;
cin >> head->d.a;
head->next = NULL;
for (int i = 2; i <= n; i++)
{
cout << "Enter" << ' ' << i << ' ' << "elements ->" << ' ';
cin >> d.a;
Add_end(head, d);
}
cout << endl;
Print(head);
}
void Delete(Node* &head, Data &k)
{
cout << "Delete third element ? (y/n) ";
cin >> k.a;
Node* t = head;
if (t->d.a == k.a)
{
head = head->next->next;
delete t;
Print1(head);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Node* head; Data d;
Init_List(head, d);
Delete(head, d);
_getch();
return 0;
}