Ring-List [C]
A simple Ring-List (based on linked-list)
Code or download:
#include <stdio.h> #include <stdlib.h>
struct node { char value; struct node *next; };
struct node *head;
// print list
void list() { struct node *current = head->next; printf("Printing the list:\n"); printf("Current node:%c\n", current->value); while (current->next != head) { current = current->next; printf("Node:%c\n", current->value); } }
// insert element
void insert(char value) { struct node *new; new = (struct node *) malloc(sizeof *new); new->value = value; new->next = head->next; head->next = new; }
// shift
void shift() { struct node *temp = head->next; struct node *mem = head->next; head->next = head->next->next; while (temp->next != head) { temp = temp->next; } temp->next = mem; mem->next = head;
}
// remove element
void delete() { head->next = head->next->next;
}
//main
int main() { char input;
//create 'First' head = (struct node *) malloc(sizeof *head); head->next = head;
printf("> "); while (scanf("%s", &input) != EOF) {
//insert if ((input >= 'a') && (input <= 'z')) { insert(input); list(); printf("\r\n"); } else //shift if (input == '<') { shift(); list(); printf("\r\n"); } else //remove if (input == '/') { delete(); list(); printf("\r\n"); } else //list if (input == '?') { list(); printf("\r\n"); } else //exit if (input == '!') { break; }
printf("> "); }
return EXIT_SUCCESS; }










