
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

























CW_Unit19-Data Structures and Algorithms
Introduction................................................................................................................................................. 2 LO1 : Implementation of Data Structure...................................................................................................... 3 Organization of Data Structure............................................................................................................ 4 Comparison of Sorting Techniques:................................................................................................... 10 Bubble Sort:....................................................................................................................................... 12 LO2: Stack and OOPS Concept in Data Structure....................................................................................... 12 OOPS In Data Structures With Encapsulation And Information Hiding :............................................ 15 LO3: Employee Management System........................................................................................................ 20 LO4 : Abstract Data Type And Asymptotic Analysis................................................................................... 27 Asymptotic Analysis :............................................................................................................................. 28 Conclusion................................................................................................................................................. 30 References................................................................................................................................................. 32 Introduction The term data structure is a system of cataloguing data and algorithm which is defined and how to use it in a program. Without data structure and algorithm the sequence of information which are processed in a program like a series of instruction are hampered and data values are invariable instructions which effect the output if a program. It is a set of instruction given to the
Trees and graphs are used in non linear type. It is a hierarchical presentation of connection of data between an particular data and an component. Data structures are split up into two type: Primitive Data Structure Non Primitive Data Structures Primitive Data Structure: They have different representation of integers, floating point numbers, characters, constants and pointers. Non Primitive Data Structure: They sort out group of various data items like arrays, lists and files. Figure 1 : Data Structure Flowchart (Source : Data Structures Using C, L. V. Narasimha Prasad, 2016) Organization of Data Structure The collection of any sort of data which are need to work a program to run, can be divided into contiguous and non contiguous data types. Figure 2 : Organization of Data Structure (Source : Data Structures Using C, E. Krishna Rao Patro, 2016)
Contiguous Data Structure It can be broken down into Arrays and Structures, the first one is array, each data component is of same type and has same size and secondly, structures, which consist of data like person’s name and age with different data types and data sizes. For example of an Array: Int arr [3] = {1,2,3]; 1 2 3 For example of an Structures : struct cust_data { int age ; char name[20]; }; Cust_data bill = {21,”bill the student”}; 21 “bill the student” Non Contiguous Structures It is a group of data items, called nodes, where each node directs to the other nodes and also can point one or more node in the whole structure. Figure 3 : Different Data Structure (Source : Learner)
q = q next; } if (q) { /* not at end of list, so detach P by making previous and next nodes point to each other */ node *p = q -> prev; node *n = q -> next; if (p) p -> next = n; if (n) n -> prev = P; } return; } Queue : It is a special type of where an item is inserted in one end and deleted in other end. Another name is “FIFO” or “FIRST IN FIRST OUT” list. The operation involves in a queue is analogue, it basically inserting an item to enter at the last allocated space of the list, rather than at the starting point. The function includes are : enqueue : which inserts an element at the end of the queue. dequeue : which deletes an element at the start of the queue. Source code for the queue operation using array functions, the different queue function is to perform creation, deletion and display of the component : insertQ(): entering an component at the end of queue Q. deleteQ(): removing the first component of Q. displayQ(): showcasing the component in the queue. Code of Queue as follows :
int Q[MAX]; int front, rear;
void insertQ() { int data; if(rear == MAX) { printf("\n Linear Queue is full"); return; } else { printf("\n Enter data: "); scanf("%d", &data); Q[rear] = data; rear++; printf("\n Data Inserted in the Queue "); } } void deleteQ() { if(rear == front) { printf("\n\n Queue is Empty.."); return; } else { printf("\n Deleted element from Queue is %d", Q[front]); front++; } } void displayQ()
int ch; do { ch = menu(); switch(ch) { case 1: insertQ(); break; case 2: deleteQ(); break; case 3: displayQ(); break; case 4: return; } getch(); } while(1); } Sorting : It is an efficient way of organizing a data element in a systematically order. For example, a dictionary where data are segregated in a organized manner. There are many type of sorting techniques like, Bubble sort, Quick Sort, Selection Sort and Heap Sort and also there are various types of sorting techniques, Internal and External sorting. Comparison of Sorting Techniques: Quick Sort and Bubble Sort are two main used for sorting of an element. Quick Sort:
It is largely and widely used sorting algorithm. It is an example of a “Divide and Conquer” technique. Algorithm of Quick Sort: Sorting an element a[p],……,a[q] which is present in universal array a[n] into an increasing order. The a[n+1] is defined and must be in data order a[n]:a[n+1] = +∞ quicksort (p, q) { if ( p < q ) then { call j = PARTITION(a, p, q+1); // j is the position of the partitioning element call quicksort(p, j – 1); call quicksort(j + 1 , q); } } partition (a, m, p) { v = a[m]; up = m; down = p; // a[m] is the partition element do { repeat up=up+1; until (a[up] > v); repeat down = down – 1; until (a[down] < v); if (up < down) then call interchange(a, up, down); } while (up > down); a[m] = a[down]; a[down] = v; return (down); }
The two introductory operations are: Push : It is used to enter an element data into the stack Pop : It is used to removes an element data outside of the stack Source Code for Stack Operation : #include int MAXSIZE = 8; int stack[8]; int top = -1; int isempty() { if(top == -1) return 1; else return 0; } int isfull() { if(top == MAXSIZE) return 1; else return 0; } int peek() { return stack[top]; } int pop() { int data; if(!isempty()) { data = stack[top]; top = top - 1; return data; } else { printf("Could not retrieve data, Stack is empty.\n");
int push(int data) { if(!isfull()) { top = top + 1; stack[top] = data; } else { printf("Could not insert data, Stack is full.\n"); } } int main() { // push items on to the stack push(3); push(5); push(9); push(1); push(12); push(15); printf("Element at top of the stack: %d\n" ,peek()); printf("Elements: \n"); // print stack data while(!isempty()) { int data = pop(); printf("%d\n",data); } printf("Stack full: %s\n" , isfull()?"true":"false"); printf("Stack empty: %s\n" , isempty()?"true":"false"); return 0; } Output : Element at top of the stack: 15
Solution : We can utilize two type of stack, one is to stock the original data element and another as an auxiliary stack to store the lesser value. Code : #include #include #include using namespace std; /* A special stack having peek() pop() and
// Encode the current value with // demoVal, combine with min and // insert into stack st.push(val * demoVal + min); cout << "pushed: " << val << endl; } int pop() { // if stack is empty return -1; if ( st.empty() ) { cout << "stack underflow" << endl ; return -1; } int val = st.top(); st.pop(); // If stack is empty, there would // be no min value present, so // make min as - if (!st.empty()) min = st.top() % demoVal; else min = -1; cout << "popped: " << val / demoVal << endl; // Decode actual value from // encoded value return val / demoVal; } int peek() { // Decode actual value // from encoded value return st.top() / demoVal;
LO3: Employee Management System A interface or console bases on employee data where we can add employee details like ID, department and phone number and alter the details with also to delete the selected data. After all that, the display of the whole data table content in a sequential order. Code #define _CRT_SECURE_NO_WARNINGS #include #define SIZE 4 structEmp { int id; stringnam; intdept; doublepno; }; int main(void) { inti, option = 0, count = 0, new = 0, srch, don = 0, nam = 0; structEmpemp[SIZE] = { { 0 } }; printf("---=== EMPLOYEE DATA MANAGEMENT SYSTEM===---\n\n"); do { printf("1. Display Employee Information\n"); printf("2. Add Employee\n"); printf("3. Update Employee Details\n"); printf("4. Remove Employee\n"); printf("0. Exit\n\n"); printf("Please select from the above options: "); scanf("%d", &option); printf("\n"); switch (option) { case 0: break; case 1: