c++ – C ++ singly linked list

Question:

Wrote a program to create a singly linked list

Menu:

Adding an element (by position number we add to the beginning, between elements, end)

List output

Removing an element There is a problem in adding between elements (the idea is that if there is already an element at the position of the added element, then the positions of the remaining elements to the next empty position will increase by 1), which does not happen when an element is added to an existing position, or disappears or is placed after.

PS I apologize for the design, I am applying for the first time, I did not really understand how the code is separated from the text, I manually translated it into the code.

#include "stdafx.h"
#include <iostream>
using namespace std;

struct data
{
int Pos;
int Val;
};

struct List
{
data data_of_el;
List *next;
};
int menu()
{
cout << "\n 1 : Add new element \n";
cout << " 2 : Show List \n";
cout << " 3 : Delete element \n";
cout << " 4 : Decide \n";
return 0;
}
int Add_to_beginning(int Pos, int Val, List **First)   //добавление в начало
{
List *el = new List;
el->data_of_el.Pos = Pos;
el->data_of_el.Val = Val;
el->next = *First;
*First = el;
return 0;
}
int Add_to_end(int Pos, int Val, List **First)   // добавление в конец
{
List *semisemi = *First;
List *el = new List;
el->data_of_el.Pos = Pos;
el->data_of_el.Val = Val;
while ((semisemi) && (Pos > semisemi->next->data_of_el.Pos))
{
    semisemi = semisemi->next;
}
semisemi->next = el;
return 0;

}
int Add_between(int Pos, int Val, List **First)     //добавление между элементами
{
List *semisemi = *First;
List *el = new List;
el->data_of_el.Pos = Pos;
el->data_of_el.Val = Val;
while ((semisemi) && (Pos > semisemi->data_of_el.Pos))
{
    semisemi = semisemi->next;
}
el = semisemi->next;
semisemi->next = el;
while ((Pos == semisemi->next->next->data_of_el.Pos) && (!semisemi->next->next->next))
{
    semisemi->next->next->data_of_el.Pos++;
    semisemi->next->next = semisemi->next->next->next;
}
return 0;
}
int Delete_el(int Pos, List **First)     //удалить элемент
{
if (!*First)
{
    return 0;
}
List *semi = *First;
while ((Pos > semi->data_of_el.Pos) && (!semi->next))
{
    semi = semi->next;
}
if (Pos = semi->data_of_el.Pos)
{
    if (semi == *First)         //начало
    {
        *First = semi->next;
        delete semi;
        return 0;
    }
    if (!semi->next)            //конец
    {
        semi = *First;
        while (!semi->next->next)
        {
            semi = semi->next;
        }
        delete(semi->next);
        semi->next = 0;
        return 0;
    }
    semi = *First;
    while (Pos > semi->next->data_of_el.Pos)
    {
        semi = semi->next;
    }
    semi->next = semi->next->next;
    List *semisemi = semi->next->next;
    delete (semi->next);
    semi->next = semisemi;
    return 0;
}
cout << "Element with this Position not exist\n";
return 0;
}
int ShowList(List **First)     // вывести список
{
List *show = *First;
if (!show)
{
    cout << " List is empty \n";
    return 0;
}
cout << " Position\t Value\n";
while (show)
{
    cout << show->data_of_el.Pos << "\t" << show->data_of_el.Val << "\n";

    show = show->next;
}
return 0;

}
int Add_elem(int Pos, int Val, List **First)
{
List *semi = *First;
cout << Pos;
if (!First)
{
    Add_to_beginning(Pos, Val, First);
    return 0;
}
if (Pos == 1)
{
    Add_to_beginning(Pos, Val, First);
    return 0;
}

while ((semi) && (Pos > semi->data_of_el.Pos))
{
    semi = semi->next;
}
if (!semi)
{
    Add_to_end(Pos, Val, First);
    return 0;
}
Add_between(Pos, Val, First);
return 0;
}
int main()
{
setlocale(LC_ALL, "Russian");
int ans;
int Pos, Val;

List* First = 0;
do
{
    menu();
    cout << " Enter you choice\n";
    cin.clear();
    _flushall();
    cin >> (ans);
    switch (ans)
    {
    case 1: cout << "Enter Position of new element\n";
                cin >> (Pos);
                if (Pos <= 0)
                {
                    cout << "Position can not be unpositive\n";
                    break;
                }
                cout << "Enter Value of new element\n";
                cin >> (Val);
                Add_elem(Pos, Val, &First);

        break;
    case 2:
        ShowList(&First);
        break;
    case 3:
        Delete_el(Pos, &First);
        break;
    }
}
while (ans != 0);
return 0;
}

Answer:

To change the positions, you need to add the appropriate code at the end of the functions, which will run to the end of the list and change these positions.

For adding:

el = el->next;
while(el)
{
 el->data_of_el.Pos++;
 el = el->next;
}

Accordingly, to remove:

el = el->next;
while(el)
{
 el->data_of_el.Pos--;
 el = el->next;
}
Scroll to Top