Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- // To add:
- // Support for A-Z && a-z - FIXED
- // Insert function: insert node only if alphabet letters - fix: main() will not enter values unless it's a letter
- // Double pointer instead of return values
- // Make insert() shorter
- typedef struct Node
- {
- char data;
- struct Node* next;
- } node;
- node *create_node(char data)
- {
- node* temp = (node*)malloc(sizeof(node));
- temp->data = data;
- temp->next = NULL;
- return temp;
- }
- char lower(char c)
- {
- if (c >= 'A' && c <= 'Z')
- {
- return c + 32;
- }
- else if (c >= 'a' && c <= 'z')
- {
- return c;
- }
- }
- node *insert(node* head, node *newnode)
- {
- // d->e->a->b ---- INPUT
- // a->b->d->e ---- SORTED
- // Insert(c)
- // a->b->c->d->e ---- NEW SORTED
- node* temp = head;
- if (head == NULL) // head empty
- {
- head = newnode;
- head->next = NULL;
- return head;
- }
- if (head->next == NULL) // 1 item in list
- {
- if (head->data <= newnode->data)
- {
- newnode->next = NULL;
- head->next = newnode;
- return head;
- }
- else if (newnode->data <= head->data)
- {
- newnode->next = head;
- head = newnode;
- return head;
- }
- }
- if (head != NULL && (newnode->data <= head->data))
- {
- newnode->next = head;
- head = newnode;
- return head;
- }
- while (temp != NULL) // normal
- {
- if (temp->next == NULL) // Last item
- {
- newnode->next = NULL;
- temp->next = newnode;
- return head;
- }
- else // Not last item
- {
- if (lower(temp->data) <= lower(newnode->data) && lower(newnode->data) <= lower(temp->next->data)) // b<c<d
- {
- newnode->next = temp->next;
- temp->next = newnode;
- return head;
- }
- else
- temp = temp->next;
- }
- }
- return head;
- }
- void print_list(node* head)
- {
- node* temp = head;
- while (temp != NULL)
- {
- printf("%c->", temp->data);
- temp = temp->next;
- }
- }
- node* func()
- {
- node* head = NULL;
- char input;
- node *input_node;
- printf("Insert only a-z letters. Insert any other character to stop.\n");
- printf("Please enter a value: ");
- scanf("%c", &input);
- rewind(stdin);
- while ((input >= 'a' && input <= 'z') || (input >= 'A' && input <= 'Z'))
- {
- input_node = create_node(input);
- head = insert(head, input_node);
- printf("Please enter a value: ");
- scanf("%c", &input);
- rewind(stdin);
- }
- return head;
- }
- node* reverse(node *head)
- {
- }
- int main()
- {
- node* head = NULL;
- head = func();
- print_list(head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement