Data Structures Past Paper PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document provides information on data structures, including linear and non-linear data structures and examples. It covers various operations on arrays such as traversal, insertion, deletion, search, and update. It also features code examples in C++ for updating, adding, and deleting elements from an array. A brief explanation of sparse matrices is also given, and example matrix multiplications are presented in code form.
Full Transcript
## علوم الحاسب ## الفرقة الثانية ### Session 1 ### هياكل البيانات ### Data Structures Computer Science Department ## Q.1. What do we mean by concept data structures? A data structure is a specific way to store and organize data in a computer's memory. Data may be arranged in many ways such as the...
## علوم الحاسب ## الفرقة الثانية ### Session 1 ### هياكل البيانات ### Data Structures Computer Science Department ## Q.1. What do we mean by concept data structures? A data structure is a specific way to store and organize data in a computer's memory. Data may be arranged in many ways such as the logical or mathematical model. ### Categories of Data Structure: - Linear Data Structure. - Non-linear Data Structure. ## Q.2. Compare between linear and non-linear data structures. | Linear | Non-Linear | | ------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | A data structure is said to be linear if its elements combine to form any specific order. | Data that contains a hierarchical relationship among elements. | | Arrays (linear memory location) | The data structure that reflects this relationship is termed as rooted tree graph or a tree | | Lists (pointers). | E.g., Graphs, family of trees, and table of contents. | | E.g., Arrays, Queues, Stacks, and Linked lists. | | ## Q.3. What is array? Each item stored in an array is called an element. Each location of an element in an array has a numerical index starts with 0 which is used to identify the element. ## Array basic operations: - Traverse - print all the array elements one by one. - Insertion - Adds an element at the given index. - Deletion - Deletes an element at the given index. - Search - Searches an element using index or by the value. - Update - Updates an element at the given index. ## Q.4. Write a program used for updating an element in array. ```c++ #include <iostream> using namespace std; int main() { int LA[] = { 1,3,5,7,8 }; int k = 3, n = 5, item = 10; int i, j; //Updating element at position 3 (index =2) cout << "The original array elements are :\n"; for (i = 0; i < n;i++) { cout << "LA[" << i << "]=" << LA[i] << "\n"; } LA[k - 1] = item; cout << "The array elements after update : \n"; for (i = 0; i < n;i++) { cout << "LA[" << i << "]=" << LA[i] << "\n"; } getchar(); } ``` ## Q.5. Write a program used for adding an element at a specific index of an array. ```c++ #include <iostream> #include <conio.h> using namespace std; int main() { int LA[] = { 1, 3, 5, 7, 8 }; int item = 10, k = 3, n = 5; int i = 0, j = n-1; //Adding an element at position 4 (index =3) cout << "The original array elements are"<<"\n"; for (i = 0; i<n; i++) { cout << "\nLA[" << i << "] =" << LA[i]; } n = n + 1; while (j >= k) { LA[j + 1] = LA[j]; j = j - 1; } LA[k] = item; cout << "\nThe array elements after insertion:\n"; for (i = 0; i<n; i++) { cout << "\nLA[" << i << "] = " << LA[i]; } _getch(); return(0); } ``` ## Q.6. Write a program used for deleting an element at a specific index of an array. ```c++ #include <iostream> #include <conio.h> using namespace std; int main() { int LA[] = {1,3,5,7,8); int i, k=3, j = k, n = 5; //Deleting an element at position 3 (index =2) cout<<"The original array elements are : \n"; for(i = 0; i< n; i++) { cout << "LA[" << i << "] =" << LA[i] << "\n"; } while (j< n) { LA[j-1] = LA[j]; j = j + 1; } n = n -1; cout<<"The array elements after deletion : \n"; for(i = 0; i< n; i++) { cout << "LA["<<i <<"]= "<<LA[i]<<"\n"; } _getch(); } ``` ## Q.7. Write a program used for searching an element in array. ```c++ #include <iostream> using namespace std; int main() { int LA[] = { 1, 3, 5, 7, 8 }; int item = 5, n = 5; int j = 0; //Searching for element=5 cout << "The original array elements are : \n"; for (int i = 0; i< n; i++) { cout << "LA[" << i << "]=" << LA[i] << "\n"; } while (j<n) { if (LA[j] == item) { cout << "Found_element " << item << " At position " << j + 1 <<endl; break; } j = j + 1; } getchar(); } ``` ## Q.8. What is sparse matrix, give an example. A 2-D array, most of the elements of the matrix have value. | Row | 0 | 0 | 1 | 1 | 3 | 3 | | :---- | :-- | :-- | :-- | :-- | :-- | :-- | | Column | 2 | 4 | 2 | 3 | 1 | 2 | | Value | 3 | 4 | 5 | 7 | 2 | 6 | ## Q.9. Write necessary code to store the following sparse matrix, save storage and computing time? ```c++ #include <iostream> using namespace std; int main() { int size = 0; int sparseMatrix[4][5] = { { 0, 0, 3, 0, 4 }, { 0, 0, 5, 7, 0 }, { 0, 0, 0, 0, 0 }, {0,2,6,0,0} }; for (int i = 0; i < 4; i++) for (int j = 0; j < 5; j++) if (sparseMatrix[i][j] != 0) size++; int compactMatrix[3][size]; // size not work with c++ int k = 0; for (int i = 0; i < 4; i++) for (int j = 0; j < 5; j++) { if (sparseMatrix[i][j] != 0) { compactMatrix[0][k] = i; compactMatrix[1][k] = j; compactMatrix[2][k] = sparseMatrix[i][j]; k++; } } } for (int i = 0; i<3; i++) { for (int j = 0; j<size; j++) cout << compactMatrix[i][j]; cout << "\n"; } getchar(); return(0); ``` ## Q.10. Write the necessary code for matrix multiplications? ```c++ if(column1==row2) { int mul[row1] [column2]; for(int i=0;i<row1;i++) { for(int j=0;j<column2;j++) { mul[i][j]=0; for(int k=0;k<column1;k++) { mul[i][j]+=matx1[i][k]*matx2[k][j]; } } } } ```