Skip to content

Instantly share code, notes, and snippets.

@shameemreza
Created January 26, 2017 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shameemreza/dd2827d22cfccd8a4e004914ea6149e8 to your computer and use it in GitHub Desktop.
Save shameemreza/dd2827d22cfccd8a4e004914ea6149e8 to your computer and use it in GitHub Desktop.
Linked List in C 3
#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;
}
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 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;
}
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