Skip to main content

Posts

Showing posts with the label #cpp

Program to add a node to rear of circular linked list

 Let us write Write a program to add a node to the rear of a circular linked list. Now a circular linked list has no end. That is to say, the last node in the list points back to the head of the list. So finding the last node will be slightly tricky. let temp = head while temp->next !=head temp = temp->next Once we find the last node, we append the new node here. But we must not forget to point his new node back to head. lastnode->next = newnode; newnode->next = head; The complexity is O(n).   Here is the complete program #include<iostream> using namespace std; struct node { int n; node *next; }; class linked_list { node *head; public: linked_list(int val = 0) { if(val==0) head = NULL; else{ head = new node; head->n = val; head->next = NULL; } } node* find_last_node() { if(head==NULL || head->next==head) ...

Silly sentence

Do you want to display silly sentences? And using code? Have three arrays with subjects, objects and verbs and randomize them.  I have used latin words in the program, you can replace them with any language of your choice and amuse your friends. #include<iostream> using namespace std; class sentence { string subject; string object; string verb; public: sentence(); sentence(string,string,string); void print_sentence(); }; sentence::sentence() { subject = ""; object = ""; verb = ""; } sentence::sentence(string sub,string obj,string verb) { this->subject = sub; this->object = obj; this->verb = verb; } void sentence::print_sentence() { cout<<subject<<" "<<object<<" "<<verb<<endl; } int main() { string sub_arr[]= {"agricola","regina","filia",...