C++ Coding Help Provide two different implementations, an array and a linked lis
ID: 3885231 • Letter: C
Question
C++ Coding Help
Provide two different implementations, an array and a linked list, to maintain a list of names (two separate programs). The following operations are available: insert rear, insert front, remove a particular element, and print the whole list. Do not implement an ADT (i.e., a class with data and operations). Just set up a fixed size array or a linked list of nodes in main and provide code in main or functions/static methods to perform insert, remove, and print. You can set up a menu or hard code some test cases to test your implementations.
Explanation / Answer
#include using namespace std; struct Node { int data; Node* next; }; // only for the 1st Node void initNode(struct Node *head,int n){ head->data = n; head->next =NULL; } // apending void addNode(struct Node *head, int n) { Node *newNode = new Node; newNode->data = n; newNode->next = NULL; Node *cur = head; while(cur) { if(cur->next == NULL) { cur->next = newNode; return; } cur = cur->next; } } void insertFront(struct Node **head, int n) { Node *newNode = new Node; newNode->data = n; newNode->next = *head; *head = newNode; } struct Node *searchNode(struct Node *head, int n) { Node *cur = head; while(cur) { if(cur->data == n) return cur; cur = cur->next; } cout next; delete ptrDel; return true; } cur = cur->next; } return false; } /* reverse the list */ struct Node* reverse(struct Node** head) { Node *parent = *head; Node *me = parent->next; Node *child = me->next; /* make parent as tail */ parent->next = NULL; while(child) { me->next = parent; parent = me; me = child; child = child->next; } me->next = parent; *head = me; return *head; } /* Creating a copy of a linked list */ void copyLinkedList(struct Node *node, struct Node **pNew) { if(node != NULL) { *pNew = new Node; (*pNew)->data = node->data; (*pNew)->next = NULL; copyLinkedList(node->next, &((*pNew)->next)); } } /* Compare two linked list */ /* return value: same(1), different(0) */ int compareLinkedList(struct Node *node1, struct Node *node2) { static int flag; /* both lists are NULL */ if(node1 == NULL && node2 == NULL) { flag = 1; } else { if(node1 == NULL || node2 == NULL) flag = 0; else if(node1->data != node2->data) flag = 0; else compareLinkedList(node1->next, node2->next); } return flag; } void deleteLinkedList(struct Node **node) { struct Node *tmpNode; while(*node) { tmpNode = *node; *node = tmpNode->next; delete tmpNode; } } void display(struct Node *head) { Node *list = head; while(list) { coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.