Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- struct Node{
- const char *txt;
- struct Node *next;
- };
- void handle_error(const char *txt);
- struct Node *create_new_element(const char *txt, struct Node *next);
- void swap_elements(struct Node *a, struct Node *b);
- struct Node *init();
- int length(struct Node *head);
- void display(struct Node *head);
- void push(struct Node **head, const char *txt);
- const char *pop(struct Node **head);
- void destroy(struct Node **head_ptr);
- void append(struct Node **head, const char *txt);
- struct Node *copy(struct Node *head);
- void reverse(struct Node **head);
- void sort(struct Node *head);
- ///------------------------------------------------------------
- void handle_error(const char *txt){
- printf("%s\n", txt);
- exit(0);
- }
- struct Node *create_new_element(const char *txt, struct Node *next){
- struct Node *new_element = malloc(sizeof (struct Node));
- if(new_element == NULL){
- handle_error("Failed to create a new element");
- }
- new_element -> txt = txt;
- new_element -> next = next;
- return new_element;
- }
- struct Node *init(){
- return NULL;
- }
- void push(struct Node **head, const char *txt){
- struct Node *new_node = create_new_element(txt, *head);
- *head = new_node;
- }
- int length(struct Node *head){
- int res = 0;
- struct Node *current_element = head;
- while(current_element != NULL){
- res += 1;
- current_element = current_element -> next;
- }
- return res;
- }
- void display(struct Node *head){
- printf("length: %d\n", length(head));
- if(length(head) == 0 ){
- printf("the list is empty\n\n");
- return;
- }
- struct Node *current_element = head;
- printf("elements of the list:--------------\n");
- while(current_element != NULL){
- printf("%s\n", current_element -> txt);
- current_element = current_element -> next;
- }
- printf("-----------------------------------\n\n");
- }
- const char *pop(struct Node **head_ptr){
- struct Node *previous = NULL;
- struct Node *current_element = *head_ptr;
- while(current_element -> next != NULL){// i go to the end
- previous = current_element;
- current_element = current_element -> next;
- }
- if(previous != NULL){//remove connection
- previous -> next = NULL;
- }
- const char *buf = current_element -> txt;
- free(current_element);
- return buf;
- }
- void destroy(struct Node **head_ptr){
- while (*head_ptr){
- struct Node *t = (*head_ptr) -> next;
- free (*head_ptr);
- *head_ptr = t;
- };
- *head_ptr = NULL;
- }
- void append(struct Node **head_ptr, const char *txt){
- struct Node *current_element = *head_ptr;
- struct Node *new_element = create_new_element(txt, NULL);
- if((*head_ptr) == NULL){
- *head_ptr = new_element;
- return;
- }
- while(current_element -> next != NULL){
- current_element = current_element -> next;
- }
- current_element -> next = new_element;
- }
- struct Node *copy(struct Node *head){
- struct Node *new_head = NULL;
- struct Node **current_element = &new_head;
- while (head){
- *current_element = create_new_element(head -> txt, NULL);
- head = head -> next;
- current_element = &((*current_element)->next);
- }
- return new_head;
- }
- void reverse(struct Node **head){
- struct Node *previous = NULL;
- struct Node *current_element = *head;
- struct Node *next_element;
- while(current_element != NULL){
- next_element = current_element -> next;
- current_element -> next = previous;
- previous = current_element;
- current_element = next_element;
- }
- *head = previous;
- }
- void swap_elements(struct Node *a, struct Node *b){
- const char *buf = a -> txt;
- a -> txt = b->txt;
- b -> txt = buf;
- }
- void sort(struct Node *head){
- if(head -> next == NULL)
- return;
- int not_sorted;
- struct Node *current_element;
- do{
- not_sorted = 0;
- current_element = head;
- while(current_element -> next != NULL){
- if(strcmp(current_element -> txt, current_element -> next -> txt) > 0){
- swap_elements(current_element, current_element -> next);
- not_sorted = 1;
- }
- current_element = current_element -> next;
- }
- }while(not_sorted);
- }
- int main(){
- struct Node *myList = init();
- display(myList);
- printf("Appending to myList\n");
- append(&myList, "jeden");
- display(myList);
- append(&myList, "dwa");
- display(myList);
- append(&myList, "trzy");
- display(myList);
- append(&myList, "cztery");
- display(myList);
- printf(" pushing to myList\n");
- push(&myList, "zero");
- display(myList);
- push(&myList, "minus jeden");
- display(myList);
- printf("Copying myList to otherList\n");
- struct Node *otherList = copy(myList);
- printf("otherList:\n");
- display(otherList);
- printf("Reversing myList\n");
- reverse(&myList);
- display(myList);
- printf("sorting myList\n");
- sort(myList);
- display(myList);
- printf("############################################## popping el. of myList\n");
- pop(&myList);
- display(myList);
- printf("destroying myList\n");
- destroy(&myList);
- display(myList);
- printf("otherList:\n");
- display(otherList);
- printf(" destroying otherList\n");
- destroy(&otherList);
- display(otherList);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement