Created
January 26, 2017 09:51
-
-
Save shameemreza/b66114fec1d64cfd51e3922e41c1e7ef to your computer and use it in GitHub Desktop.
Linked List in C 2
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; | |
while (current != NULL) { | |
printf("%d\n", current->val); | |
current = current->next; | |
} | |
} | |
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 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 0 to exit.\n"); | |
scanf("%d", &n); | |
switch(n) { | |
case 1: | |
print(head); | |
break; | |
case 2: | |
push_pichone(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