Add all function logic for linked list example

This commit is contained in:
Sambo Chea 2020-08-01 20:58:47 +07:00
parent 566ae2c7e8
commit 7aae05eae6

View File

@ -1,7 +1,8 @@
#include "stdio.h" #include "stdio.h"
#include "stdlib.h" #include "stdlib.h"
struct node { struct node
{
int data; int data;
struct node *next; struct node *next;
}; };
@ -15,13 +16,16 @@ void remove_last();
int count = 0; int count = 0;
void display_menu(); void display_menu();
int main(void) { int main(void)
{
printf("Welcome!\n"); printf("Welcome!\n");
int selected, data; int selected, data;
while (1) { while (1)
if (count == 100) { {
if (count == 100)
{
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
@ -33,22 +37,23 @@ int main(void) {
return 0; return 0;
} }
void display_menu() { void display_menu()
{
printf("1. Add first element\n"); printf("1. Add first element\n");
printf("2. Add last element\n"); printf("2. Add last element\n");
printf("3. Traverse element\n"); printf("3. Traverse element\n");
printf("4. Remove first element\n"); printf("4. Remove first element\n");
printf("5. Remove last element\n"); printf("5. Remove last element\n");
} }
void add_first(int element) { void add_first(int element)
{
// init node // init node
struct node *node; struct node *node;
// allocate the node for linkedlist // allocate the node for linkedlist
node = (struct node*)malloc(sizeof(struct node)); node = (struct node *)malloc(sizeof(struct node));
// add the data into node // add the data into node
node->data = element; node->data = element;
@ -57,7 +62,8 @@ void add_first(int element) {
count++; count++;
// check the head, if null put the data into it // check the head, if null put the data into it
if (head == NULL) { if (head == NULL)
{
// replace the node into head // replace the node into head
head = node; head = node;
@ -76,14 +82,16 @@ void add_first(int element) {
head = node; head = node;
} }
void add_last(int element) { void add_last(int element)
{
struct node *node, *temp; struct node *node, *temp;
node = (struct node*)malloc(sizeof (struct node)); node = (struct node *)malloc(sizeof(struct node));
node->data = element; node->data = element;
count++; count++;
if (head == NULL) { if (head == NULL)
{
head = node; head = node;
head->next = NULL; head->next = NULL;
return; return;
@ -92,7 +100,8 @@ void add_last(int element) {
// store current as tempo // store current as tempo
temp = node; temp = node;
while (temp->next != NULL) { while (temp->next != NULL)
{
temp = temp->next; temp = temp->next;
} }
@ -100,22 +109,82 @@ void add_last(int element) {
node->next = NULL; node->next = NULL;
} }
void traverse() { void traverse()
{
struct node *node; struct node *node;
node = head; node = head;
if (node == NULL) { if (node == NULL)
{
printf("Linked list is empty!\n"); printf("Linked list is empty!\n");
return; return;
} }
printf("Total elements count: %d in linked list.\n", count); printf("Total elements count: %d in linked list.\n", count);
while (node->next != NULL) { while (node->next != NULL)
{
printf("%d\n", node->data); printf("%d\n", node->data);
node = node->next; node = node->next;
} }
printf("%d\n", node->data); printf("%d\n", node->data);
}
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");
} }