Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <locale.h>
- typedef struct List
- {
- int info;
- struct List* next;
- } List;
- List *First, *Last, *el, *tmp,*tmp2;
- void delete(int k)
- {
- List *el, * tmp;
- int count=0;
- if (First == NULL)
- return;
- if (k < 0)
- return;
- for (el = First; el != NULL; el = el->next)
- count++;
- if (k > count)
- {
- printf("\nТакого элемента не существует\n");
- return;
- }
- if (First->next != NULL && k == 1) {
- First = First->next;
- return;
- }
- if (k == 2)
- {
- el= First->next;
- First->next = el->next;
- }
- el = First;
- tmp= el->next;
- count = 2;
- for (el = First; el != NULL; el = el->next,count++)
- if (count == k ) {
- tmp = el->next;
- el->next = tmp->next;
- }
- }
- void push(int inf)
- {
- struct List* el;
- el = (struct List*)malloc(sizeof(struct List));
- if (el == NULL)
- {
- printf("Не удалось выделить память под элемент списка.\n");
- return;
- }
- el->info = inf;
- el->next = NULL;
- if (First == NULL)
- First = Last = el;
- else
- {
- Last->next = el;
- Last = Last->next;
- }
- }
- int main(void)
- {
- int inf, k = 0;
- setlocale(LC_ALL, "Russian");
- First = Last = NULL;
- scanf("%d", &k);
- printf("Введите элементы списка (окончание ввода -- любой символ, кроме цифры):\n");
- while (scanf("%d", &inf) != 0)
- {
- push(inf);
- }
- delete(k);
- printf("\n");
- for (el = First; el != NULL; el = el->next)
- {
- printf("%d ", el->info);
- }
- for (el = First; el != NULL;)
- {
- tmp = el;
- el = el->next;
- free(tmp);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement