Skip to content

Instantly share code, notes, and snippets.

@RalXYZ
Created March 13, 2020 17:59
Show Gist options
  • Save RalXYZ/c4e38052d8ba6623634bb981551c7884 to your computer and use it in GitHub Desktop.
Save RalXYZ/c4e38052d8ba6623634bb981551c7884 to your computer and use it in GitHub Desktop.
A set of linked list functions
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode *next;
};
struct ListNode *listCreat();
void listPrint(struct ListNode *head );
struct ListNode *listElementDelete(struct ListNode *head );
struct ListNode *listReverse( struct ListNode *head );
int main() {
struct ListNode *head;
head = listCreat();
listPrint(head);
return 0;
}
/*******************************************************************************/
struct ListNode *listCreat() {
struct ListNode *head = (struct ListNode *) malloc(sizeof(struct ListNode));
struct ListNode *current = head;
struct ListNode *tail = head;
int firstLoop = 1;
for (int n;; current = current->next) {
scanf("%d", &n);
if (n == -1) {
if (firstLoop) {
free(head);
head = NULL;
}
break;
}
current->data = n;
current->next = (struct ListNode *) malloc(sizeof(struct ListNode));
tail = current;
firstLoop = 0;
}
if (firstLoop)
return NULL;
free(tail->next);
tail->next = NULL;
return head;
}
void listPrint(struct ListNode *head ) {
struct ListNode *p = head;
while (p) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
struct ListNode *listCut( struct ListNode **originalHead ) {
if (*originalHead == NULL)
return NULL;
struct ListNode *targetHead = NULL;
struct ListNode *targetCurrent = targetHead;
struct ListNode *previous = NULL;
for (struct ListNode *current = *originalHead; current != NULL;) {
if (current->data % 2 == 1) { // Change this to what you want to cut
if(targetHead == NULL) {
targetHead = (struct ListNode *)malloc(sizeof(struct ListNode));
targetHead->data = current->data;
targetHead->next = NULL;
targetCurrent = targetHead;
}
else {
targetCurrent->next = (struct ListNode *)malloc(sizeof(struct ListNode));
targetCurrent = targetCurrent->next;
targetCurrent->data = current->data;
targetCurrent->next = NULL;
}
if (current == *originalHead) {
struct ListNode *temp = current->next;
free(current);
current = temp;
*originalHead = current;
}
else {
struct ListNode *temp = current->next;
free(current);
current = temp;
previous->next = current;
}
}
else {
previous = current;
current = current->next;
}
}
return targetHead;
}
struct ListNode *listElementDelete(struct ListNode *head ) {
struct ListNode* moveTo = NULL;
struct ListNode* previous = NULL;
for (struct ListNode* p = head; p != NULL; ) {
if (p->data % 2 == 0) { // Change this to what you want to delete
if (p == head) {
moveTo = p->next;
head = p->next;
free(p);
p = NULL;
previous = p;
}
else {
moveTo = p->next;
previous->next = p->next;
free(p);
p = NULL;
}
}
else {
previous = p;
moveTo = p->next;
}
p = moveTo;
}
return head;
}
struct ListNode *listReverse( struct ListNode *head ) {
struct ListNode* current = head;
struct ListNode* next = head;
struct ListNode* previous = NULL;
for (current = head; current != NULL; current = next) {
next = current->next;
current->next = previous;
previous = current;
}
return previous;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment