Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include <stdbool.h>
- typedef struct node{
- char *data;
- struct node *next;
- }node;
- node *init();
- node *allocate();
- int lengthMeasure(node* head);
- void display(node* head);
- void push(node**head, char *data);
- void destroy(node **head);
- void appendNode(node**head, char *data);
- node *copy(node *src);
- char *pop(node**head);
- void reverse(node **head);
- void swapData(node **newNode);
- void sort(node **head);
- int main (int argc, char **argv)
- {
- node *list = init();
- push(&list, "Word");
- push(&list, "4");
- push(&list, "6");
- push(&list, "4");
- push(&list, "7");
- appendNode(&list, "5");
- display(list);
- printf("Length: %d\n", lengthMeasure(list));
- printf("Popped value: %s\n", pop(&list));
- display(list);
- reverse(&list);
- node *duplicate = copy(list);
- printf("Destroy\n");
- destroy(&list);
- display(list);
- display(duplicate);
- sort(&duplicate);
- display(duplicate);
- destroy(&duplicate);
- return(0);
- }
- node *init()
- {
- return NULL;
- }
- node *allocate()
- {
- node *head = malloc(sizeof(node));
- return head;
- }
- int lengthMeasure(node *head)
- {
- node *position = head;
- int count = 0;
- while(position != NULL)
- {
- count++;
- position = position->next;
- }
- return count;
- }
- void display(node *head)
- {
- node *position = head;
- if(head == NULL){
- printf("Lack of elements\n");
- }
- else
- {
- while(position != NULL)
- {
- printf("%s ", position->data);
- position = position->next;
- }
- printf("\n");
- }
- }
- void push(node **head, char *data)
- {
- struct node *newNode = malloc (sizeof (struct node));
- newNode->data = malloc(sizeof(data));
- strcpy(newNode->data, data);
- newNode->next = *head;
- *head = newNode;
- }
- char *pop(node **head)
- {
- char *temp = (*head)->data;
- node *newNode = *head;
- (*head) = (*head)->next;
- newNode->next = NULL;
- free(newNode->data);
- free(newNode);
- return temp;
- }
- void destroy(node **head)
- {
- if(*head != NULL)
- {
- destroy(&(*head)->next);
- free((*head)->data);
- (*head)->data = NULL;
- free(*head);
- *head = NULL;
- }
- }
- void appendNode(node **head, char *data)
- {
- node *position = *head;
- node *newNode;
- newNode = allocate();
- newNode->data = malloc(sizeof(data));
- strcpy(newNode->data, data);
- newNode->next = NULL;
- if(position == NULL)
- {
- *head = newNode;
- }
- else
- {
- while(position->next !=NULL)
- {
- position = position->next;
- }
- position->next = newNode;
- }
- }
- node *copy(node *src)
- {
- node *head = NULL;
- node **dst = &head;
- while(src)
- {
- *dst = allocate();
- (*dst)->data = malloc(sizeof(src->data));
- strcpy((*dst)->data, src->data);
- (*dst)->next = NULL;
- src = src->next;
- dst = &((*dst)->next);
- }
- return head;
- }
- void reverse(node **head)
- {
- node *currentNode = *head;
- node *previous = NULL;
- node *after = NULL;
- while(currentNode != NULL)
- {
- after = currentNode->next;
- currentNode->next = previous;
- previous = currentNode;
- currentNode = after;
- }
- *head = previous;
- }
- void swapData(node **newNode)
- {
- char *temporaryString = (*newNode)->data;
- (*newNode)->data = (*newNode)->next->data;
- (*newNode)->next->data = temporaryString;
- }
- void sort(node **head)
- {
- bool swapped;
- node *newNode;
- node *lastNode = NULL;
- do
- {
- swapped = false;
- newNode = *head;
- while(newNode->next != lastNode)
- {
- if(strcmp(newNode->data, newNode->next->data) >= 0)
- {
- swapData(&newNode);
- swapped = true;
- }
- newNode = newNode->next;
- }
- lastNode = newNode;
- }while(swapped);
- }
Add Comment
Please, Sign In to add comment