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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | #include #include #define FALSE 0 #define TRUE 1 unsigned short dispFwd = TRUE; struct node { int data; //Data of the node struct node *nextptr, *prevptr; //Address of the next node }*HeadNode, *RearNode; void createNodeList(int n); // function to create the list void displayList(); // function to display the list void insertNode(int data, int pos); // function to insert node to pointed position in the list void insertHead(int data); // function to insert node to beginning in the list void insertRear(int data); // function to insert node to end in the list void createHead(int data); // Function to create HeadNode // Remark Refer follows link for more details on adding Color to printf() Output; // https://www.theurbanpenguin.com/4184-2/ void print_green() {printf("\033[1;32m");} void print_cyan() {printf("\033[1;36m");} void print_reset() {printf("\033[0m");} int main(){ unsigned short i, opt, optDisp; int data, pos, n; printf(" Linked List : To create and display Double Linked List : "); printf("------------------------------------------------------------- "); print_cyan(); printf(" Input the number of nodes :> "); scanf("%d", &n); createNodeList(n); displayList(); while(1){ print_cyan(); printf(" ***Double Linked List***"); printf(" [1] Display data order "); printf(" [2] Insert new node at any position "); printf(" [3] Insert new node at the beginig of list "); printf(" [4] Insert new node at the end of list "); printf(" [5] Exit "); printf(" Select Operation to the list :> "); scanf("%d", &opt); switch (opt){ case 1: printf(" ***Display data order***"); printf(" [1] Forward "); printf(" [2] Reverse "); printf(" Select display data order to the list :> "); scanf("%d", & optDisp); switch(optDisp){ case 1: dispFwd = TRUE; break; case 2: dispFwd = FALSE; break; default: printf (">: Invalid Input! No action being taken! "); } displayList(); break; case 2: printf(" ***Insert data request*** Add new data to the list: "); scanf("%d", &data); if (HeadNode == NULL) createHead(data); else{ printf(" Enter the insert position:> "); scanf("%d", &pos); insertNode(data, pos); } printf(" >: Data after inserted in the list are : "); displayList(); break; case 3: printf(" ***Insert data request*** Add new data to the list: "); scanf("%d", &data); if (HeadNode == NULL) createHead(data); else insertHead(data); printf(" >: Data after inserted in the list are : "); displayList(); break; case 4: printf(" ***Insert data request*** Add new data to the list: "); scanf("%d", &data); if (HeadNode == NULL) createHead(data); else insertRear(data); printf(" >: Data after inserted in the list are : "); displayList(); break; case 5: goto exitloop; default: printf (">: Invalid Input! No action being taken! "); } } exitloop: ; return 0; } void createNodeList(int n){ struct node *stNode; int data, i; for (i=1; i<=n; i++){ stNode = (struct node *)malloc(sizeof(struct node)); if(stNode == NULL){ //check whether the fnnode is NULL and if so no memory allocation printf(" Memory can not be allocated."); break; } else{ // reads data for the node through keyboard printf(" Input data for node %d : ", i); scanf("%d", &data); stNode->data = data; if (HeadNode == NULL){ stNode->prevptr = NULL; // links the address field to NULL stNode->nextptr = NULL; // links the address field to NULL HeadNode = RearNode = stNode; } else{ stNode->nextptr = NULL; stNode->prevptr = RearNode; RearNode->nextptr = stNode; RearNode = stNode; } } } } void createHead(int data){ struct node *stNode; stNode = (struct node *)malloc(sizeof(struct node)); //check whether the fnnode is NULL and if so no memory allocation if(stNode == NULL) printf(" Memory can not be allocated."); else{ stNode->data = data; stNode->prevptr = NULL; // links the address field to NULL stNode->nextptr = NULL; // links the address field to NULL HeadNode = RearNode = stNode; } } void insertHead(int data){ struct node *stNode; stNode = (struct node*)malloc(sizeof(struct node)); if(stNode == NULL) printf(" Memory can not be allocated."); else { stNode->data = data; stNode->prevptr = NULL; stNode->nextptr = HeadNode; //Links the address part HeadNode->prevptr = stNode; HeadNode = stNode; //Makes stnode as first node } } void insertRear(int data){ struct node *stNode; stNode = (struct node*)malloc(sizeof(struct node)); if(stNode == NULL) printf(" Memory can not be allocated."); else { stNode->data = data; stNode->prevptr = RearNode; stNode->nextptr = NULL; //Links the address part RearNode->nextptr = stNode; RearNode = stNode; //Makes stnode as first node } } void insertNode(int data, int pos){ struct node *stNode, *PreNode; int i, tpos = 1; stNode = (struct node*)malloc(sizeof(struct node)); if(stNode == NULL) printf(" Memory can not be allocated."); else { stNode->data = data; //Links the data part //Case A: When the insert location is at the first Node (i.e., Headnode) if (pos==1){ stNode->prevptr = NULL; stNode->nextptr = HeadNode; //Links the address part HeadNode->prevptr = stNode; HeadNode = stNode; //Makes stnode as first node } //Case B: Insert a new node at the middle of Singly Linked List else{ PreNode = HeadNode; for(i=2; inextptr; if(PreNode == NULL) break; } if(PreNode != NULL){ stNode->nextptr = PreNode->nextptr; // PreNode->nextptr pointed the current Node at "pos" stNode->prevptr = PreNode; PreNode->nextptr = stNode; if (stNode->nextptr == NULL) RearNode = stNode; } else printf(" Insert is not possible to the given position. "); } } } void displayList(){ print_green(); struct node *stNode; if(HeadNode == NULL || RearNode == NULL) printf(" List is empty."); else { if(dispFwd){ stNode = HeadNode; while(stNode != NULL){ printf(" Data = %d ", stNode->data); // prints the data of current node stNode = stNode->nextptr; // advances the position of current node } } else{ stNode = RearNode; while(stNode != NULL){ printf(" Data = %d ", stNode->data); // prints the data of current node stNode = stNode->prevptr; // advances the position of current node } |