Created
January 26, 2017 09:45
-
-
Save shameemreza/9d5ab54ca8be2e67f548eb4ba72c9d5a to your computer and use it in GitHub Desktop.
Linked List in C - 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
typedef struct node { | |
int val; | |
struct node * next; | |
} node_t; | |
void print(node_t * head) { | |
node_t * current = head; | |
printf("***The list begins***\n"); | |
while (current != NULL) { | |
printf("%d\n", current->val); | |
current = current->next; | |
} | |
printf("***The list ends***\n"); | |
} | |
void push_pichone(node_t * head) { | |
printf("Enter the integer: "); | |
int n; | |
scanf("%d", &n); | |
node_t * current = head; | |
// list er shes element ta khuje ber korbo | |
while (current->next != NULL) { | |
current = current->next; | |
} | |
// ebar amra variable add korbo | |
current->next = malloc(sizeof(node_t)); // malloc use kora khub e important | |
current->next->val = n; | |
current->next->next = NULL; | |
} | |
void push_shamne(node_t ** head) { | |
printf("Enter the integer: "); | |
int n; | |
scanf("%d", &n); | |
// notun node | |
node_t * new_node; | |
new_node = malloc(sizeof(node_t)); | |
// node er value | |
new_node->val = n; | |
new_node->next = *head; | |
// head er value bodle dicchi | |
*head = new_node; | |
} | |
int pop_shamne(node_t ** head) { | |
int ret = -1; | |
node_t * next_node = NULL; | |
// eta korchi jaate | |
if (*head == NULL) { | |
return -1; | |
} | |
next_node = (*head)->next; // head variable er next address ta rakhtichi save kore | |
ret = (*head)->val; // value ta save kore rakhtichi jaate pore return korte pari | |
free(*head); // memory free kore dicchi | |
*head = next_node; // head hishebe shei purono store kora address ta set kortichi | |
return ret; | |
} | |
void menu(node_t *head) { | |
int n; | |
printf("What operation do you want to execute?\n"); | |
printf("Press 1 to print all elements.\n"); | |
printf("Press 2 to enter an integer at the end of the list.\n"); | |
printf("Press 3 to enter an integer at the beginning of the list.\n"); | |
printf("Press 4 to remove the first item.\n"); | |
printf("Press 0 to exit.\n"); | |
scanf("%d", &n); | |
switch(n) { | |
case 1: | |
print(head); | |
break; | |
case 2: | |
push_pichone(head); | |
break; | |
case 3: | |
push_shamne(&head); | |
break; | |
case 4: | |
printf("The removed element: %d\n", pop_shamne(&head)); | |
break; | |
} | |
if (n!=0) menu(head); | |
} | |
int main() { | |
node_t *head = NULL; | |
head = malloc(sizeof(node_t)); | |
int num; | |
printf("Enter the first integer: "); | |
scanf("%d", &num); | |
head->val = num; // head er address e jei node ta ache tar val er man rakhlam num | |
head->next = NULL; // porer address hishebe rakhlam NULL. orthat ekhanei shes | |
menu(head); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment