Advertisement
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();
- Node *copy(Node *src);
- int lengthMeasure(Node* head);
- void display(Node* head);
- void push(Node**head, char *data);
- void destroy(Node **head);
- void appendNode(Node**head, char *data);
- 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, "STRING ");
- push(&list, "four");
- push(&list, "six");
- push(&list, "four");
- push(&list, "seven");
- appendNode(&list, "five");
- display(list);
- printf("Measured 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 *currentPosition = head;
- int count = 0;
- while(currentPosition != NULL)
- {
- count++;
- currentPosition = currentPosition->next;
- }
- return count;
- }
- void display(Node *head)
- {
- Node *currentPosition = head;
- if(head == NULL)
- {
- printf("NO ELEMENETS\n");
- }
- else
- {
- while(currentPosition != NULL)
- {
- printf("%s ", currentPosition->data);
- currentPosition = currentPosition->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 *currentPosition = *head;
- Node *newNode;
- newNode = allocate();
- newNode->data = malloc(sizeof(data));
- strcpy(newNode->data, data);
- newNode->next = NULL;
- if(currentPosition == NULL)
- {
- *head = newNode;
- }
- else
- {
- while(currentPosition->next !=NULL)
- {
- currentPosition = currentPosition->next;
- }
- currentPosition->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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement