diff --git a/Codes/Practical-A1.cpp b/Codes/Practical-A1.cpp new file mode 100644 index 0000000..ca7c00e --- /dev/null +++ b/Codes/Practical-A1.cpp @@ -0,0 +1,168 @@ +// WARNING: THIS CODE HAS NOT BEEN TESTED. +// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE. +// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS, +// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY. + +/* +Problem Statement: Consider telephone book database of N clients. Make use of a hash table implementation to quickly look up client's telephone number. Make use of two collision handling techniques and compare them using number of comparisons required to find a set of telephone numbers. + +Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/ +*/ + +// BEGINNING OF CODE +#include +#include +#define max 10 +using namespace std; + +struct client +{ + long int iPhno; + char name[20]; + +};//end of structure + +class hashtable +{ + client ht[max]; + + public: + hashtable() //constructor + { + for(int i=0;i>c.iPhno; + iPos=hash(c.iPhno); + + if(ht[iPos].iPhno==0) + { + ht[iPos]=c; + } + else + { + for(int i=iPos+1;i%max!=iPos;i++) + { + ht[i]=c; + break; + } + } + cout<<"\n Add More:"; + cin>>cAns; + }while(cAns=='y' || cAns=='Y'); +}//end of insert + +int hashtable::hash(long int key) +{ + return(key%max); +}//end of hash + +void hashtable::display() +{ + cout<<"------------------------------------"; + cout<<"\nSrno\tPhone number\n"; + cout<<"------------------------------------\n"; + for(int i=0;i>x; + for(int i=0;i>s; + for(int i=0;i>iCh; + cout<<"\n"; + + switch(iCh) + { + case 1://insert + h.insert(); + cout<<"\n"; + break; + + case 2://display + h.display(); + cout<<"\n"; + break; + + case 3://search + h.search(y); + cout<<"\n"; + break; + + case 4://delete + h.del(s); + cout<<"\n"; + break; + + case 5://exit + break; + }//end of switch + }while(iCh!=5);//end of do +return 0; +} +// END OF CODE +// EXPERIMENTAL CODE \ No newline at end of file diff --git a/Codes/Practical-A4.cpp b/Codes/Practical-A4.cpp new file mode 100644 index 0000000..3a568c2 --- /dev/null +++ b/Codes/Practical-A4.cpp @@ -0,0 +1,513 @@ +// WARNING: THIS CODE HAS NOT BEEN TESTED. +// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE. +// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS, +// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY. + +/* +Problem Statement: To create ADT that implement the "set" concept. +a. Add (newElement) -Place a value into the set +b. Remove (element) Remove the value +c. Contains (element) Return true if element is in collection +d. Size () Return number of values in collection Iterator () Return an iterator used to loop over collection +e. Intersection of two sets +f. Union of two sets +g. Difference between two sets +h. Subset + +Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/ +*/ + +// BEGINNING OF CODE +#include +using namespace std; +templateclass Set; +templateclass Iterator; +template +class Node +{ + T data; + Node *next; + public: + Node() + { + next=NULL; + } + Node(T x) + { + data=x; + next=NULL; + } + friend class Set; + friend class Iterator; +}; +template +class Iterator +{ + Node *ptr; + friend class Set; + public: + Iterator() + { ptr=NULL; } + void set(Node *&t) + { ptr=t; } + void operator++(int) + { ptr=ptr->next; } + void next() + { ptr=ptr->next; } + bool operator!=(Iterator a) + { if(a.ptr!=NULL&&ptr!=NULL) + { + if((a.ptr)->data==(ptr)->data) + return false; + return true; + } + if(a.ptr==NULL&&ptr==NULL) + return false; + return true; + } + void del() + { delete ptr; } +}; +template +class Set +{ + Node *head; + public: + Set() + { + head=NULL; + } + Iterator begin() + { + Iterator a; + a.ptr=head; + return a; + } + Iterator end() + { + Iterator a; + a.ptr=NULL; + return a; + } + int size(); + void display(); + void insert(T); + bool remove(T); + bool contain(T); + bool subSet(Set&); + Set unionOf(Set&); + Set intersectionOf(Set&); + Set differenceOf(Set&); +}; +template +int Set::size() +{ + Iterator it; + it.set(head); + int cnt=0; + while(it!=end()) + { + cnt++; + it++; + } + return cnt; +} +template +void Set::insert(T x) +{ + Node *curr=head,*prev=NULL; + while(curr!=NULL&&curr->datanext; + } + if(curr!=NULL&&curr->data==x) + { + cout<(x); + head->next=curr; + } + else + { + prev->next=new Node(x); + prev->next->next=curr; + } +} +template +void Set::display() +{ + Iterator it; + it.set(head); + while(it!=end()) + { + cout<data<<" "; + it.next(); + } + cout<<"\n"; +} +template +bool Set::contain(T x) +{ + Iterator it; + it.set(head); + while(it!=end()) + { + if(it.ptr->data==x) + return true; + it++; + } + return false; +} +template +bool Set::remove(T x) +{ + if(contain(x)) + { + Iterator curr,prev; + curr=begin(); + while(curr.ptr->data!=x) + { + prev=curr; + curr.next(); + } + if(prev.ptr==NULL) + head=head->next; + else + prev.ptr->next=curr.ptr->next; + curr.del(); + return true; + } + else + return false; +} +template +bool Set::subSet(Set &S2) +{ + if(head==NULL) + return true; + if(S2.head==NULL) + return false; + Iterator p,q; + p=begin(); + q=S2.begin(); + int cnt=0; + while(q!=S2.end()&&p!=end()) + { + if(p.ptr->data==q.ptr->data) + { + cnt++; + p++; + } + q.next(); + } + if(cnt==size()) + return true; + return false; +} +template +Set Set::unionOf(Set &S2) +{ + Set U; + Iterator p=begin(),q=S2.begin(); + Node *r=NULL; + if(p!=end()&&q!=S2.end()) + { + if(p.ptr->datadata) + { + U.head=new Node(p.ptr->data); + p++; + } + else + { + U.head=new Node(q.ptr->data); + if(p.ptr->data==q.ptr->data) + p++; + q++; + } + } + r=U.head; + while(p!=end()&&q!=S2.end()) + { + if(p.ptr->datadata) + { + r->next=new Node(p.ptr->data); + p++; + } + else + { + r->next=new Node(q.ptr->data); + if(p.ptr->data==q.ptr->data) + p++; + q++; + } + r=r->next; + } + if(r==NULL) + { + if(p.ptr==NULL) + { + U.head=new Node(q.ptr->data); + q++; + } + else + { + U.head=new Node(p.ptr->data); + p++; + } + r=U.head; + } + while(p!=end()) + { + r->next=new Node(p.ptr->data); + r=r->next; + p++; + } + while(q!=S2.end()) + { + r->next=new Node(q.ptr->data); + r=r->next; + q++; + } + return U; +} +template +Set Set::intersectionOf(Set &S2) +{ + Set I; + Iterator p=begin(),q=S2.begin(); + Node *r=NULL; + while(p!=end()&&q!=S2.end()) + { + if(p.ptr->data==q.ptr->data) + { + if(I.head==NULL) + { + I.head=new Node(p.ptr->data); + r=I.head; + } + else + r->next=new Node(p.ptr->data); + q++; + p++; + } + else if(p.ptr->data>q.ptr->data) + q++; + else + p++; + } + return I; +} +template +Set Set::differenceOf(Set &S2) +{ + Node *r; + Iterator p,q; + if(head==NULL) + { + Set D; + return D; + } + Set D; + p=begin(); + q=S2.begin(); + while(p!=end()&&q!=S2.end()) + { + if(p.ptr->data==q.ptr->data) + { + p.next(); + q.next(); + } + else + { + if(D.head==NULL) + { + D.head=new Node(p.ptr->data); + r=D.head; + } + else + { + r->next=new Node(p.ptr->data); + r=r->next; + } + p++; + } + } + if(D.head==NULL&&p!=end()) + { + D.head=new Node(p.ptr->data); + p.next(); + r=D.head; + } + while(p!=end()) + { + r->next=new Node(p.ptr->data); + p.next(); + r=r->next; + } + return D; +} +template +void operations(T var) +{ + Set S1,S2,Res; + T ele; + int work,choice; + do + { + cout<<"Enter\n1 To Work with Set 1\n2 To Work with Set 2\n0 To EXIT\n"; + cin>>work; + switch(work) + { + case 1: + do + { + cout<<"Enter\n1 To Insert\n2 To Remove\n3 To Search\n4 To Print Size\n5 To Check if Subset\n"; + cout<<"6 To Find Union\n7 To Find Intersection\n8 To Find Difference\n0 To Previous Menu\n"; + cin>>choice; + switch(choice) + { + case 1: + cout<<"Enter the Element to Insert\n"; + cin>>ele; + S1.insert(ele); + S1.display(); + break; + case 2: + cout<<"Enter the Element to Remove\n"; + cin>>ele; + if(S1.remove(ele)) + cout<<"Element Removed\n"; + else + cout<<"Element Not Present\n"; + S1.display(); + break; + case 3: + cout<<"Enter the Element to search\n"; + cin>>ele; + if(S1.contain(ele)) + cout<<"Element Present\n"; + else + cout<<"Element Not Present\n"; + break; + case 4: + cout<<"Set Size is "<>choice; + switch(choice) + { + case 1: + cout<<"Enter the Element to Insert\n"; + cin>>ele; + S2.insert(ele); + S2.display(); + break; + case 2: + cout<<"Enter the Element to Remove\n"; + cin>>ele; + if(S2.remove(ele)) + cout<<"Element Removed\n"; + else + cout<<"Element Not Present\n"; + S2.display(); + break; + case 3: + cout<<"Enter the Element to search\n"; + cin>>ele; + if(S2.contain(ele)) + cout<<"Element Present\n"; + else + cout<<"Element Not Present\n"; + break; + case 4: + cout<<"Set Size is "<>ch; + if(ch==1) + operations((int)1); + else if(ch==2) + operations((float)1.0f); + else if(ch==3) + operations('a'); + else + cout<<"Invalid Choice\n"; + return 0; +} +// END OF CODE +// EXPERIMENTAL CODE \ No newline at end of file diff --git a/Codes/Practical-B11.cpp b/Codes/Practical-B11.cpp new file mode 100644 index 0000000..4e0ebb2 --- /dev/null +++ b/Codes/Practical-B11.cpp @@ -0,0 +1,300 @@ +// WARNING: THIS CODE HAS NOT BEEN TESTED. +// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE. +// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS, +// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY. + +/* +Problem Statement: A Dictionary stores keywords & its meanings. Provide facility for adding new keywords, deleting keywords, updating values of any entry. Provide facility to display whole data sorted in ascending/ Descending order. Also find how many maximum comparisons may require for finding any keyword. Use Binary Search Tree for implementation. + +Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/ +*/ + +// BEGINNING OF CODE +#include +using namespace std; +class node +{ + public: + string word; + string meaning; + node* left=NULL; + node* right=NULL; + + node(string x, string y) + { + word = x; + meaning = y; + left = NULL; + right = NULL; + } + friend class Dictionary; +}; + +class Dictionary +{ +public: + node* root, *q; //q is parent here + Dictionary() + { + root = NULL; + q=NULL; + } + + void insert(node*, string, string); + void display_asc(node *); + void display_desc(node *); + void comparisons(node*, string); + void updateWord(node*, string); + //void deleteWord(node*, string); + //node* min_node(node *); +}; + +void Dictionary::insert(node* p, string key, string keyMeaning) +{ + if(key < p->word) + { + if(p->left != NULL) + insert(p->left, key, keyMeaning); + else + p->left = new node(key, keyMeaning); + } + else if(key > p->word) + { + if(p->right != NULL) + insert(p->right, key, keyMeaning); + else + p->right = new node(key, keyMeaning); + } +} + +void Dictionary::display_asc(node *p) //inorder +{ + if(p->left!=NULL) + display_asc(p->left); + cout<<"\n" << p->word<<" \t" << p->meaning; + if(p->right!=NULL) + display_asc(p->right); +} + +void Dictionary::display_desc(node *p) +{ + if(p->right!=NULL) + display_desc(p->right); + cout<<"\n" << p->word<<" \t" << p->meaning; + if(p->left!=NULL) + display_desc(p->left); +} + +void Dictionary::comparisons(node* p, string key) +{ + static int count = 0; + while(p!=NULL) + { + if(key < p->word) + { + count++; + p = p->left; + } + + else if(key > p->word) + { + count++; + p = p->right; + } + + else if(key == p->word) + { + count++; + cout<<"Number of comparisons to find the word: " << count; + return ; + } + } + + cout<<"\nWord not found!"; +} + +node* Dictionary::min_node(node *p) +{ + while(p->left != NULL) + { + q = p; + p = p->left; + } + return p; +} + +void Dictionary::deleteWord(node* p, string key) +{ + node *s; + while(p!=NULL) //searching for word + { + if(key < p->word) + { + q = p; + p = p->left; + } + + else if(key > p->word) + { + q=p; + p = p->right; + } + + else if(key == p->word) //word found + { + if(p->left==NULL && p->right==NULL) //no child + { + if(q->left==p) + { + delete p; + q->left=NULL; + return; + } + + if(q->right==p) + { + delete p; + q->right=NULL; + return; + } + + } + + if(p->right!=NULL && p->left==NULL) //right child only + { + if(q->right == p) + { + q->right = p->right; + delete p; + return; + } + + else if(q->left == p) + { + q->left = p->right; + delete p; + return; + } + + } + + else if(p->left!=NULL && p->right==NULL) //left child only + { + if(q->right == p) + { + q->right = p->left; + delete p; + return; + } + + else if(q->left == p) + { + q->left=p->left; + delete p; + return; + } + + } + + else if(p->left!=NULL && p->right!=NULL) + { + s = min_node(p->right); + p->word = s->word; + p->meaning = s->meaning; + deleteWord(s, s->word); + return; + + } + } + } + cout<<"\nWord NOT found!"; +} + +void Dictionary::updateWord(node* p, string key) +{ + while(p!=NULL) + { + if(key < p->word) + p = p->left; + + else if(key > p->word) + p = p->right; + + else if(key == p->word) + { + cout<<"\nEnter its new meaning: "; + cin>>p->meaning; + return; + } + } + cout<<"\nWord not found!"; +} +//f b h a d g i c e k j + +int main() +{ + int choice, n; + string newWord, searchWord, newMeaning; + Dictionary d1; + menu: + cout<<"\n\nDICTIONARY: "; + cout<<"\n\n1. Insert new words"; + cout<<"\n2. Display the dictionary in ascending order"; + cout<<"\n3. Display the dictionary in descending order"; + cout<<"\n4. Search and update a word"; + cout<<"\n5. Delete a word"; + cout<<"\n6. Comparisons"; + cout<<"\n\nEnter your choice: "; + cin>>choice; + switch(choice) + { + case 1: + cout<<"\nEnter the number of words to insert: "; + cin >> n; + for(int i=0 ; i> newWord; + cout<<"\nEnter its meaning: "; + cin >> newMeaning; + if(d1.root == NULL) + d1. root = new node(newWord, newMeaning); + else + d1.insert(d1.root, newWord, newMeaning); + } + break; + + case 2: + d1.display_asc(d1.root); + break; + + case 3: + d1.display_desc(d1.root); + break; + + case 4: + cout<<"\nEnter the word to search: "; + cin >> searchWord; + d1.updateWord(d1.root, searchWord); + break; + + case 5: + cout<<"\nEnter the word to delete: "; + cin>>searchWord; + d1.deleteWord(d1.root, searchWord); + break; + + case 6: + cout<<"\nEnter the word to find comparisons: "; + cin >> searchWord; + d1.comparisons(d1.root, searchWord); + + default: + cout<<"\nInvalid input!"; + } + if(choice!=7) + goto menu; + return 0; +} +// END OF CODE +// EXPERIMENTAL CODE \ No newline at end of file diff --git a/Codes/Practical-B5.cpp b/Codes/Practical-B5.cpp new file mode 100644 index 0000000..483b854 --- /dev/null +++ b/Codes/Practical-B5.cpp @@ -0,0 +1,119 @@ +// WARNING: THIS CODE HAS NOT BEEN TESTED. +// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE. +// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS, +// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY. + +/* +Problem Statement: A book consists of chapters, chapters consist of sections and sections consist of subsections. Construct a tree and print the nodes. Find the time and space requirements of your method. + +Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/ +*/ + +// BEGINNING OF CODE +#include +#include +using namespace std; + +struct node // Node Declaration +{ + string label; + //char label[10]; + int ch_count; + struct node *child[10]; +} * root; + +class GT // Class Declaration +{ +public: + void create_tree(); + void display(node *r1); + + GT() + { + root = NULL; + } +}; + +void GT::create_tree() +{ + int tbooks, tchapters, i, j, k; + root = new node; + cout << "Enter name of book : "; + cin.get(); + getline(cin, root->label); + cout << "Enter number of chapters in book : "; + cin >> tchapters; + root->ch_count = tchapters; + for (i = 0; i < tchapters; i++) + { + root->child[i] = new node; + cout << "Enter the name of Chapter " << i + 1 << " : "; + cin.get(); + getline(cin, root->child[i]->label); + cout << "Enter number of sections in Chapter : " << root->child[i]->label << " : "; + cin >> root->child[i]->ch_count; + for (j = 0; j < root->child[i]->ch_count; j++) + { + root->child[i]->child[j] = new node; + cout << "Enter Name of Section " << j + 1 << " : "; + cin.get(); + getline(cin, root->child[i]->child[j]->label); + } + } +} + +void GT::display(node *r1) +{ + int i, j, k, tchapters; + if (r1 != NULL) + { + cout << "\n-----Book Hierarchy---"; + cout << "\n Book title : " << r1->label; + tchapters = r1->ch_count; + for (i = 0; i < tchapters; i++) + { + + cout << "\nChapter " << i + 1; + cout << " : " << r1->child[i]->label; + cout << "\nSections : "; + for (j = 0; j < r1->child[i]->ch_count; j++) + { + cout << "\n"<< r1->child[i]->child[j]->label; + } + } + } + cout << endl; +} + +int main() +{ + int choice; + GT gt; + while (1) + { + cout << "-----------------" << endl; + cout << "Book Tree Creation" << endl; + cout << "-----------------" << endl; + cout << "1.Create" << endl; + cout << "2.Display" << endl; + cout << "3.Quit" << endl; + cout << "Enter your choice : "; + cin >> choice; + switch (choice) + { + case 1: + gt.create_tree(); + case 2: + gt.display(root); + break; + case 3: + cout << "Thanks for using this program!!!"; + exit(1); + default: + cout << "Wrong choice!!!" << endl; + } + } + return 0; +} +// END OF CODE +// EXPERIMENTAL CODE \ No newline at end of file diff --git a/Codes/Practical-B7.cpp b/Codes/Practical-B7.cpp new file mode 100644 index 0000000..031a041 --- /dev/null +++ b/Codes/Practical-B7.cpp @@ -0,0 +1,146 @@ +// WARNING: THIS CODE HAS NOT BEEN TESTED. +// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE. +// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS, +// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY. + +/* +Problem Statement: Construct an expression tree from the given prefix expression eg. +--a*bc/def and +traverse it using postordertraversal(non recursive) and then delete the entire tree. + +Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/ +*/ + +// BEGINNING OF CODE +#include +#include +using namespace std; + +struct node +{ + char data; + node *left; + node *right; +}; +class tree +{ + char prefix[20]; + +public: + node *top; + void expression(char[]); + void display(node *); + void non_rec_postorder(node *); + void del(node *); +}; +class stack1 +{ + node *data[30]; + int top; + +public: + stack1() + { + top = -1; + } + int empty() + { + if (top == -1) + return 1; + return 0; + } + void push(node *p) + { + data[++top] = p; + } + node *pop() + { + return (data[top--]); + } +}; +void tree::expression(char prefix[]) +{ + char c; + stack1 s; + node *t1, *t2; + int len, i; + len = strlen(prefix); + for (i = len - 1; i >= 0; i--) + { + top = new node; + top->left = NULL; + top->right = NULL; + if (isalpha(prefix[i])) + { + top->data = prefix[i]; + s.push(top); + } + else if (prefix[i] == '+' || prefix[i] == '*' || prefix[i] == '-' || prefix[i] == '/') + { + t2 = s.pop(); + t1 = s.pop(); + top->data = prefix[i]; + top->left = t2; + top->right = t1; + s.push(top); + } + } + top = s.pop(); +} +void tree::display(node *root) +{ + if (root != NULL) + { + cout << root->data; + display(root->left); + display(root->right); + } +} +void tree::non_rec_postorder(node *top) +{ + stack1 s1, s2; /*stack s1 is being used for flag . A NULL data implies that the right subtree has not been visited */ + node *T = top; + cout << "\n"; + s1.push(T); + while (!s1.empty()) + { + T = s1.pop(); + s2.push(T); + if (T->left != NULL) + s1.push(T->left); + if (T->right != NULL) + s1.push(T->right); + } + while (!s2.empty()) + { + top = s2.pop(); + cout << top->data; + } +} +void tree::del(node *node) +{ + if (node == NULL) + return; + /* first delete both subtrees */ + del(node->left); + del(node->right); + /* then delete the node */ + cout <data<> expr; + cout << expr; + t.expression(expr); + //t.display(t.top); + //cout< +using namespace std; +#define MAX 10 +#define TRUE 1 +#define FALSE 0 + +// declaring an adjacency list for storing the graph +class lgra +{ +private: + struct node1 + { + int vertex; + struct node1 *next; + }; + node1 *head[MAX]; + int visited[MAX]; + +public: + //static int nodecount; + lgra(); + void create(); + void dfs(int); +}; + +//constructor +lgra::lgra() +{ + int v1; + for (v1 = 0; v1 < MAX; v1++) + visited[v1] = FALSE; + for (v1 = 0; v1 < MAX; v1++) + head[v1] = NULL; +} + +void lgra::create() +{ + int v1, v2; + char ans; + node1 *N, *first; + cout << "Enter the vertices no. beginning with 0 : "; + do + { + cout << "\nEnter the Edge of a graph : \n"; + cin >> v1 >> v2; + if (v1 >= MAX || v2 >= MAX) + cout << "Invalid Vertex Value!!\n"; + else + { + //creating link from v1 to v2 + N = new node1; + if (N == NULL) + cout << "Insufficient Memory!!\n"; + N->vertex = v2; + N->next = NULL; + first = head[v1]; + if (first == NULL) + head[v1] = N; + else + { + while (first->next != NULL) + first = first->next; + first->next = N; + } + //creating link from v2 to v1 + N = new node1; + if (N == NULL) + cout << "Insufficient Memory!!\n"; + N->vertex = v1; + N->next = NULL; + first = head[v2]; + if (first == NULL) + head[v2] = N; + else + { + while (first->next != NULL) + first = first->next; + first->next = N; + } + } + cout << "\n Want to add more edges?(y/n) : "; + cin >> ans; + } while (ans == 'y'); +} + +//dfs function +void lgra::dfs(int v1) +{ + node1 *first; + cout << endl + << v1; + visited[v1] = TRUE; + first = head[v1]; + while (first != NULL) + if (visited[first->vertex] == FALSE) + dfs(first->vertex); + else + first = first->next; +} + +int main() +{ + int v1; + lgra g; + + g.create(); + cout << endl << "Enter the vertex from where you want to traverse : "; + cin >> v1; + if (v1 >= MAX) + cout << "Invalid Vertex!!\n"; + else + { + cout << "The Dfs of the graph : "; + g.dfs(v1); + } + + return 0; +} +// END OF CODE +// EXPERIMENTAL CODE \ No newline at end of file diff --git a/Codes/Practical-C13 (Matrix).cpp b/Codes/Practical-C13 (Matrix).cpp new file mode 100644 index 0000000..2a25454 --- /dev/null +++ b/Codes/Practical-C13 (Matrix).cpp @@ -0,0 +1,93 @@ +// WARNING: THIS CODE HAS NOT BEEN TESTED. +// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE. +// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS, +// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY. + +/* +Problem Statement: Represent a given graph using ADJACENCY MATRIX to perform DFS and using adjacency list to perform BFS. Use the map of the area around the college as the graph. Identify the prominent land marks as nodes and perform DFS and BFS on that. + +Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/ +*/ + +// BEGINNING OF CODE +#include +#include +using namespace std; + +int cost[10][10], i, j, k, n, qu[10], front, rear, v, visit[10], visited[10]; +int stk[10], top, visit1[10], visited1[10]; + +int main() +{ + int m; + cout << "Enter number of vertices : "; + cin >> n; + cout << "Enter number of edges : "; + cin >> m; + + cout << "\nEDGES :\n"; + for (k = 1; k <= m; k++) + { + cin >> i >> j; + cost[i][j] = 1; + cost[j][i] = 1; + } + + //display function + cout << "The adjacency matrix of the graph is : " << endl; + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + cout << " " << cost[i][j]; + } + cout << endl; + } + + cout << "Enter initial vertex : "; + cin >> v; + cout << "The BFS of the Graph is\n"; + cout << v<> v; + cout << "The DFS of the Graph is\n"; + cout << v<= 1; j--) + if (cost[v][j] != 0 && visited1[j] != 1 && visit1[j] != 1) + { + visit1[j] = 1; + stk[top] = j; + top++; + } + v = stk[--top]; + cout << v << " "; + k++; + visit1[v] = 0; + visited1[v] = 1; + } + + return 0; +} +// END OF CODE +// EXPERIMENTAL CODE \ No newline at end of file diff --git a/Codes/Practical-C15.cpp b/Codes/Practical-C15.cpp new file mode 100644 index 0000000..70c08f1 --- /dev/null +++ b/Codes/Practical-C15.cpp @@ -0,0 +1,134 @@ +// WARNING: THIS CODE HAS NOT BEEN TESTED. +// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE. +// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS, +// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY. + +/* +Problem Statement: You have a business with several offices; you want to lease phone lines to connect them up with each other; and the phone company charges different amounts of money to connect different pairs of cities. You want a set of lines that connects all your offices with a minimum total cost. Solve the problem by suggesting appropriate data structures. + +Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/ +*/ + +// BEGINNING OF CODE +#include +using namespace std; + +class Office +{ + int n; + int a[10][10]; + string office[10]; + +public: + void input(); + void display(); + void Prims(); +}; + +void Office::input() +{ + cout<<"\nEnter no. of offices: "; + cin>>n; + cout<<"\nEnter the names of offices: "; + for(int i=0 ; i> office[i]; + + cout<<"\nEnter the cost to connect the offices: "; + for(int i=0 ; i> a[i][j]; + a[j][i] = a[i][j]; + } +} + +void Office::display() +{ + + for(int i=0 ; i "; + while(1) + { + minCost = 10000; + for(int i=0 ; i "; + cost = cost + minCost; + count++; + + if(count==n) + break; + } + + cout<<"\nMinimum cost: "<> choice; + switch(choice) + { + case 1: + o1.input(); + break; + case 2: + o1.display(); + break; + case 3: + o1.Prims(); + break; + case 4: + return 0; + default: + cout<<"\nInvalid choice.Try again!"; + } + if(choice != 5) + goto MENU; + return 0; +} +// END OF CODE +// EXPERIMENTAL CODE \ No newline at end of file diff --git a/Codes/Practical-D19.cpp b/Codes/Practical-D19.cpp new file mode 100644 index 0000000..2275300 --- /dev/null +++ b/Codes/Practical-D19.cpp @@ -0,0 +1,291 @@ +// WARNING: THIS CODE HAS NOT BEEN TESTED. +// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE. +// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS, +// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY. + +/* +Problem Statement: A Dictionary stores keywords & its meanings. Provide facility for adding new keywords, deleting keywords, updating values of any entry. Provide facility to display whole data sorted in ascending/ Descending order. Also find how many maximum comparisons may require for finding any keyword. Use Height balance tree and find the complexity for finding a keyword. + +Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/ +*/ + +// BEGINNING OF CODE +#include +#include +#include +#define MAX 50 +#define SIZE 20 +using namespace std; + +struct AVLnode +{ + public: + char cWord[SIZE],cMeaning[MAX]; + AVLnode *left,*right; + int iB_fac,iHt; +}; + +class AVLtree +{ + + public: + AVLnode *root; + AVLtree() + { + root=NULL; + } + int height(AVLnode*); + int bf(AVLnode*); + AVLnode* insert(AVLnode*,char[SIZE],char[MAX]); + AVLnode* rotate_left(AVLnode*); + AVLnode* rotate_right(AVLnode*); + AVLnode* LL(AVLnode*); + AVLnode* RR(AVLnode*); + AVLnode* LR(AVLnode*); + AVLnode* RL(AVLnode*); + AVLnode* delet(AVLnode*,char x[SIZE]); + void inorder(AVLnode*); +}; + +AVLnode *AVLtree::delet(AVLnode *curr,char x[SIZE]) +{ + AVLnode *temp; + if(curr==NULL) + return(0); + else + if(strcmp(x,curr->cWord)>0) + { + curr->right=delet(curr->right,x); + if(bf(curr)==2) + if(bf(curr->left)>=0) + curr=LL(curr); + else + curr=LR(curr); + } + else + if(strcmp(x,curr->cWord)<0) + { + curr->left=delet(curr->left,x); + if(bf(curr)==-2) + if(bf(curr->right)<=0) + curr=RR(curr); + else + curr=RL(curr); + } + else + { + if(curr->right!=NULL) + { + temp=curr->right; + while(temp->left!=NULL) + temp=temp->left; + strcpy(curr->cWord,temp->cWord); + curr->right=delet(curr->right,temp->cWord); + if(bf(curr)==2) + if(bf(curr->left)>=0) + curr=LL(curr); + else + curr=LR(curr); + } + else + return(curr->left); + } + curr->iHt=height(curr); + return(curr); +} + + +AVLnode* AVLtree :: insert(AVLnode*root,char newword[SIZE],char newmeaning[MAX]) +{ + if(root==NULL) + { + root=new AVLnode; + root->left=root->right=NULL; + strcpy(root->cWord,newword); + strcpy(root->cMeaning,newmeaning); + } + + else if(strcmp(root->cWord,newword)!=0) + { + if(strcmp(root->cWord,newword)>0) + { + root->left=insert(root->left,newword,newmeaning); + if(bf(root)==2) + { + if (strcmp(root->left->cWord,newword)>0) + root=LL(root); + else + root=LR(root); + } + } + + else if(strcmp(root->cWord,newword)<0) + { + root->right=insert(root->right,newword,newmeaning); + if(bf(root)==-2) + { + if(strcmp(root->right->cWord,newword)>0) + root=RR(root); + else + root=RL(root); + } + } + } + else + cout<<"\nRedundant AVLnode"; + root->iHt=height(root); + return root; +} + +int AVLtree :: height(AVLnode* curr) +{ + int lh,rh; + if(curr==NULL) + return 0; + if(curr->right==NULL && curr->left==NULL) + return 0; + else + { + lh=lh+height(curr->left); + rh=rh+height(curr->right); + if(lh>rh) + return lh+1; + return rh+1; + } +} + +int AVLtree :: bf(AVLnode* curr) +{ + int lh,rh; + if(curr==NULL) + return 0; + else + { + if(curr->left==NULL) + lh=0; + else + lh=1+curr->left->iHt; + if(curr->right==NULL) + rh=0; + else + rh=1+curr->right->iHt; + return(lh-rh); + } +} + +AVLnode* AVLtree :: rotate_right(AVLnode* curr) +{ + AVLnode* temp; + temp=curr->left; + curr->left=temp->right; + temp->left=curr; + curr->iHt=height(curr); + temp->iHt=height(temp); + return temp; +} + +AVLnode* AVLtree :: rotate_left(AVLnode* curr) +{ + AVLnode* temp; + temp=curr->right; + curr->right=temp->left; + temp->left=curr; + curr->iHt=height(curr); + temp->iHt=height(temp); + return temp; +} + +AVLnode* AVLtree :: RR(AVLnode* curr) +{ + curr=rotate_left(curr); + return curr; +} + +AVLnode* AVLtree :: LL(AVLnode* curr) +{ + curr=rotate_right(curr); + return curr; +} + +AVLnode* AVLtree :: RL(AVLnode* curr) +{ + curr->right=rotate_right(curr->right); + curr=rotate_left(curr); + return curr; +} + +AVLnode* AVLtree::LR(AVLnode* curr) +{ + curr->left=rotate_left(curr->left); + curr=rotate_right(curr); + return curr; +} + +void AVLtree :: inorder(AVLnode* curr) +{ + if(curr!=NULL) + { + inorder(curr->left); + cout<<"\n\t"<cWord<<"\t"<cMeaning; + inorder(curr->right); + } +} + +int main() +{ + int iCh; + AVLtree a; + AVLnode *curr=NULL; + char cWd[SIZE],cMean[MAX]; + cout<<"\n--------------------------------------"; + cout<<"\n\tAVL TREE IMPLEMENTATION"; + cout<<"\n--------------------------------------"; + do + { cout<<"\n--------------------------------"; + cout<<"\n\t\tMENU"; + cout<<"\n--------------------------------"; + cout<<"\n1.Insert\n2.Inorder\n3.Delete\n4.Exit"; + cout<<"\n--------------------------------"; + cout<<"\nEnter your choice :"; + cin>>iCh; + + switch(iCh) + { + case 1: cout<<"\nEnter Word : "; + cin>>cWd; + /*for(int i;cMean[i]!='\0';i++) + { + if(cMean[i]>='A'&& cMean[i]<='Z') + { + cMean[i]=cMean[i]+32; + } + } + cout<>cWd; + curr=a.delet(a.root,cWd); + if(curr==NULL) + cout<<"\nWord not present!"; + else + cout<<"\nWord deleted Successfully!"; + curr=NULL; + break; + + case 4: exit(0); + } + }while(iCh!=4); + + return 0; +} +// END OF CODE +// EXPERIMENTAL CODE \ No newline at end of file diff --git a/Codes/Practical-F23.cpp b/Codes/Practical-F23.cpp new file mode 100644 index 0000000..5253d6f --- /dev/null +++ b/Codes/Practical-F23.cpp @@ -0,0 +1,163 @@ +// WARNING: THIS CODE HAS NOT BEEN TESTED. +// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE. +// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS, +// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY. + +/* +Problem Statement: Department maintains a student information. The file contains roll number, name, division and address. Allow user to add, delete information of student. Display information of particular employee. If record of student does not exist an appropriate message is displayed. If it is, then the system displays the student details. Use sequential file to main the data. + +Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/ +*/ + +// BEGINNING OF CODE +#include +#include +using namespace std; + +class student +{ + public: + char name[10]; + int roll; + void getdata() + { + cout<<"\n Enter the roll no and name:"; + cin>>roll>>name; + } + + void putdata() + { + cout<<"\n The roll no and name:"; + cout<>ans; + }while(ans=='Y'||ans=='y'); + fp.close(); + } + void append() + { + char ans; + student s; + fp.open("stu.dat",ios::app); + do + { + s.getdata(); + fp.write((char *)&s,sizeof(s)); + cout<<"\n More?"; + cin>>ans; + }while(ans=='Y'||ans=='y'); + fp.close(); + } + void display() + { + student s; + fp.open("stu.dat",ios::in); + while(!fp.eof()) + { + fp.read((char *)&s,sizeof(s)); + if (!fp.eof()) + s.putdata(); + } + fp.close(); + } + void search() + { + student s; int flag=0; + cout <<"\n Enter roll to be searched:"; + int r; cin >> r; + fp.open("stu.dat",ios::in); + while(!fp.eof()) + { + fp.read((char *)&s,sizeof(s)); + if (!fp.eof() && s.roll==r) + { flag=1; s.putdata(); break; } + } + if (flag==0) cout << "\n Not found"; + fp.close(); + } + void update() + { + student s; int flag=0; + cout <<"\n Enter roll to be updated:"; + int r; cin >> r; + fp.open("stu.dat",ios::in|ios::out); + while(!fp.eof()) + { + fp.read((char *)&s,sizeof(s)); + if (!fp.eof() && s.roll==r) + { + flag=1; + cout <<"\n Enter new data\n"; + s.getdata(); + fp.seekp(-1*sizeof(s),ios::cur); + fp.write((char *)&s,sizeof(s)); + break; + } + } + + if (flag==0) cout << "\n Not found"; + fp.close(); + } + void delete1() + { + student s; int flag=0; + fstream fp1; + cout <<"\n Enter roll to be deleted:"; + int r; cin >> r; + fp.open("stu.dat",ios::in); + fp1.open("temp.dat",ios::out); + while(fp.read((char *)&s,sizeof(s))) + { + + if ( s.roll!=r) + { + flag=1; + fp1.write((char *)&s,sizeof(s)); + } + } + if (flag==0) cout << "\n Not found"; + fp.close(); + fp1.close(); + remove("stu.dat"); + rename("temp.dat","stu.dat"); + } +}; + + +int main() +{ + file f; int choice; + do{ + cout << "\n1 create \n 2 display \n 3 Search \n 4 Append"; + cout << " \n 6 delete \n 7 Update"; + cout << "\n Enter choice:"; cin >> choice; + switch(choice) + { + case 1:f.create(); break; + case 2:f.display(); break; + case 3:f.search(); break; + case 4:f.append(); break; + case 6:f.delete1();break; + case 7:f.update();break; + } + }while(choice< 8); +} +// END OF CODE +// EXPERIMENTAL CODE \ No newline at end of file