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();y
- int length(node* head);
- void display(node* head);
- void push(node**headPtr, char *data);
- void freeList(node** head);
- void appendNode(node**headRef, char *data);
- node* copy(node* source);
- void pop(node**headPtr);
- void reverse(node **headPtr);
- void sort(node **headPtr);
- int main (int argc, char **argv)
- {
- node* list = init();
- push(&list, "ONE");
- appendNode(&list, "TWO");
- display(list);
- printf("\n");
- pop(&list);
- reverse(&list);
- node *duplicate = copy(list);
- freeList(&list);
- display(list);
- printf("\n");
- display(duplicate);
- printf("\n");
- sort(&duplicate);
- display(duplicate);
- return(0);
- }
- node* init()//nitializes empty list, returns pointer to newly created list,
- {
- node *head = NULL;
- node *second = NULL;
- node *third = NULL;
- head = malloc(sizeof(node));
- second = malloc(sizeof(node));
- third = malloc(sizeof(node));
- head->data = "AAAAAAAAAA";
- head->next = second;
- second->data ="BBBBBBBB";
- second->next = third;
- third->data = "CCCCCCCCCCC";
- third->next = NULL;
- return head;
- }
- int length(node* head)// returns number of elements stores on the list identified by parameter
- {
- node *currentPosition = head;
- int count = 0;
- while(currentPosition != NULL)
- {
- count++;
- currentPosition = currentPosition->next;
- }
- return count;
- }
- void display(node* head)//– shows all elements stored on the list identified by a parameter
- {
- node *currentPosition = head;
- if(length(head) == 0)
- {
- printf("NO ELEMNETS\n");
- }
- else
- {
- while(currentPosition != NULL)
- {
- printf("%s\n", currentPosition->data);
- currentPosition = currentPosition->next;
- }
- }
- }
- void push(node**headPtr, char *data)// inserts new element at the beginning on the list identified by paramete
- {
- node *newNode = malloc(sizeof(node));
- newNode->data = data;
- newNode->next = *headPtr;
- *headPtr = newNode;
- }
- void pop(node**headPtr)//removes first element from the end on the list identified by parameter and return storedvalue
- {
- node*newNode;
- newNode = *headPtr;
- (*headPtr) = (*headPtr)->next;
- newNode->next = NULL;
- free(newNode);
- }
- void freeList(node** headPtr)// destroys the list identified by parameter
- {
- while(*headPtr)
- {
- freeList(&((*headPtr)->next));
- free(*headPtr);
- *headPtr = NULL;
- }
- }
- void appendNode(node**headRef, char *data)// to the end of the list insert element
- {
- node* currentPosition = *headRef;
- node* newNode;
- newNode = malloc(sizeof(node));
- newNode->data = data;
- newNode->next = NULL;
- if(currentPosition == NULL)
- {
- *headRef = newNode;
- }
- else
- {
- while(currentPosition->next !=NULL)
- {
- currentPosition = currentPosition->next;
- }
- currentPosition->next = newNode;
- }
- }
- node* copy(node* source)// creates copy of the list identified by parameter
- {
- node* head = NULL;
- node** temp = &head;
- while(source)
- {
- *temp = malloc(sizeof(node));
- (*temp)->data = source->data;
- (*temp)->next = NULL;
- source = source->next;
- temp = &((*temp)->next);
- }
- return head;
- }
- void reverse(node **headPtr)//reverses order of elements of the list identified by parameter
- {
- 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)//sort elements of the list identified by parameter
- {
- 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement