Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node {
- char *slowo;
- struct node * next;
- } node_t;
- void drukuj(node_t * head) {
- if(head != 0) {
- node_t * current = head;
- while(current != NULL) {
- printf("%s\n", current->slowo);
- current = current->next;
- }
- }
- else
- printf("pusta lista\n");
- }
- void wstaw(node_t ** head, char *wyraz) {
- node_t * new_node = malloc(sizeof(node_t));
- new_node->slowo = wyraz;
- new_node->next = *head;
- *head = new_node;
- }
- long int szukaj(node_t * head, char *wyraz) {
- node_t * current = head;
- while(current != NULL) {
- if(current->slowo == wyraz)
- return current;
- current = current->next;
- }
- return NULL;
- }
- void szukaj2(node_t * head) {
- node_t * new_node = head;
- if(new_node == NULL) {
- printf("nie ma takiego elementu\n");
- }
- else
- printf("%s\n", new_node->slowo);
- }
- long int prev(node_t * head) {
- if(head + 2 == NULL)
- return NULL;
- else
- return head + 2;
- }
- void usun(node_t ** head, char *wyraz) {
- long int x = szukaj(*head, wyraz);
- //node_t * usuwany = x;
- //usuwany->next = usuwany - 2;
- //printf("XXXXXXXXXX %d %d\n", usuwany->next, x - 32);
- // printf("TET, %s\n", usuwany->next->slowo);
- if(x != NULL) {
- long int poprzedni = prev(x);
- // printf("TET %ld, %ld\n", poprzedni, usuwany);
- if(x != *head) {
- node_t * prev = poprzedni;
- if(prev->next->next == NULL)
- prev->next = 0;
- // printf("ASDASD %s, %s\n", prev->slowo, usuwany->slowo);
- else
- prev->next = x - 32;
- // printf("TET %d\n", prev->next);
- }
- else {
- *head -=2;
- }
- }
- }
- void kasuj(node_t ** head) {
- while(*head != NULL) {
- free(*head);
- *head = (*head)->next;
- }
- //free(*head);
- }
- long int bezPowtorzen(node_t * head) {
- node_t * new_node = malloc(sizeof(node_t));
- node_t * current = head;
- new_node->slowo = current->slowo;
- while(current != NULL) {
- char *czyWyraz = current->slowo;
- if(szukaj(new_node, czyWyraz) == 0) {
- wstaw(&new_node, czyWyraz);
- }
- current = current->next;
- }
- return new_node;
- }
- long int scal(node_t ** l1, node_t ** l2) {
- node_t * new_node = malloc(sizeof(node_t));
- node_t * current = *l1;
- node_t * current2 = *l2;
- new_node->slowo = current->slowo;
- current = current->next;
- while(current != NULL) {
- wstaw(&new_node, current->slowo);
- current = current->next;
- }
- while(current2 != NULL) {
- wstaw(&new_node, current2->slowo);
- current2 = current2->next;
- }
- new_node = bezPowtorzen(new_node);
- kasuj(l1);
- kasuj(l2);
- return new_node;
- }
- int main() {
- node_t * lista = malloc(sizeof(node_t));
- lista->slowo = "knur";
- wstaw(&lista, "dzik");
- wstaw(&lista, "dzik");
- wstaw(&lista, "lis");
- wstaw(&lista, "kuna");
- wstaw(&lista, "sowa");
- wstaw(&lista, "lis");
- wstaw(&lista, "wiewiorka");
- wstaw(&lista, "dzieciol");
- drukuj(lista);
- printf("\n");
- szukaj2(szukaj(lista, "dzik"));
- szukaj2(szukaj(lista, "wiewiorka"));
- szukaj2(szukaj(lista, "asdasdasd"));
- usun(&lista, "knur");
- printf("\n");
- drukuj(lista);
- // kasuj(&lista);
- // drukuj(lista);
- printf("\n");
- node_t * lista2 = bezPowtorzen(lista);
- printf("\nLISTA 2:\n");
- drukuj(lista2);
- printf("\n");
- wstaw(&lista2, "lania");
- wstaw(&lista2, "zbik");
- wstaw(&lista, "zbik");
- wstaw(&lista, "kojot");
- node_t * lista3 = scal(&lista, &lista2);
- printf("\nLISTA 3:\n");
- drukuj(lista3);
- printf("\n");
- printf("\nLISTY 1 I 2 PO SCALENIU:\n");
- drukuj(lista);
- drukuj(lista2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement