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>
- typedef struct node{
- char *data;
- struct node *next;
- }node;
- node* init();
- int length(node* head);
- void display(node* head);
- void push(node**headPtr, char *data);
- void freeList(node** head);
- void append(node**headRef, char *data);
- node* copy(node* source);
- void pop(node**headPtr);
- void reverse(node **headPtr);
- void sort(node **headPtr);
- void pushTest(node **list);
- void popTest(node **list);
- int main (int argc, char **argv) {
- node* list = init();
- popTest(&list);
- pushTest(&list);
- /* node* list = init();
- push(&list, "Ala");
- append(&list, "Beka");
- display(list);
- printf("Nastepny test\n");
- reverse(&list);
- display(list);
- node *duplicate = copy(list);
- freeList(&list);
- display(list);
- display(duplicate);
- sort(&duplicate);
- display(duplicate);
- /* pop(&list);
- reverse(&list);
- node *duplicate = copy(list);
- freeList(&list);
- display(list);
- display(duplicate);
- sort(&duplicate);
- display(duplicate);
- */
- return(0);
- }
- node* init()
- {
- node *head = NULL;
- node *second = NULL;
- node *third = NULL;
- head = malloc(sizeof(node));
- second = malloc(sizeof(node));
- third = malloc(sizeof(node));
- head->data = "1";
- head->next = second;
- second->data ="2";
- second->next = third;
- third->data = "3";
- third->next = NULL;
- return head;
- }
- int length(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(length(head) == 0)
- {
- printf("Empty list\n");
- }
- else
- {
- while(position != NULL)
- {
- printf("%s\n", position->data);
- position = position->next;
- }
- }
- }
- void push(node**headPtr, char *data)
- {
- node *newNode = malloc(sizeof(node));
- newNode->data = data;
- newNode->next = *headPtr;
- *headPtr = newNode;
- }
- void pop(node**headPtr)
- {
- node*newNode;
- newNode = *headPtr;
- (*headPtr) = (*headPtr)->next;
- newNode->next = NULL;
- free(newNode);
- }
- void freeList(node** headPtr)
- {
- while(*headPtr)
- {
- freeList(&((*headPtr)->next));
- free(*headPtr);
- *headPtr = NULL;
- }
- }
- void append(node**headRef, char *data)
- {
- node* position = *headRef;
- node* newNode;
- newNode = malloc(sizeof(node));
- newNode->data = data;
- newNode->next = NULL;
- if(position == NULL)
- {
- *headRef = newNode;
- }
- else
- {
- while(position->next !=NULL)
- {
- position = position->next;
- }
- position->next = newNode;
- }
- }
- node* copy(node* source)
- {
- node* head = NULL;
- node** dst = &head;
- while(source)
- {
- *dst = malloc(sizeof(node));
- (*dst)->data = source->data;
- (*dst)->next = NULL;
- source = source->next;
- dst = &((*dst)->next);
- }
- return head;
- }
- void reverse(node **headPtr)
- {
- node* currentNode = *headPtr;
- node* previous = NULL;
- node* after = NULL;
- while(currentNode != NULL)
- {
- after = currentNode->next;
- currentNode->next = previous;
- previous = currentNode;
- currentNode = after;
- }
- *headPtr = previous;
- }
- void sort(node **headPtr)
- {
- int swapped;
- node*newNode;
- node* lastNode = NULL;
- do
- {
- swapped = 0;
- newNode = *headPtr;
- while(newNode->next != lastNode)
- {
- char* firstString = newNode->data;
- char * secondString = newNode->next->data;
- if(strcmp(firstString,secondString) > 0)
- {
- char * temporaryString = newNode->data;
- newNode->data = newNode->next->data;
- newNode->next->data = temporaryString;
- swapped = 1;
- }
- newNode = newNode->next;
- }
- lastNode = newNode;
- }
- while(swapped);
- }
- void pushTest(node **list)
- {
- struct node *head = *list;
- push(&head, "0");
- push(&head, "13");
- display(head);
- }
- void popTest(node **list)
- {
- struct node *head = *list;
- pop(&head);
- display(head);
- int len = length(head);
- printf("%d\n", len);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement