123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- //Ñòðóêòóðà âåðøèíû ñïèñêà
- typedef struct Vertex {
- int id; //Ïîðÿäêîâûé íîìåð ñïèñêà
- int data; //Äàííûå ýåëìåíòà ñïèñêà
- struct Vertex* next; //Óêàçàòåëü íà ñëåäóþùèé ýëåìåíò ñïèñêà
- } Vertex;
- Vertex* create_node(int id, int data) {
- Vertex* new_node = (Vertex*)malloc(sizeof(Vertex));
- if (!new_node) {
- printf("Îøèáêà âûäåëåíèÿ ïàìÿòè.");
- exit(1);
- }
- new_node->id = id;
- new_node->data = data;
- new_node->next = NULL;
- return new_node;
- }
- //Ôóíêöèÿ äëÿ äîáàâëåíèÿ ýëåìåíòà â êîíåö ñïèñêà
- void add_vertex(Vertex** head, int id, int data) {
- Vertex* new_vertex = create_node(id, data);
- if (*head == NULL)
- {
- *head = new_vertex;
- }
- else {
- Vertex* temp = *head;
- while (temp->next != NULL) {
- temp = temp->next;
- }
- temp->next = new_vertex;
- }
- }
- //Ôóíêöèÿ äëÿ óäàëåíèÿ âñåãî ñïèñêà
- void delete_list(Vertex** head) {
- Vertex* current = *head;
- Vertex* next = NULL;
- while (current != NULL) {
- next = current->next;
- free(current);
- current = next;
- }
- *head = NULL;
- printf("Ñïèñîê óäàëåí \n");
- }
- //Ôóíêöèÿ äëÿ âñòàâêè íîâîãî ýëåìåíòà â ñïèñîê ïî ïîðÿäêîâîìó íîìåðó
- void insert_to_vertex(Vertex** head, int position, int id, int data) {
- Vertex* new_vertex = create_node(id, data);
- if (position == 0) {
- new_vertex->next = *head;
- *head = new_vertex;
- return;
- }
- Vertex* temp = *head;
- for (int i = 0; temp != NULL && i < position - 1; i++) {
- temp = temp->next;
- }
- if (temp == NULL) {
- printf("Ïîçèöèÿ íå íàéäåíà \n");
- free(new_vertex);
- return;
- }
- new_vertex->next = temp->next;
- temp->next = new_vertex;
- }
- //Ôóíêöèÿ äëÿ óäàëåíèÿ ýëåìåíòà èç ñïèñêà ïî ïîðÿäêîâîìó íîìåðó
- void delete_vertex(Vertex** head, int id) {
- Vertex* temp = *head;
- Vertex* prev = NULL;
- //åñëè óäàëÿåìûé ýëåìåíò - ïåðâûé
- if (temp != NULL && temp->id == id) {
- *head = temp->next;
- free(temp);
- return;
- }
- //Ïîèñê ýëåìåíòà äëÿ óäàëåíèÿ
- while (temp != NULL && temp->id != id) {
- prev = temp;
- temp = temp->next;
- }
- if (temp == NULL) {
- printf("Ýëåìåíåíò ñ id %d íå íàéäåí \n", id);
- return;
- }
- //Óäàëåíèå óçëà
- prev->next = temp->next;
- free(temp);
- }
- //Ôóíêöèÿ äëÿ çàìåíû äâóõ ýëåìåíòîâ ìåñòàìè
- void swap_vertexes(Vertex** head, int id1, int id2)
- {
- if (id1 == id2) return; //Åñëè id îäèíàêîâûå, îáìåí íå òðåáóåòñÿ
- Vertex* prevX = NULL, * currX = *head;
- Vertex* prevY = NULL, * currY = *head;
- //Ïîèñê ïåðâîãî ýëåìåíòà
- while (currX && currX->id != id1) {
- prevX = currX;
- currX = currX->next;
- }
- //Ïîèñê âòîðîãî ýëåìåíòà
- while (currY && currY->id != id2) {
- prevY = currY;
- currY = currY->next;
- }
- //Åñëè áû õîòÿ áû îäèí ýëåìåíò íå íàéäåí
- if (!currX || !currY) {
- printf("Îäèí èëè îáà ýëåìåíòà ñ id %d è %d íå íàéäåíû \n", id1, id2);
- return;
- }
- //Åñëè currX íå ÿâëÿåòñÿ ïåðâîé âåðøèíîé
- if (prevX != NULL)
- prevX->next = currY;
- else //åñëè currX - ýòî ïåðâàÿ âåðøèíà
- *head = currY;
- //Åñëè curryY íå ÿâë ïåðâîé âåðøèíîé
- if (prevY != NULL)
- prevY->next = currX;
- else //Åñëè curryY - ýòî ïåðâàÿ âåðøèíà
- *head = currX;
- //Îáìåí next óêàçàòåëÿìè
- Vertex* temp = currX->next;
- currX->next = currY->next;
- currY->next = temp;
- }
- //Ôóíêöèÿ äëÿ îòîáðàæåíèÿ ñïèñêà
- void print_list(Vertex* head) {
- Vertex* temp = head;
- while (temp != NULL) {
- printf("ID: %d, Data: %d \n", temp->id, temp->data);
- temp = temp->next;
- }
- }
- int main()
- {
- system("chcp 1251>null");
- Vertex* head = NULL;
- //Ñîçäàíèå ñïèñêà
- add_vertex(&head, 1, 10);
- add_vertex(&head, 2, 20);
- add_vertex(&head, 3, 30);
- add_vertex(&head, 4, 40);
- add_vertex(&head, 5, 50);
- add_vertex(&head, 6, 60);
- add_vertex(&head, 7, 70);
- printf("Èñõîäíûé ñïèñîê:\n");
- print_list(head);
- //Âñòàâêà
- printf("\nÂñòàâêà ýëåìåíòà ñ ID 6 íà ïîçèöèþ 0: \n");
- insert_to_vertex(&head, 0, 6, 60);
- print_list(head);
- //Óäàëåíèå
- printf("\nÓäàëåíèå ýëåìåíòà ñ ID 0: \n");
- delete_vertex(&head, 6);
- print_list(head);
- //Çàìåíà
- printf("\nÇàìåíà ýëåìåíòîâ ñ ID 2 è 4: \n");
- swap_vertexes(&head, 1, 2);
- print_list(head);
- //Óäàëåíèå âñåãî ñïèñêà
- delete_list(&head);
- return 0;
- }
|