Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://vk.com/evgenykravchenko0
- ___ ___ ___
- / /\ ___ / /\ / /\
- / /:/_ /__/\ / /:/_ / /:/_
- / /:/ /\ \ \:\ / /:/ /\ / /:/ /\
- / /:/ /:/_ \ \:\ / /:/_/::\ / /:/ /:/_
- /__/:/ /:/ /\ ___ \__\:\ /__/:/__\/\:\ /__/:/ /:/ /\
- \ \:\/:/ /:/ /__/\ | |:| \ \:\ /~~/:/ \ \:\/:/ /:/
- \ \::/ /:/ \ \:\| |:| \ \:\ /:/ \ \::/ /:/
- \ \:\/:/ \ \:\__|:| \ \:\/:/ \ \:\/:/
- \ \::/ \__\::::/ \ \::/ \ \::/
- \__\/ ~~~~ \__\/ \__\/
- ___
- /__/\ ___ ___
- \ \:\ / /\ / /\
- \ \:\ / /:/ / /:/
- _____\__\:\ /__/::\ /__/::\
- /__/::::::::\ \__\/\:\__ \__\/\:\__
- \ \:\~~\~~\/ \ \:\/\ \ \:\/\
- \ \:\ ~~~ \__\::/ \__\::/
- \ \:\ /__/:/ /__/:/
- \ \:\ \__\/ \__\/
- \__\/
- #include <stdio.h>
- #include "list.h"
- // œÓθÁÓ‚‡ÚÂθÒ͇ˇ ÒÚÛÍÚÛ‡, ÍÓÚÓ‡ˇ ÒÍ˚‚‡ÂÚ ÏÂı‡ÌËÁÏ ı‡ÌÂÌˡ ‰‡ÌÌ˚ı.
- struct List {
- int* array; // ‰Ë̇Ï˘ÂÒÍËÈ Ï‡ÒÒË‚, ‚ ÍÓÚÓÓÏ ·Û‰ÛÚ ‡ÌËÚ¸Òˇ ‚Ò ‰‡ÌÌ˚Â
- size_t length; // ‡ÁÏ χÒÒË‚‡ array
- };
- // Ô˜‡Ú¸ ˝ÎÂÏÂÌÚÓ‚ ÒÔËÒ͇
- void PrintList(List *lst)
- {
- size_t i;
- for (i = 0; i < lst->length; i ++)
- printf("%d ", lst->array[i]);
- if (i == 0)
- printf("—ÔËÒÓÍ ÔÛÒÚ");
- printf("\n");
- }
- List *Create() // ÒÓÁ‰‡ÌË ÔÛÒÚÓ„Ó ÒÔËÒ͇
- {
- // ‚˚‰ÂΡÂÏ Ô‡ÏˇÚ¸
- List *lst = malloc(sizeof(List));
- // Ó·ÌÛΡÂÏ ÔÓΡ ÒÚÛÍÚÛ˚
- lst->array = NULL;
- lst->length = 0;
- // ‚ÓÁ‚‡˘‡ÂÏ ÒËÒÓÍ
- return lst;
- }
- List *Copy(const List *src) // ÔÓÎÌÓ ÍÓÔËÓ‚‡ÌË ÒÔËÒ͇
- {
- size_t i;
- // ÒÓÁ‰‡∏Ï ÔÛÒÚÓÈ ÒÔËÒÓÍ
- List *dst = Create();
- // ‚˚‰ÂΡÂÏ Ô‡ÏˇÚ¸ ÔÓ‰ ˝ÎÂÏÂÌÚ˚
- dst->array = malloc(src->length * sizeof(int));
- // ÂÒÎË Ô‡ÏˇÚ¸ Ì ‚˚‰ÂÎË·Ҹ ‚ÓÁ‚‡˘‡ÂÏ ÌÛ΂ÓÈ Û͇Á‡ÚÂθ
- if (dst->array == NULL)
- return NULL;
- // Á‡ÔÓÎÌˇÂÏ ÔÓΡ ÒÚÛÍÚÛ˚
- for (i = 0; i < src->length; i ++)
- dst->array[i] = src->array[i];
- dst->length = src->length;
- // ‚ÓÁ‚‡˘‡ÂÏ ÒËÒÓÍ
- return dst;
- }
- void Append(List *lst, int val) // ‚ÒÚ‡‚͇ ˝ÎÂÏÂÌÚ‡ ‚ ÍÓ̈ ÒÔËÒ͇
- {
- // ‚˚˜ËÒΡÂÏ ÌÓ‚˚È ‡ÁÏÂ
- size_t newSize = lst->length + 1;
- // Ô‚˚‰ÂΡÂÏ Ô‡ÏˇÚ¸
- lst->array = realloc(lst->array, newSize * sizeof(int));
- // ÂÒÎË Ô‡ÏˇÚ¸ Ì ‚˚‰ÂÎË·Ҹ ‚ÓÁ‚‡˘‡ÂÏ ÌÛ΂ÓÈ Û͇Á‡ÚÂθ
- if (lst->array == NULL)
- return;
- // ‚ÌÓÒËÏ ÌÓ‚˚È ˝ÎÂÏÂÌÚ
- if (lst->array)
- lst->array[lst->length] = val;
- // Û‚Â΢˂‡ÂÏ ‡ÁÏÂ
- lst->length ++;
- }
- void Prepend(List *lst, int val) // ‚ÒÚ‡‚͇ ˝ÎÂÏÂÌÚ‡ ‚ ̇˜‡ÎÓ ÒÔËÒ͇
- {
- // ‚˚˜ËÒΡÂÏ ÌÓ‚˚È ‡ÁÏÂ
- size_t i, newSize = lst->length + 1;
- // Ô‚˚‰ÂΡÂÏ Ô‡ÏˇÚ¸
- lst->array = realloc(lst->array, newSize * sizeof(int));
- if (lst->array == NULL)
- return;
- // ÒÏ¢‡ÂÏ ÒÚ‡˚ Á‡ÔËÒË ‚ ÍÓ̈ ̇ 1 ÔÓÁËˆË˛
- for (i = lst->length; i > 0; i --)
- lst->array[i] = lst->array[i - 1];
- // ‚ÌÓÒËÏ ÌÓ‚˚È ˝ÎÂÏÂÌÚ
- lst->array[0] = val;
- // Û‚Â΢˂‡ÂÏ ‡ÁÏÂ
- lst->length ++;
- }
- void AppendAll(List *dst, const List *src) // ‚ÒÚ‡‚ËÚ¸ ‚Ò ˝ÎÂÏÂÌÚ˚ Ó‰ÌÓ„Ó ÒÔËÒ͇ ‚ ÍÓ̈ ‰Û„Ó„Ó
- {
- // ‚˚˜ËÒΡÂÏ ÌÓ‚˚È ‡ÁÏÂ
- size_t i, newSize = src->length + dst->length;
- // Ô‚˚‰ÂΡÂÏ Ô‡ÏˇÚ¸
- dst->array = realloc(dst->array, newSize * sizeof(int));
- // ÂÒÎË Ô‡ÏˇÚ¸ Ì ‚˚‰ÂÎË·Ҹ ‚ÓÁ‚‡˘‡ÂÏ ÌÛ΂ÓÈ Û͇Á‡ÚÂθ
- if (dst->array == NULL)
- return;
- // ‰ÓÔËÒ˚‚‡ÂÏ ˝ÎÂÏÂÌÚ˚ ËÒıÓ‰ÌÓ„Ó Ï‡ÒÒË‚‡ ‚ ÍÓ̈ ˆÂ΂ӄÓ
- for (i = 0; i < src->length; i ++)
- dst->array[dst->length + i] = src->array[i];
- // Á‡ÔÓÏË̇ÂÏ ÌÓ‚˚È ‡ÁÏÂ
- dst->length = newSize;
- }
- void InsertAt(List *lst, int val, size_t pos) // ‚ÚÒ‡‚͇ ˝ÎÂÏÂÌÚ‡ ÔÓ Ë̉ÂÍÒÛ
- {
- // ‚˚˜ËÒΡÂÏ ÌÓ‚˚È ‡ÁÏÂ
- size_t i, newSize = lst->length + 1;
- // ÂÒÎË ÔÓÁËˆËˇ ‚˚ıÓ‰ËÚ Á‡ „‡ÌˈÛ, ‰Ó·‡‚ΡÂÏ ‚ ÍÓ̈
- if (pos > lst->length)
- {
- Append(lst, val);
- return;
- }
- // Ô‚˚‰ÂΡÂÏ Ô‡ÏˇÚ¸
- lst->array = realloc(lst->array, newSize * sizeof(int));
- // ÂÒÎË Ô‡ÏˇÚ¸ Ì ‚˚‰ÂÎË·Ҹ ‚ÓÁ‚‡˘‡ÂÏ ÌÛ΂ÓÈ Û͇Á‡ÚÂθ
- if (lst->array == NULL)
- return;
- // ÒÏ¢‡ÂÏ ÒÚ‡˚ Á‡ÔËÒË ‚ ÍÓ̈ ̇ 1 ÔÓÁËˆË˛
- for (i = lst->length; i > pos; i --)
- lst->array[i] = lst->array[i - 1];
- // ‚ÌÓÒËÏ ÌÓ‚˚È ˝ÎÂÏÂÌÚ
- lst->array[pos] = val;
- // Û‚Â΢˂‡ÂÏ ‡ÁÏÂ
- lst->length ++;
- }
- void RemoveAt(List *lst, size_t pos) // Û‰‡ÎÂÌË ˝ÎÂÏÂÌÚ‡ ÔÓ Ë̉ÂÍÒÛ
- {
- size_t i = pos;
- // ÂÒÎË ÔÓÁËˆËˇ ‚˚ıÓ‰ËÚ Á‡ „‡ÌˈÛ, ÌË˜Â„Ó Ì ۉ‡ÎˇÂÏ
- if (pos >= lst->length)
- return;
- // ÒÏ¢‡ÂÏ ˝ÎÂÏÂÌÚ˚ ‚΂Ó, ÔÂÂÁ‡ÔËÒ˚‚‡ˇ Û‰‡ÎˇÂÏ˚È
- for (; i < lst->length - 1; i ++)
- lst->array[i] = lst->array[i + 1];
- // ÛÏÂ̸¯‡ÂÏ ‡ÁÏ ÒÔËÒ͇
- lst->length --;
- // Ô‚˚‰ÂΡÂÏ Ô‡ÏˇÚ¸
- lst->array = realloc(lst->array, lst->length * sizeof(int));
- }
- void RemoveAll(List *lst) // Û‰‡ÎÂÌË ‚ÒÂı ˝ÎÂÏÂÌÚÓ‚ ËÁ ÒÔËÒ͇
- {
- if (lst->length)
- {
- // ÓÒ‚Ó·Ó˝Ê‰‡ÂÏ Ô‡ÏˇÚ¸
- free(lst->array);
- // Ó·ÌÛΡÂÏ ÔÓΡ
- lst->array = NULL;
- lst->length = 0;
- }
- }
- int Pop(List *lst) // Û‰‡ÎÂÌË ˝ÎÂÏÂÌÚ‡ Ò ÍÓ̈‡ ÒÔËÒ͇, ÙÛÌÍˆËˇ ‚ÓÁ‚‡˘‡ÂÚ Û‰‡ÎÂÌÌ˚È ˝ÎÂÏÂÌÚ
- {
- // ÂÒÎË ÒÔËÒÓÍ ÔÛÒÚ, ‚ÓÁ‚‡˘‡ÂÏ ÌÓθ
- if (lst->length == 0)
- return 0;
- // ÔÓÎÛ˜‡ÂÏ Í‡ÈÌËÈ Ò ÍÓ̈‡ ˝ÎÂÏÂÌÚ
- int val = lst->array[lst->length - 1];
- // ÛÏÂ̸¯‡ÂÏ ‡ÁÏ ÒÔËÒ͇
- lst->length --;
- // Ô‚˚‰ÂΡÂÏ Ô‡ÏˇÚ¸
- lst->array = realloc(lst->array, lst->length * sizeof(int));
- // ‚ÓÁ‚‡˘‡ÂÏ ÂÁÛθڇÚ
- return val;
- }
- int Dequeue(List *lst) // Û‰‡ÎÂÌË ˝ÎÂÏÂÌÚ‡ Ò Ì‡˜‡Î‡ ÒÔËÒ͇, ÙÛÌÍˆËˇ ‚ÓÁ‚‡˘‡ÂÚ Û‰‡ÎÂÌÌ˚È ˝ÎÂÏÂÌÚ
- {
- // ÂÒÎË ÒÔËÒÓÍ ÔÛÒÚ, ‚ÓÁ‚‡˘‡ÂÏ ÌÓθ
- if (lst->length == 0)
- return 0;
- // ÔÓÎÛ˜‡ÂÏ Í‡ÈÌËÈ Ò Ì‡˜‡Î‡ ˝ÎÂÏÂÌÚ
- int val = lst->array[0], i;
- // ÒÏ¢‡ÂÏ ˝ÎÂÏÂÌÚ˚ ‚ÎÂ‚Ó Ì‡ Ó‰ÌÛ ÔÓÁËˆË˛
- for (i = 0; i < lst->length - 1; i ++)
- lst->array[i] = lst->array[i + 1];
- // ÛÏÂ̸¯‡ÂÏ ‡ÁÏ ÒÔËÒ͇
- lst->length --;
- // Ô‚˚‰ÂΡÂÏ Ô‡ÏˇÚ¸
- lst->array = realloc(lst->array, lst->length * sizeof(int));
- // ‚ÓÁ‚‡˘‡ÂÏ ÂÁÛθڇÚ
- return val;
- }
- size_t Length(const List *lst) // ‚˚˜ËÒÎÂÌË ‰ÎËÌ˚ ÒÔËÒ͇
- {
- // ‚ÓÁ‚‡˘‡ÂÏ Á̇˜ÂÌË ÒÓÓÚ‚ÂÚÒÚ‚Û˛˘Â„Ó ÔÓΡ
- return lst->length;
- }
- int GetAt(const List *lst, size_t pos) // ‚ÁˇÚË ˝ÎÂÏÂÌÚ‡ ÒÔËÒ͇ ÔÓ Ë̉ÂÍÒÛ
- {
- int res = 0;
- // ÔÓÎÛ˜‡ÂÏ Á̇˜ÂÌË ˝ÎÂÏÂÌÚ‡ Ò ÔÓ‚ÂÍÓÈ Ì‡ ‚˚ıÓ‰ Á‡ „‡ÌËˆÛ Ï‡ÒÒË‚‡
- if (pos <= lst->length - 1)
- res = lst->array[pos];
- // ‚ÓÁ‚‡˘‡ÂÏ ÂÁÛθڇÚ
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement