Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct tnode {
- char value;
- struct tnode *next;
- } node;
- node *dodaj_na_poczatek(node *head, char val) {
- node *temp = malloc(sizeof(struct tnode));
- if (!temp) {
- printf("Brak pamieci !\n");
- return head;
- }
- temp->value = val;
- temp->next = head;
- return temp;
- }
- node *dodaj_rosnaco(node *head, char val) {
- node *elem = malloc(sizeof(node));
- if (!elem)
- return head;
- elem->value = val;
- elem->next = NULL;
- if (head) {
- if (head->value > val) {
- elem->next = head;
- head = elem;
- } else {
- node *p = head;
- for (; p->next && p->next->value < val; p = p->next);
- elem->next = p->next;
- p->next = elem;
- }
- } else {
- head = elem;
- }
- return head;
- }
- void wypisz_liste(const node *wsk) {
- if (!wsk) {
- printf("Lista jest pusta.\n");
- return;
- }
- printf("head -> ");
- while (wsk != NULL) {
- printf("[ %c ] -> ", wsk->value);\
- wsk = wsk->next;
- }
- printf("NULL\n");
- }
- node *usun_wszystkie(node *wsk) {
- while (wsk) {
- node *next = wsk->next;
- free(wsk);
- wsk = next;
- }
- return NULL;
- }
- int main(void) {
- node *head = NULL;
- head = dodaj_rosnaco(head, 'z');
- head = dodaj_rosnaco(head, 'j');
- head = dodaj_rosnaco(head, 'a');
- head = dodaj_rosnaco(head, 'k');
- puts("Przed dodaniem \'w\':");
- wypisz_liste(head);
- head = dodaj_rosnaco(head, 'w');
- puts("Po dodaniu \'w\':");
- wypisz_liste(head);
- usun_wszystkie(head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement