How do you create a LinkedList::Add to the head of the list?With only the Data,H
ID: 3690709 • Letter: H
Question
How do you create a LinkedList::Add to the head of the list?With only the Data,Head,Next, and a Constant Interger only known node are A and prev. Without creating a new node or using new or malloc
void List::Add(const x){
Add code
}
Explanation / Answer
/* * C Program to Implement Singly Linked List using Dynamic Memory Allocation */ #include #include #define ISEMPTY printf(" EMPTY LIST:"); /* * Node Declaration */ struct node { int value; struct node *next; }; snode* create_node(int); void insert_node_first(); void insert_node_last(); void insert_node_pos(); void sorted_ascend(); void delete_pos(); void search(); void update_val(); void display(); void rev_display(snode *); typedef struct node snode; snode *newnode, *ptr, *prev, *temp; snode *first = NULL, *last = NULL; /* * Main :contains menu */ int main() { int ch; char ans = 'Y'; while (ans == 'Y'||ans == 'y') { printf(" --------------------------------- "); printf(" Operations on singly linked list "); printf(" --------------------------------- "); printf(" 1.Insert node at first"); printf(" 2.Insert node at last"); printf(" 3.Insert node at position"); printf(" 4.Sorted Linked List in Ascending Order"); printf(" 5.Delete Node from any Position"); printf(" 6.Update Node Value"); printf(" 7.Search Element in the linked list"); printf(" 8.Display List from Beginning to end"); printf(" 9.Display List from end using Recursion"); printf(" 10.Exit "); printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ "); printf(" Enter your choice"); scanf("%d", &ch); switch (ch) { case 1: printf(" ...Inserting node at first... "); insert_node_first(); break; case 2: printf(" ...Inserting node at last... "); insert_node_last(); break; case 3: printf(" ...Inserting node at position... "); insert_node_pos(); break; case 4: printf(" ...Sorted Linked List in Ascending Order... "); sorted_ascend(); break; case 5: printf(" ...Deleting Node from any Position... "); delete_pos(); break; case 6: printf(" ...Updating Node Value... "); update_val(); break; case 7: printf(" ...Searching Element in the List... "); search(); break; case 8: printf(" ...Displaying List From Beginning to End... "); display(); break; case 9: printf(" ...Displaying List From End using Recursion... "); rev_display(first); break; case 10: printf(" ...Exiting... "); return 0; break; default: printf(" ...Invalid Choice... "); break; } printf(" YOU WANT TO CONTINUE (Y/N)"); scanf(" %c", &ans); } return 0; } /* * Creating Node */ snode* create_node(int val) { newnode = (snode *)malloc(sizeof(snode)); if (newnode == NULL) { printf(" Memory was not allocated"); return 0; } else { newnode->value = val; newnode->next = NULL; return newnode; } } /* * Inserting Node at First */ void insert_node_first() { int val; printf(" Enter the value for the node:"); scanf("%d", &val); newnode = create_node(val); if (first == last && first == NULL) { first = last = newnode; first->next = NULL; last->next = NULL; } else { temp = first; first = newnode; first->next = temp; } printf(" ----INSERTED----"); } /* * Inserting Node at Last */ void insert_node_last() { int val; printf(" Enter the value for the Node:"); scanf("%d", &val); newnode = create_node(val); if (first == last && last == NULL) { first = last = newnode; first->next = NULL; last->next = NULL; } else { last->next = newnode; last = newnode; last->next = NULL; } printf(" ----INSERTED----"); } /* * Inserting Node at position */ void insert_node_pos() { int pos, val, cnt = 0, i; printf(" Enter the value for the Node:"); scanf("%d", &val); newnode = create_node(val); printf(" Enter the position "); scanf("%d", &pos); ptr = first; while (ptr != NULL) { ptr = ptr->next; cnt++; } if (pos == 1) { if (first == last && first == NULL) { first = last = newnode; first->next = NULL; last->next = NULL; } else { temp = first; first = newnode; first->next = temp; } printf(" Inserted"); } else if (pos>1 && posnext = newnode; newnode->next = ptr; printf(" ----INSERTED----"); } else { printf("Position is out of range"); } } /* * Sorted Linked List */ void sorted_ascend() { snode *nxt; int t; if (first == NULL) { ISEMPTY; printf(":No elements to sort "); } else { for (ptr = first;ptr != NULL;ptr = ptr->next) { for (nxt = ptr->next;nxt != NULL;nxt = nxt->next) { if (ptr->value > nxt->value) { t = ptr->value; ptr->value = nxt->value; nxt->value = t; } } } printf(" ---Sorted List---"); for (ptr = first;ptr != NULL;ptr = ptr->next) { printf("%d ", ptr->value); } } } /* * Delete Node from specified position in a non-empty list */ void delete_pos() { int pos, cnt = 0, i; if (first == NULL) { ISEMPTY; printf(":No node to delete "); } else { printf(" Enter the position of value to be deleted:"); scanf(" %d", &pos); ptr = first; if (pos == 1) { first = ptr->next; printf(" Element deleted"); } else { while (ptr != NULL) { ptr = ptr->next; cnt = cnt + 1; } if (pos > 0 && pos next = ptr->next; } else { printf("Position is out of range"); } free(ptr); printf(" Element deleted"); } } } /* * Updating Node value in a non-empty list */ void update_val() { int oldval, newval, flag = 0; if (first == NULL) { ISEMPTY; printf(":No nodes in the list to update "); } else { printf(" Enter the value to be updated:"); scanf("%d", &oldval); printf(" Enter the newvalue:"); scanf("%d", &newval); for (ptr = first;ptr != NULL;ptr = ptr->next) { if (ptr->value == oldval) { ptr->value = newval; flag = 1; break; } } if (flag == 1) { printf(" Updated Successfully"); } else { printf(" Value not found in List"); } } } /* * searching an element in a non-empty list */ void search() { int flag = 0, key, pos = 0; if (first == NULL) { ISEMPTY; printf(":No nodes in the list "); } else { printf(" Enter the value to search"); scanf("%d", &key); for (ptr = first;ptr != NULL;ptr = ptr->next) { pos = pos + 1; if (ptr->value == key) { flag = 1; break; } } if (flag == 1) { printf(" Element %d found at %d position ", key, pos); } else { printf(" Element %d not found in list ", key); } } } /* * Displays non-empty List from Beginning to End */ void display() { if (first == NULL) { ISEMPTY; printf(":No nodes in the list to display "); } else { for (ptr = first;ptr != NULL;ptr = ptr->next) { printf("%d ", ptr->value); } } } /* * Display non-empty list in Reverse Order */ void rev_display(snode *ptr) { int val; if (ptr == NULL) { ISEMPTY; printf(":No nodes to display "); } else { if (ptr != NULL) { val = ptr->value; rev_display(ptr->next); printf("%d ", val); } } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.