The C++ program creates a singly linked list using the Node struct defined below
ID: 642027 • Letter: T
Question
The C++ program creates a singly linked list using the Node struct defined below.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int d=0, Node* p=0) : data(d), next(p) {}
Node* insert_after(int value);
void delete_after();
void display_list() const;
};
Node* Node::insert_after(int value) {
Node *p = new Node(value);
p->next = next;
next = p;
return p;
}
void Node::delete_after(){
Node *p = next;
if (p != 0) {
next = p->next;
delete p;
}
}
void Node::display_list() {
Node *head = this;
while(head != 0) {
cout << head->data << " ";
head = head->next;
}
cout << ' ';
}
int main(){
Node *head= new Node(10);
Node *p = head->insert_after(20);
p = p->insert_after(30);
p->insert_after(40);
head->display_list();
head->delete_after();
p->delete_after();
head->display_list();
return 0;
}
Explanation / Answer
#include #include //Declare Structure for the Link List struct node { int data; struct node *next; }; //Define struct node as a link datatype typedef struct node link; // Initialize a starting pointer link *start=NULL; //Insert Node at Begining void insert_beg() { link *n1; int num; printf("Enter the number you want to enter:"); scanf("%d",&num); //Allocates the memory of new node n1=(link *)malloc(sizeof (link *)); //Stores the inputed num to the data portion of node n1->data=num; if(start==NULL) { start=n1; n1->next=NULL; } else { n1->next=start; start=n1; } } //Insert Node at Last void insert_end() { link *n1,*ptr; int num; printf("Enter the number:"); scanf("%d",&num); //Allocates the memory for new node n1=(link *)malloc(sizeof (link *)); //Stores the inputed num to the data portion of node n1->data=num; if(start==NULL) { start=n1; n1->next=NULL; } else { ptr=start; while(ptr->next != NULL) { ptr=ptr->next; } ptr->next=n1; n1->next=NULL; } } //Display Elements of the Link void display() { link *ptr=start; //Check if link list is empty if(ptr==NULL) { printf("Linklists empty "); } else { while(ptr != NULL) { printf(" %d -> ",ptr->data); ptr=ptr->next; } } } //Delete Node from the begining void delete_beg() { link *n1; n1=start; //Check if Link list is empty if (start==NULL) { printf("Link List is Empty "); } //Deletes the element else { start=n1->next; free(n1); printf("Element Deleted Succesfully"); } } //Delete From last //optional void delete_end() { link *ptr,*ptr1; //Check is link list is empty if(start==NULL) { printf("Link List is Empty"); } else { while(ptr->next != NULL) { ptr1=ptr; ptr=ptr->next; } ptr1->next=NULL; free(ptr); } } //Delet before a specific node void delete_before() { link *ptr1,*ptr2,*ptr; int val; //Check if link list is empty if(start==NULL) { printf("Linked List is Empty "); } else { //Get the value from which the node has to be deleted before printf("From which Value you want to delete before?:"); scanf("%d",&val); ptr=start; while(ptr->next->data != val) { ptr2=ptr1; ptr=ptr->next; } ptr2->next=ptr1->next; free(ptr1); } } //Delete After Element void delete_after() { link *ptr,*ptr1; int val; ptr=start; //Check if link list is empty if(start==NULL) { printf("Linked List is empty"); } else { //Get the value from which the node has to be deleted before printf("Enter the data of node:"); scanf("%d",&val); while(ptr1->data != val) { ptr1=ptr; ptr=ptr->next; } ptr1->next=ptr->next; free(ptr); printf("Node Deleted"); } } //Sort the Linked List void sort() { link *ptr,*ptr1; ptr=start; int temp; while(ptr!=NULL) { ptr1=ptr->next; while(ptr1!=NULL) { if(ptr1->data data) { temp=ptr->data; ptr->data=ptr1->data; ptr1->data=temp; } ptr1=ptr1->next; } ptr=ptr->next; } } //Insert Element in sorted linked list void insert_sorted() { link *ptr1,*ptr2,*new_node; int n; //Calls Sort function to sort the elements sort(); //Allocates the memory new_node=(link *)malloc(sizeof ( link *)); printf("Enter The value you want to add in the sorted link list:"); scanf("%d",&n); //Stores the Entered value to the new node's data part new_node->data=n; ptr1=start; while(ptr2->data next; } ptr2->next=new_node; new_node->next=ptr1; } //Main Execution void main() { int choice; clrscr(); while(1) { printf(" "); printf(""); printf(" 1. Insert at beginig"); printf(" 2. Insert at End"); printf(" 3. Display Linked List"); printf(" 4. Delete at Begining"); printf(" 5. Delete at End"); printf(" 6. Delete Before"); printf(" 7. Delete After"); printf(" 8. Insert into sorted linked list"); printf(" 9. Sort the linked list"); printf(" Enter 0 to exit"); printf(" "); printf(" Enter Your Choice:"); scanf("%d",&choice); switch(choice) { case 1: insert_beg(); break; case 2: insert_end(); break; case 3: display(); break; case 4: delete_beg(); break; case 5: delete_end(); break; case 6: delete_before(); break; case 7: delete_after(); break; case 8: insert_sorted(); break; case 9: sort(); break; case 0: exit(1); default: printf("Enter Valid Choice"); } } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.