Beruflich Dokumente
Kultur Dokumente
for an element specified by the user. Print the result of the search appropriately. Use the triple <row, column, value> to represent an element in the sparse matrix.
#include <stdio.h> #include <stdlib.h> #include<stdlib.h> #define MAX_SIZE 100 int main() { int i,r,c,v,key; typedef struct { int row,col,val; }sparse; sparse a[MAX_SIZE]; printf("enter the number of rows,columns,values\n"); scanf("%d%d%d",&r,&c,&v); a[0].row=r; a[0].col=c; a[0].val=v; printf("enter elements"); printf("\nrow\tcol\tval\n"); for(i=0;i<=v;i++) scanf("%d%d%d",&a[i].row,&a[i].col,&a[i].val);
printf("enter the key\n"); scanf("%d",&key); for(i=1;i<=v;i++) { if(a[i].val==key){ printf("successful\n"); printf("element is found at %d row,%d coloumn",a[i].row,a[i].col); exit(0); } } printf("failure\n"); return 0; }
Design, develop, and execute a program in C++ to create a class called LIST (linked list) with member functions to insert an element at the front of the list as well as to delete an element from the front of the list. Demonstrate all the functions after creating a list object.
class node { public: int info; node *link; }; //class list to construct the singly linked list class list { node *first; public: list() //constructor of class list { first = NULL; } int insert_front(); int delete_front();
int display(); }; //member function to insert the element at the front of the list int list::insert_front() { node *temp; temp = new node; temp->link = NULL; cout<<"Enter item: "; cin>>temp->info; if(first == NULL) first = temp; else { temp->link = first; first = temp; } cout<<"Element "<<temp->info<<" inserted successfully\n"; } //member function to delete an element from the front of the list int list::delete_front() { if(first == NULL) { cout<<"\nList EMPTY.\n";
return 0; } else { node *temp; temp = first; first= first->link; cout<<"Deleted item: "<<temp->info; delete temp; } } //member function to display the singly linked list int list::display() { if(first == NULL) { cout<<"\nList EMPTY.\n"; return 0; } else { node *temp; temp = first; cout<<"List: "; while(temp != NULL) { cout<<" "<<temp->info;
temp=temp->link; } cout<<endl; } } int main() { list l; int ch; for(;;) { cout<<"\n Menu"; cout<<"\n 1. Insert front"; cout<<"\n 2. Delete front"; cout<<"\n 3. Display"; cout<<"\n 4. Exit\n"; cout<<" Your choice: "; cin>>ch; switch(ch) { case 1: l.insert_front(); break; case 2: l.delete_front(); break; case 3: l.display(); break;
case 4: exit(0); }
} }