Full Transcript

HW 3: Q 1; #include<iostream> using namespace std; struct node { float data; node* next; }; node* head = NULL; void insert(float value) { node* N, * curr, * pre; N = new node; N->data = value; if (head == NULL) { head = N; N->next = NULL; } else { curr = head; while (curr != NULL && curr->data < val...

HW 3: Q 1; #include<iostream> using namespace std; struct node { float data; node* next; }; node* head = NULL; void insert(float value) { node* N, * curr, * pre; N = new node; N->data = value; if (head == NULL) { head = N; N->next = NULL; } else { curr = head; while (curr != NULL && curr->data < value) { pre = curr; curr = curr->next; } pre->next = N; N->next = curr->next; } } float ave() { node* curr; float sum = 0, c = 0; if (head == NULL) return -1; else { curr = head; while (curr != NULL) { c++; sum = sum + (curr->data); curr = curr->next; } return (sum / c); } } void Delete(float value) { node* curr, * pre; curr = head; if (!head) return; if (head->data == value) { curr = head->next; delete head; head = curr; } else { while (curr != NULL && curr->data != value) { pre = curr; curr = curr->next; } pre->next = curr->next; delete curr; } } void display() { node* curr; curr = head; if (!head) return; else { while (curr != NULL) { cout << curr->data << " "; curr = curr->next; } } } int main() { float item, avereg; char ch; bool flag = false; do { cout << "1.Insert item.\n 2.Avereg.\n 3.Delete item.\n 4.Display.\n 5.Exit.\n"; cout << "Enter your choice: "; cin >> ch; switch (ch) { case '1': cout << "Enter item: "; cin >> item; insert(item); cout << endl; break; case '2': avereg = ave(); if (avereg != -1) cout << " avereg: " << avereg; else cout << "the node is Empty.\n"; cout << endl; break; case '3': cout << "select the item: "; cin >> item; cout << endl; break; case '4': display(); break; case '5': flag = true; break; default: cout << "choice agin.\n"; } } while (!flag); } Q2; #include <iostream> using namespace std; struct node { int data; node* next; }; node* head = NULL; void insert(int no) { node* N, * curr,*pre=NULL; N = new node; N->data = no; N->next=NULL; if (!head) head=N; else { curr=head; while(curr!=NULL && curr->data < no) { pre=curr; curr=curr->next; } if(pre ==NULL) { head=N; N->next=curr; } else { pre->next=N; N->next=curr; } } } void search(int item) { node* curr, * pre,*N; N = new node; if (head != NULL) { curr = head; while (curr != NULL && curr->data != item) { pre = curr; curr = curr->next; } if (curr->data == item) { cout << "greet, you Found it.\n"; pre->next=curr->next; delete curr; if(pre->next==NULL) { pre->next=N; N->data=item; } else { while(pre!=NULL) pre=pre->next; pre->next=N; N->data=item; } } else cout << "sorry, try agin.\n"; } } void display() { node* curr; curr = head; if (!head) return; else { while (curr != NULL) { cout << curr->data << " "; curr = curr->next; } } } int main() { int item; bool flag=false; char ch; insert(56); insert(8); insert(2); insert(4); insert(25); insert(90); insert(87); insert(34); insert(32); insert(60); insert(49); insert(73); do { cout << "\nEnter ( 1 ) to Guess the number in the list.\n And ( 2 ) to Eixt.\n"; cin >> ch; switch (ch) { case'1': cout << "\nEnter the number: "; cin >> item; search(item); break; case '2': cout<<"The items in list are :"; display(); flag = true; break; default: cout << "Choice 1 or 2." << endl; break; } } while (!flag); display(); }

Use Quizgecko on...
Browser
Browser