Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <iostream>
- #define list struct spisok
- using namespace std;
- list
- {
- list *next;// следущий элемент списка
- int data;// данные
- list *null_element; // указатель на выделенный элемент
- };
- list *create(int data); // инициализация списка
- list *get_last(list* head); // поиск последнего элемента списка
- void add(list* head, int data); // добавление нового элемента
- void print(list* head); //вывод списка на экран
- void point(list* head); //присваивание полям "null_element" указатель на последний элемент спискa
- void print2(list* head); // вывод списка на экран с содержанием поля "null_element"
- void del_list(list* head);// удаление списка
- int main() {
- int a,c=1,n;
- cout << "Enter a first element: " << endl;
- cin >> a;
- n = 1;
- list* s = create(a); // голова списка
- while (c) {
- cout << "Do you want to add a new element?Yea(1)/No(0)" << endl;
- cin >> c;
- if (c != 0) {
- cout << "Enter a next element: " << endl;
- cin >> a;
- add(s, a);
- n++;
- }
- }
- print(s);
- cout << "\nNumber of elements in list N = " << n << endl;
- point(s);
- print2(s);
- del_list(s);
- return 0;
- }
- void add(list* head,int data)
- {
- list* s = get_last(head);
- list* t = new list;
- t->data = data;
- t->next = NULL;
- t->null_element = NULL;
- s->next = t;
- }
- void print(list* head)
- {
- list* t = head;
- while (t != NULL) {
- cout << t->data << " -> ";
- t = t->next;
- }
- }
- void point(list* head)
- {
- list* t = head;
- list* last = get_last(head);
- while (t->next != NULL) {
- t->null_element = last;
- t = t->next;
- }
- }
- void print2(list* head)
- {
- list* t = head;
- while (t->next != NULL) {
- cout << t->data << " -> " << t->null_element->data;
- cout << "\n|\n";
- t = t->next;
- }
- cout << t->data << endl;
- }
- void del_list(list* head)
- {
- while (head != NULL) {
- list* t = head;
- head = head->next;
- free(t);
- }
- }
- list* create(int data)
- {
- list* head = new list;
- head->data = data;
- head->next = NULL;
- head->null_element = NULL;
- return head;
- }
- list* get_last(list* head)
- {
- if (head == 0) {
- return NULL;
- }
- while (head->next) {
- head = head->next;
- }
- return head;
- }
Add Comment
Please, Sign In to add comment