Skip to content

Instantly share code, notes, and snippets.

@eduardoaugustojulio
Last active February 22, 2021 20:13
Show Gist options
  • Save eduardoaugustojulio/0e2c42659be782879cb88ce5e1c23844 to your computer and use it in GitHub Desktop.
Save eduardoaugustojulio/0e2c42659be782879cb88ce5e1c23844 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
typedef struct SinglyLinkedListNode
{
int data;
SinglyLinkedListNode *next;
}SinglyLinkedListNode;
typedef struct SinglyLinkedList
{
SinglyLinkedListNode *head;
SinglyLinkedListNode *tail;
}SinglyLinkedList;
SinglyLinkedListNode *create_singly_linked_node(int data)
{
SinglyLinkedListNode *node = (SinglyLinkedListNode*)malloc(sizeof(SinglyLinkedListNode*));
if (node)
{
node->data = data;
node->next = NULL;
}
return node;
}
SinglyLinkedList *create_singly_linked_list(void)
{
SinglyLinkedList *list = (SinglyLinkedList*)malloc(sizeof(SinglyLinkedList));
if (list)
{
list->head = NULL;
list->tail = NULL;
}
return list;
}
void clear_singly_linked_list(SinglyLinkedList *list)
{
SinglyLinkedListNode *node, *temp;
node = list->head;
while (node != NULL) {
temp = node->next;
free(node);
node = temp;
}
}
void delete_singly_linked_list(SinglyLinkedList *list)
{
if (list) {
clear_singly_linked_list(list);
free(list);
}
}
void print_singly_linked_list(SinglyLinkedListNode* node)
{
while (node)
{
printf("%d\n",node->data);
node = node->next;
}
}
void push_back(SinglyLinkedList *list, int data)
{
SinglyLinkedListNode *node = create_singly_linked_node(data);
if (list->head == NULL)
{
/* Adding the first node */
list->head = node;
list->tail = node;
}
else
{
list->tail->next = node;
list->tail = node;
}
}
void pop_back(SinglyLinkedList * list)
{
if (list->tail)
{
SinglyLinkedListNode *current, *previous = NULL;
current = list->head;
while (current->next)
{
previous = current;
current = current->next;
}
free(current);
if (previous)
{
previous->next = NULL;
}
else
{
/* List is now empty */
list->head = NULL;
list->tail = NULL;
}
if (list->tail->next == NULL)
{
list->head = list->tail;
}
}
}
SinglyLinkedListNode *sort_list(SinglyLinkedListNode *head)
{
SinglyLinkedListNode *node, *current = head;
while(current->next)
{
if(current->data > current->next->data)
{
node = current->next;
current->next = current;
current = node;
}
else
{
current = current->next;
}
}
return head;
}
SinglyLinkedListNode *remove_duplicate_nodes(SinglyLinkedListNode* head)
{
SinglyLinkedListNode *current = head;
while(current->next)
{
if(current->data != current->next->data)
{
current = current->next;
}
else
{
free(current->next);
current->next = current->next->next;
}
}
return head;
}
int main()
{
SinglyLinkedList *list = create_singly_linked_list();
push_back(list, 2);
push_back(list, 1);
push_back(list, 2);
push_back(list, 1);
push_back(list, 4);
push_back(list, 3);
push_back(list, 3);
remove_duplicate_nodes(list->head);
delete_singly_linked_list(list);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment