2020-08-01 20:32:55 +07:00
|
|
|
#include "stdio.h"
|
|
|
|
#include "stdlib.h"
|
|
|
|
|
2020-08-01 20:58:47 +07:00
|
|
|
struct node
|
|
|
|
{
|
2020-08-01 20:32:55 +07:00
|
|
|
int data;
|
|
|
|
struct node *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct node *head = NULL;
|
|
|
|
void add_first(int);
|
|
|
|
void add_last(int);
|
|
|
|
void traverse();
|
|
|
|
void remove_first();
|
|
|
|
void remove_last();
|
|
|
|
int count = 0;
|
|
|
|
void display_menu();
|
|
|
|
|
2020-08-01 20:58:47 +07:00
|
|
|
int main(void)
|
|
|
|
{
|
2020-08-01 20:32:55 +07:00
|
|
|
printf("Welcome!\n");
|
|
|
|
|
|
|
|
int selected, data;
|
|
|
|
|
2020-08-01 20:58:47 +07:00
|
|
|
while (1)
|
|
|
|
{
|
2020-08-01 21:13:49 +07:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
// show menu
|
|
|
|
display_menu();
|
|
|
|
|
|
|
|
printf("Enter your option: ");
|
|
|
|
scanf("%d", &selected);
|
|
|
|
|
|
|
|
if (selected == 1)
|
|
|
|
{
|
|
|
|
printf("Enter you element to add first: ");
|
|
|
|
scanf("%d", &data);
|
|
|
|
add_first(data);
|
|
|
|
}
|
|
|
|
else if (selected == 2)
|
|
|
|
{
|
|
|
|
printf("Enter you element to add last: ");
|
|
|
|
scanf("%d", &data);
|
|
|
|
add_last(data);
|
|
|
|
}
|
|
|
|
else if (selected == 3)
|
|
|
|
{
|
|
|
|
traverse();
|
|
|
|
}
|
|
|
|
else if (selected == 4)
|
2020-08-01 20:58:47 +07:00
|
|
|
{
|
2020-08-01 21:13:49 +07:00
|
|
|
remove_first;
|
|
|
|
}
|
|
|
|
else if (selected == 5)
|
|
|
|
{
|
|
|
|
remove_last();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("\nGoodbye!");
|
2020-08-01 20:32:55 +07:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-01 20:58:47 +07:00
|
|
|
void display_menu()
|
|
|
|
{
|
2020-08-01 20:32:55 +07:00
|
|
|
printf("1. Add first element\n");
|
|
|
|
printf("2. Add last element\n");
|
|
|
|
printf("3. Traverse element\n");
|
|
|
|
printf("4. Remove first element\n");
|
|
|
|
printf("5. Remove last element\n");
|
2020-08-01 21:13:49 +07:00
|
|
|
printf("Any to quite program\n");
|
2020-08-01 20:32:55 +07:00
|
|
|
}
|
|
|
|
|
2020-08-01 20:58:47 +07:00
|
|
|
void add_first(int element)
|
|
|
|
{
|
2020-08-01 20:32:55 +07:00
|
|
|
// init node
|
|
|
|
struct node *node;
|
|
|
|
|
|
|
|
// allocate the node for linkedlist
|
2020-08-01 20:58:47 +07:00
|
|
|
node = (struct node *)malloc(sizeof(struct node));
|
2020-08-01 20:32:55 +07:00
|
|
|
|
|
|
|
// add the data into node
|
|
|
|
node->data = element;
|
|
|
|
|
|
|
|
// count the nodes, that added into data
|
|
|
|
count++;
|
|
|
|
|
|
|
|
// check the head, if null put the data into it
|
2020-08-01 20:58:47 +07:00
|
|
|
if (head == NULL)
|
|
|
|
{
|
2020-08-01 20:32:55 +07:00
|
|
|
// replace the node into head
|
|
|
|
head = node;
|
|
|
|
|
|
|
|
// set the next of head into null
|
|
|
|
head->next = NULL;
|
|
|
|
|
|
|
|
// return or end this process
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-01 20:48:36 +07:00
|
|
|
// put the first element of the head
|
|
|
|
// because we need the old head into the next of new head
|
2020-08-01 20:32:55 +07:00
|
|
|
node->next = head;
|
2020-08-01 20:48:36 +07:00
|
|
|
|
|
|
|
// replace the head node from current node
|
2020-08-01 20:32:55 +07:00
|
|
|
head = node;
|
2020-08-01 20:48:36 +07:00
|
|
|
}
|
|
|
|
|
2020-08-01 20:58:47 +07:00
|
|
|
void add_last(int element)
|
|
|
|
{
|
2020-08-01 20:48:36 +07:00
|
|
|
struct node *node, *temp;
|
|
|
|
|
2020-08-01 20:58:47 +07:00
|
|
|
node = (struct node *)malloc(sizeof(struct node));
|
2020-08-01 20:48:36 +07:00
|
|
|
node->data = element;
|
|
|
|
count++;
|
|
|
|
|
2020-08-01 20:58:47 +07:00
|
|
|
if (head == NULL)
|
|
|
|
{
|
2020-08-01 20:48:36 +07:00
|
|
|
head = node;
|
|
|
|
head->next = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// store current as tempo
|
|
|
|
temp = node;
|
|
|
|
|
2020-08-01 20:58:47 +07:00
|
|
|
while (temp->next != NULL)
|
|
|
|
{
|
2020-08-01 20:48:36 +07:00
|
|
|
temp = temp->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp->next = node;
|
|
|
|
node->next = NULL;
|
|
|
|
}
|
|
|
|
|
2020-08-01 20:58:47 +07:00
|
|
|
void traverse()
|
|
|
|
{
|
2020-08-01 20:48:36 +07:00
|
|
|
struct node *node;
|
|
|
|
|
|
|
|
node = head;
|
|
|
|
|
2020-08-01 20:58:47 +07:00
|
|
|
if (node == NULL)
|
|
|
|
{
|
2020-08-01 20:48:36 +07:00
|
|
|
printf("Linked list is empty!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Total elements count: %d in linked list.\n", count);
|
|
|
|
|
2020-08-01 20:58:47 +07:00
|
|
|
while (node->next != NULL)
|
|
|
|
{
|
2020-08-01 20:48:36 +07:00
|
|
|
printf("%d\n", node->data);
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%d\n", node->data);
|
2020-08-01 20:58:47 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
void remove_first()
|
|
|
|
{
|
|
|
|
struct node *node;
|
|
|
|
int data;
|
|
|
|
|
|
|
|
if (head == NULL)
|
|
|
|
{
|
|
|
|
printf("No elements found!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = head->data;
|
|
|
|
node = head->next;
|
|
|
|
free(head);
|
|
|
|
head = node;
|
|
|
|
count--;
|
|
|
|
|
|
|
|
printf("Element %d has been removed from first!\n", data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_last()
|
|
|
|
{
|
|
|
|
struct node *node, *temp;
|
|
|
|
int data;
|
|
|
|
|
|
|
|
if (head == NULL)
|
|
|
|
{
|
|
|
|
printf("No elements found!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
count--;
|
|
|
|
|
|
|
|
if (head->next == NULL)
|
|
|
|
{
|
|
|
|
data = head->data;
|
|
|
|
free(head);
|
|
|
|
head = NULL;
|
|
|
|
printf("Element %d has been removed from last!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = head;
|
|
|
|
|
|
|
|
while (node->next != NULL)
|
|
|
|
{
|
|
|
|
temp = node;
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = node->data;
|
|
|
|
temp->next = NULL;
|
|
|
|
free(node);
|
|
|
|
|
|
|
|
printf("Element %d has been removed from last!\n");
|
2020-08-01 20:32:55 +07:00
|
|
|
}
|