123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- struct MyStruct
- {
- int a;
- struct MyStruct* next;
- };
- typedef struct MyStruct s;
- s* create(int n) {
- s* start = malloc(sizeof(s));
- start->a = 1;
- start->next = NULL;
- s* p, * q;
- p = start;
- for (int i = 0; i < n - 1; i++) {
- q = malloc(sizeof(s));
- q->a = p->a + 1;
- p->next = q;
- p = q;
- }
- p->next = NULL;
- return start;
-
- }
- void show(s* list) {
- while (list) {
- if (list != NULL) {
- printf("%d ", list->a);
- list = list->next;
- }
-
- }
- printf("\n");
- }
- void delete(s* list) {
- s* p = list;
- while (p->next) {
- p = list->next;
- free(list);
- list = p;
- }
- }
- s* insert(s* list, s item, int k)
- {
- s* el = malloc(sizeof(s));//âûäåëÿåì ïàìÿòü ïîä íîâûé ýëåìåíò ñïèñêà
- el->a = item.a;//ïîìåùàåì â íåãî ïîëå èç ñòðóêòóðû
- if (k == 1)//åñëè ìû ìåíÿåì ïåðâûé ýëåìåíò
- {
- el->next = list;
- list = el;
- }
- else // åñëè ìåíÿåì íå ïåðâûé ýëåìåíò
- {
- s* start = list;//çàïîìèíàåì ãîëîâó ñïèñêà
- for (size_t i = 0; i < k - 1; i++)
- {
- list = list->next;//ñäâèãàåì íà k ïîçèöèé
- if (!(list->next))
- {
- printf("âû ââåëè èíäåêñ, ïðåâûøàþùèé ðàçìåðû ñïèñêà. ÌÛ âñòàâèì ýëåìåíò â êîíåö ñïèñêà\n");
- break;
- }
- }
- void* temp = list->next;//îáìåí àäðåñàìè
- list->next = el;
- el->next = temp;
- list = start;//âîçâðàùàåì ãîëîâó íà ìåñòî
- }
- return list;
- }
- void delete_el(s* list) {
- printf_s("Ââåäèòå íîìåð ýëåìåíòà ñïèñêà, êîòîðûé âû õîòèòå óäàëèòü\n");
- int n = 0;
- scanf_s("%d", &n);
- if (list == NULL) {
- return;
- }
- s* p = list;
- s* prev = NULL;
- while (p) {
- if (p->a == n) {
- if (prev == NULL) {
- // Remove head node
- list = p->next;
- }
- else {
- prev->next = p->next;
- }
- free(p);
- return;
- }
- prev = p;
- p = p->next;
- }
- }
- void change_element(s*list) {
- int c, b = 0;
- printf_s("Ââåäèòå ïåðâûé index äëÿ çàìåíû\n");
- scanf_s("%d", &c);
- printf_s("Ââåäèòå âòîðîé index äëÿ çàìåíû\n");
- scanf_s("%d", &b);
- if (list == NULL) {
- return;
- }
- s* p = list;
- s* prev = NULL;
- s* p1 = list;
- s* prev1 = NULL;
-
- while (p && p->a != c) {
- prev = p;
- p = p->next;
- }
-
- while (p1 && p1->a != b) {
- prev1 = p1;
- p1 = p1->next;
- }
- if (!p || !p1) {
- return;
- }
- if (prev == NULL) {
- // p1 is the head node
- list = p1;
- }
- else {
- prev->next = p1;
- }
- if (prev1 == NULL) {
- // p2 is the head node
- list = p;
- }
- else {
- prev1->next = p;
- }
- s* temp = p->next;
- p->next = p1->next;
- p1->next = temp;
- }
- int main() {
- system("chcp 1251>nul");
- int siz = 0;
- printf_s("Ââåäèòå ðàçìåð ñïèñêà\n");
- scanf_s("%d", &siz);
- s* list1 = create(siz);
- show(list1);
- int v = 0;
- printf("Âûáåðèòå, ÷òî áóäåì äåëàòü:\n1.Âñòàâèòü çíà÷åíèå.\n2.Óäàëèòü ñïèñîê.\n3.Óäàëèòü ýëåìåíò èç ñïèñêà\n4.Ïîìåíÿòü ìåñòàìè ýëåìåíòû ñïèñêà\n");
- scanf_s("%d", &v);
- if(v == 1){
- int value = 0;
- int mesto = 0;
- printf_s("Ââåäèòå æåëàåìîå çíà÷åíèå\n");
- scanf_s("%d", &value);
- s item = { value,NULL };
- printf_s("Ââåäèòå ìåñòî, êóäà âû õîòèòå âñòàâèòü çíà÷åíèå\n");
- scanf_s("%d", &mesto);
- list1 = insert(list1, item, mesto-1);
- show(list1);
-
- }
- else if (v == 2) {
- delete(list1);
- show(list1);
- }
- else if (v == 3) {
- delete_el(list1);
- show(list1);
- }
- else if (v == 4) {
- change_element(list1);
- show(list1);
- }
-
-
- return 0;
- }
|