Untitled
unknown
c_cpp
a year ago
3.3 kB
2
Indexable
Never
#include <iostream> #include <string> using namespace std; struct Node { int num; string name; Node* nextlink; Node* prevlink; }; void display(Node* head) { if (head != NULL) { while (head != NULL) { cout << head->name << " "; cout << head->num << endl; head = head->nextlink; } } else { cout << "The List is Empty!" << endl; } } void deleteData(Node* *head, Node* *tail) { //Delete from front (head) //Node* d = *head; //*head = (*head)->link; //*head->prevlink = NULL; //free(d); //Delete from back //Node* d = *head; //while (d->link->link != NULL) //d = d->link; //Node* x = d; //x = x->link; //free(x); //d->link = NULL; //Delete from back (if have tail) //Node* d = *head; //while (d->link->link != NULL) //d = d->link; //free(*tail); //d->link = NULL; //(*tail) = d; //Delete from Middle int p = 2; //Specify Position of the node Node* d = *head; for (int i = 0; i < 2; i++) { d = d->nextlink; } Node* x = d; d->nextlink = d->nextlink->nextlink; d->nextlink->prevlink = d; x = x->nextlink; free(x); } void createNode(Node* *head, Node* *tail, int num, string x) { if (*head == NULL) { Node* n = new Node(); //Creates a node n->num = num; n->name = x; n->nextlink = NULL; n->prevlink = NULL; *head = *tail = n; } else { //push front (push to head) Node* n = new Node(); n->num = num; n->name = x; n->nextlink = *head; n->prevlink = NULL; (*head)->prevlink = n; *head = n; //push back (push to back) //Node* nav = *head; //while (nav->link != NULL) //nav = nav->nextlink; //Node* n = new Node(); //n->num = num; //n->name = x; //n->prevlink = nav; //n->nextlink = NULL; //nav->link = n; //push in the middle of the node //int p = 2; //Specify Position of the node //Node* nav = *head; //for (int i = 0; i < 2; i++) { //nav = nav->link; //} //Node* n = new Node(); //n->num = num; //n->name = x; //n->prevlink = nav; //n->nextlink = nav->nextlink; //n->nextlink->prevlink = n; //nav->nextlink = n; } } void addNode(Node* head, Node* tail, int num, string x) { if (head == NULL) { Node* n = new Node(); //Creates a node n->num = num; n->name = x; n->link = NULL; head = n; tail = n; } else { //push front (push to head) Node* n = new Node(); n->num = num; n->name = x; n->link = head; head = n; //push back (push to back) //Node* nav = *head; //while (nav->link != NULL) //nav = nav->link; //Node* n = new Node(); //n->num = num; //n->name = x; //n->link = NULL; //nav->link = n; //push in the middle of the node //int p = 2; //Specify Position of the node //Node* nav = *head; //for (int i = 0; i < 2; i++) { //nav = nav->link; //} //Node* n = new Node(); //n->num = num; //n->name = x; //n->link = nav->link; //nav->link = n; } } int main() { Node* Head = NULL; Node* Tail = NULL; int x; string y; cout << "Enter a Number: "; cin >> x; cout << "Enter a String: "; cin >> y; addNode(Head, Tail, x, y); createNode(&Head, &Tail, x, y); display(Head); return 0; }