Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- using namespace std;
- // обязательно!
- struct Node {
- int item;
- Node*next;
- Node (int a) {
- item = a;
- next = NULL;
- }
- Node(int a, Node*n) {
- item = a;
- next = n;
- }
- };
- //
- // Вывод списка
- void printlist(Node*head) {
- for (; head!=NULL; head = head->next) {
- cout << head->item << "->";
- }
- }
- // Добавляем в конец списка
- void addEnd(Node*&head, int a) {
- Node*temp = new Node(a);
- if (head == NULL) {
- head = temp;
- return;
- }
- Node *current = head;
- while (current->next != NULL) {
- current = current->next;
- }
- current->next = temp;
- }
- // Стирание списка
- void deleteList(Node*&head) {
- Node*h = head;
- Node*temp;
- while (h) {
- temp = h;
- h = h->next;
- delete temp;
- }
- head = NULL;
- }
- //Уберем узел - элемент списка
- int deleteNode(Node*&head, int a) {
- Node*temp;
- if (head->item == a) {
- temp = head;
- head = head->next;
- delete temp;
- }
- Node*h;
- h->next != NULL;
- for (h = head; h->next != NULL && h->next->item != a; h = h->next);
- if (h->next == NULL) return 1;
- temp = h->next;
- h->next = temp->next;
- delete temp;
- return 0;
- }
- int main()
- {
- // Инициализируем и создаем список
- Node*head = NULL;
- Node*temp;
- for (int i = 1; i < 10; i++) {
- temp = new Node(i, head);
- head = temp;
- }
- //
- cout << "\n";
- // Создание другого списка, правильно переворачиваем его
- Node*head1 = NULL;
- Node*temp1;
- Node*prev = NULL;
- for (int i = 1; i < 10; i++) {
- temp1 = new Node(i);
- if (head1 == NULL) {
- head1 = temp1;
- }
- else prev->next = temp1;
- prev = temp1;
- }
- //
- addEnd(head, 10);
- printlist(head);
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement