This is just a simple linked creation with the help of c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
#include <stdio.h> #include <conio.h> typedef struct node // structure node { int info; // a information that is to be stored struct node *next; // create a next name pointer which will point to node type }node_t; // node_t object is created node_t *first,*rear,*printing; // first is for storing first information pointer // rear is to store where we have last stored the information // printing is to track while printing. void createlist(); // it create a empty list void insert_element(int); // it will create a new node and insert that into list void print_list(); // it will print the list void main() { int info,choice; clrscr(); // create list at first createlist(); // loop till user wants to exit do { // ask user for choices printf("Enter your choice"); printf("Press 1 to Enter the Number\n"); printf("Press 2 to exit\n"); scanf("%d",&choice); // if one call insert_element() if(choice==1) { printf(" \n Enter the element"); scanf("%d",&info); insert_element(info); } } while(choice!=2); // if choice is 2 break the loop print_list(); // prints the list getch(); } void createlist() { node_t *createlist=malloc(sizeof(node_t)); // create a new node // intialize everyting with null createlist->next=NULL; createlist->info=NULL; // initialize first and rear with the first node created first=createlist; rear=createlist; } void insert_element(int element) { node_t *new_node=malloc(sizeof(node_t)); // create a new node // check if list is empty // if yes then enter the first element if(first->info==NULL) { new_node->info=element; first->info=element; } // if not first node then // enter the info in newly created node and // point its next to our last created node. // and make our rear pointing to newly created node. else { new_node->info=element; new_node->next=rear; rear=new_node; } } // prints the whole list void print_list() { printing=rear; // intializes the printing // while loop -- until a node with next=null is not found while(printing!=NULL) { printf("%d ",printing->info); printing=printing->next; } } |