Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
DATA STRUCTURE LAB (ETCS 257) Faculty Name: Deepak Gupta Student Name: Vikash Kr.Das Roll No.: 05396402812 Semester: 3rd Batch : E-7 Department of Electronics & Communication Engineering (ECE) Maharaja Agrasen Institute of technology Maharaja Agrasen Chowk, Sector – 22, Rohini, New Delhi – 110086 VKD DATA STRUCTURE PRACTICAL RECORD PAPER CODE : ETCS257 Name of the student : VIKASH KUMAR DAS University Roll No. : 05396402812 Branch : ECE Section/ Group : E-7 PRACTICAL DETAILS Exp. no Experiment Name Date of performance Date of checking Remarks Marks (10) VKD EXPERIMENT NO. 01 AIM: To implement traversal, insertion, deletion in a linear array. ALGORITHM: TRAVERSAL: This algorithm traverses a linear array A with lower bound 0 and upper bound 9. It traverses A searching the desired number NUMB. Step 1: Repeat for I=0 to 9 Apply search operation for A[I]==NUMB. End of loop. Step 2: Exit. INSERTION: In this A is a linear array with 10 elements and NUMB is any number and POS is the potion at which the NUMB is to be inserted. Step 1: Set I=10. [Initialize counter.] Step 2: Repeat steps 3 and 4 while I>=POS. Step 3: Set A[I+1]=A[I]. [Move Jth element downwards.] Step 4: Set I=I-1. [Decrease counter.] Step 5: Set A[POS]=NUMB. [Insert element.] Step 6: Set 10 to 11. [Reset number of elements.] Step 7: Exit. DELETION: In this A is a linear array with 10 elements and NUMB is any number and this deletes the NUMB number from the array A. Step 1: Repeat for I=0 to 9 Apply search operation for A[I]==NUMB. End of loop. Step 2: Repeat for I=POS to 8 Set A[I]=A[I+1] [Move Ith element upwards.] End of loop Step 3: Set 9 to 8. [Reset number of elements.] Step 4: Exit. VKD PROGRAM CODE: #include<iostream.h> #include<conio.h> void main () { clrscr(); int a[20],numb,pos,x,flag=0; cout<<"Enter 10 numbers: "<<endl; for(int i=0;i<10;i++) {cin>>a[i];} cout<<"1-TRAVERSE"<<endl; cout<<"2-INSERION"<<endl; cout<<"3-DELETION"<<endl; cin>>x; if(x==1) {cout<<"Enter number to be searched: "<<endl; cin>>numb; for(i=0;i<=10;i++) {if(a[i]==numb) {pos=i; cout<<"The position of th enumber is: "<<pos+1; flag++: } } if(flag==0) {cout<<"Number not in the series.";} } else if (x==2) {cout<<"Enter the number to insert: "<<endl; cin>>numb; cout<<"Enter the position at which you want to insert: "<<endl; cin>>pos; for(int i=10;i>pos-1;i--) {a[i]=a[i-1];} a[pos-1]=numb; cout<<"The new Array is: "<<endl; for(int j=0;j<11;j++) VKD {cout<<a[j]<<endl;} } else if (x==3) {cout<<"Enter the number to delete: "<<endl; cin>>numb; for(int i=0;i<10;i++) {if(a[i]==numb) {for(int j=i;j<10;j++) {a[j]=a[j+1];} } } cout<<"the new array is: "<<endl; for(int j=0;j<9;j++) {cout<<a[j]<<endl;} } getch(); } OUTPUT: VKD VKD EXPERIMENT NO. 02 AIM: To implement Stacks using arrays. ALGORITHM: PUSH OPERATION: Step 1: {Check for stack overflow} If TOS>=Size-1 Output ”Stack Overflow” Step 2: {Increment the counter value by one} TOS=TOS+1 Step 3: {Perform Insertion} a[TOS]=value Step 4: Exit. POP OPERATION: Step 1: {Check whether the stack is empty} If TOS=0 Output “Stack Underflow” Step 2: {Pop the TOS value} value=a[TOS} TOS=TOS-1 Step 3: {Print the top most value of the stack} Return (value) Step 4: Exit. VKD PROGRAM CODE: #include<iostream.h> #include<conio.h> #include<process.h> void main() { clrscr(); int TOS=0,i,a[5],value,ch; cout<<endl<<"Enter the operation you ant to perform"; cout<<endl<<"1:Push"<<endl<<"2:Pop"<<endl<<"3:Exit"<<endl; cin>>ch; do {if(ch==1) {if(TOS>4) {cout<<"Stack overflow";} else { cout<<endl<<"Enter the element you want to insert: "; cin>>value; TOS=TOS+1; a[TOS]=value; } cout<<endl<<"Enter "<<endl<<"1:Push"<<endl<<"2:Pop"<<endl<<"3:Exit"<<endl; cin>>ch; } if(ch==2) {cout<<endl<<"The top most element is: "<<endl; if(TOS==-1) {cout<<"Stack Underflow"<<endl;} else {value= a[TOS]; TOS=TOS-1; cout<<value<<endl; } cout<<endl<<"Enter "<<endl<<"1:Push"<<endl<<"2:Pop"<<endl<<"3:Exit"<<endl; cin>>ch; } VKD if(ch==3) {exit(0);} }while(ch==1 || ch==2); } OUTPUT: VKD EXPERIMENT NO. 03 AIM: To implement Linear and Circular Queue using arrays. ALGORITHM: Linear Queue: Inserting an element to linear Queue Step 1: If FRONT =1 and REAR=N or if FRONT=REAR+1 then : Print : Overflow and return Step 2: IF FRONT = NULL then Set FRONT =1 and REAR =1 Else If REAR =N then Set REAR = 1 Else Set REAR =REAR +1 Step 3: Set QUEUE[REAR]=ITEM Step 4: RETURN Deleting an element from a linear queue Step 1: If FRONT =NULL then : Print : Underflowflow and return Step 2: Set ITEM = QUEUE[FRONT] Step 3: IF FRONT = REAR then Set FRONT =0 and REAR =0 Else If FRONT =N then Set FRONT = 1 Else Set FRONT =FRONT +1 Step 4: RETURN VKD PROGRAM CODE: Linear Queue: #include<iostream.h> #include<conio.h> #include<process.h> #define MAX 50 int queue[50],rear=-1,front=-1,item; void insert() {if(rear==MAX-1) {cout<<"Queue is full.";} else {cout<<endl<<"Enter item: "; cin>>item; if(rear==-1 && front==-1) {rear=0; front=0; } else {rear++;} queue[rear]=item; cout<<"Item inserted: "<<item<<endl<<endl; } } void del() {if(front==-1) {cout<<endl<<"Queue is empty.";} else {item=queue[front]; if(front==rear) {front=-1; rear=-1; } else {front++;} cout<<"Item deleted: "<<item<<endl<<endl; } } VKD void display() {int i; if(front==-1) {cout<<endl<<"Queue is empty.";} else {cout<<endl; for(i=front;i<=rear;i++) {cout<<" "<<queue[i];} } cout<<endl; } void main() {clrscr(); int ch; do {cout<<endl<<"1=Insert"<<endl<<"2=Delete"<<endl<<"3=Display"<<e ndl<<"4=Exit"<<endl<<"Choice: "; cin>>ch; switch(ch) {case 1:insert(); break; case 2:del(); break; case 3:display(); break; case 4:exit(0); default:cout<<endl<<"Invalid entry."<<endl; } }while(1); } VKD OUTPUT: Linear Queue: VKD EXPERIMNET NO. 04 AIM: To implement Linear and Circular Queue using arrays. ALGORITHM: Circular Queue: Inserting an element to circular Queue Step 1: Initialize FRONT = – 1; REAR = 1 Step 2: REAR = (REAR + 1) % SIZE Step 3: If (FRONT is equal to REAR) (a) Display “Queue is full” (b) Exit Step 4: Else (a) Input the value to be inserted and assign to variable “DATA” Step 5: If (FRONT is equal to – 1) (a) FRONT = 0 (b) REAR = 0 Step 6: Q[REAR] = DATA Step 7: Repeat steps 2 to 5 if we want to insert more elements Step 8: Exit Deleting an element from a circular queue Step 1: If (FRONT is equal to – 1) (a) Display “Queue is empty” (b) Exit Step 2: Else (a) DATA = Q[FRONT] Step 3: If (REAR is equal to FRONT) (a) FRONT = –1 (b) REAR = –1 Step 4: Else (a) FRONT = (FRONT +1) % SIZE Step 5: Repeat the steps 1, 2 and 3 if we want to delete more elements Step 6: Exit VKD PROGRAM CODE: Circular Queue: #include<iostream.h> #include<conio.h> #include<process.h> #define SIZE 5 int queue[SIZE],rear=-1,front=-1,item; void insert() {if((front==0 && rear==SIZE-1) || (front==rear+1)) {cout<<endl<<"Queue is full.";} else {cout<<endl<<"Enter item: "; cin>>item; if(rear==-1) {rear=0; front=0; } else if(rear==SIZE-1) {rear=0;} else {rear++;} queue[rear]=item; cout<<"Item Inserted: "<<item<<endl<<endl; } } void del() {if(front==-1) {cout<<endl<<"Queue is empty.";} else {item=queue[front]; if(front==rear) {front=-1; rear=-1; } else if(front==SIZE-1) {front=0;} else VKD {front++;} cout<<endl<<"Item Deleted: "<<item<<endl<<endl; } } void display() {int i; if((front==-1) || (front==rear+1)) {cout<<"Queue is empty.";} else {cout<<endl; for(i=front;i<=rear;i++) {cout<<" "<<queue[i];} } cout<<endl; } void main() {clrscr(); int ch; do {cout<<endl<<"1=Insert"<<endl<<"2=Delete"<<endl<<"3=Display"<<e ndl<<"4=Exit"<<endl<<"Choice: "; cin>>ch; switch(ch) {case 1:insert(); break; case 2:del(); break; case 3:display(); break; case 4:exit(0); default:cout<<endl<<"Invalid entry."<<endl; } }while(1); } VKD OUTPUT: Circular Queue: VKD EXPERIMENT NO. 05 AIM: To perform linear or binary search as per the users choice. ALGORITHM: LINEAR SEARCH: Step 1: For each item in the list Check if value matches any item in the array. Step 2: If it matches. Return its index. Step 3: If it does not match. Continue searching until the end of the array. Step 4: Exit. BINARY SEARCH: While (beg<= end) Step 1: mid=(beg+end)/2 Step 2: if x==a[mid] Return mid Step 3: else if x>a[mid] beg=mid+1 Step 4: else end=mid-1 Step 5: Return x not found in the array. Step 6: Exit. VKD PROGRAM CODE: #include<iostream.h> #include<conio.h> class Search { public: int a[10],x,y,flag,i,j,temp; void getdata(); void linear(int x); void sort(int a[]); void binary(int x); }; void Search::getdata() {cout<<"Enter 10 numbers: "<<endl; for(i=0;i<10;i++) {cin>>a[i];} cout<<endl<<"Enter the number to be searched: "<<endl; cin>>x; cout<<endl<<"Select any one:"<<endl<<"1-Linear Search"<<endl<<"2Binary Search"<<endl; cin>>y; if(y==1) {linear(x);} else if(y==2) {sort(a); binary(x);} else {cout<<"Invalid choice.";} } void Search::linear(int x) {flag=0; for(int i=0;i<10;i++) {if(a[i]==x) {cout<<"The element exists at position: "<<i+1; flag++; break; } VKD } if(flag==0) {cout<<"The element doesnt exist.";} } void Search::sort(int a[]) {for(i=0;i<10;i++) {for(j=0;j<10;j++) {if(a[i]<a[j]) {temp=a[i]; a[i]=a[j]; a[j]=temp; } } } cout<<endl<<"The sorted array is: "<<endl; for(i=0;i<10;i++) {cout<<a[i]<<endl;} } void Search::binary(int x) {flag=0; int beg=0,last=10,mid; while(beg<=last) {mid=(beg+last)/2; if(x==a[mid]) {flag++; cout<<"The element exists at position: "<<mid+1; break;} else if (x>a[mid]) {beg=mid;} else {last=mid-1;} } if(flag==0) {cout<<"The element doesnt exist.";} } void main() { clrscr(); Search s; s.getdata(); getch(); VKD OUTPUT: VKD EXPERIMENT NO. 06 AIM: To perform various operations on a singly linked list. ALGORITHM: INSERTION OF A NODE: A) AT THE BEGINNING OF A LINKED LIST Step 1: {Check for free space} If new = NULL output “OVERFLOW” and exit. Step 2: {Allocate free space} New=new Link Step 3: {Read value of information part of a new node} Info[New]= Value Step 4: {Link address part of the currently created node with the address of start} Next[Next]=Start Step 5: {Now assign address of newly created node to the start} Start=New Step 6: Exit. B) AT THE END OF A LINKED LIST Step 1: {Check for free space} If new = NULL output “OVERFLOW” and exit. Step 2: {Allocate free space} New=new Link Step 3: {Read value of information part of a new node} Info[New]= Value Step 4: {Move the pointer to the end of the list} Repeat while Node<>NULL Node=Next[Node] Previous=Next[Previous] Step 5: {Link currently created node with the last node of the list} Next[New]=Node Next[Previous]=New Step 6: Exit. C) AT A DESIRED PLACE,WHEN NODE NUMBER IS KNOWN Step 1: {Check for free space} If new = NULL output “OVERFLOW” and exit. Step 2: {Initializaton} Node_number=0 Node=Start.Next[Points to first node of the list] Previous=Address of Start[Assign address of start to previous] Step 3: {Read node number that we want to insert} Input Insert_node Step 4: {Perform insertion operation} Repeat through Step 5 while NODE<>NULL Step 5: {Check if Insert_node is equal to the Node_number} If Node_number+1=Insert_node Next[New]=Node VKD Previous->Next=New Info[New]=Value Return Else {Move the pointer forward} Node=Next[Node] Previous=Next[Previous] Node_number=Node_number+1 Step 6: Exit. DELETION OF A NODE: A) FROM THE BEGINNING OF A LINKED LIST Step 1: {Initialization} Node=start.Next{Points to the first node in the list} Previous=assign address of start. Step 2: {Perform deletion operation } If node=NULL Output “UNDERFLOW” and exit. Else {Delete first node} Next[Previous]=Next[Node] {Move pointer to next node in the list} Free the space associated with Node Step 3: Exit. B) FROM THE END OF A LINKED LIST Step 1: {Initialization} Node=start.Next {Points to the first node in the list} Previous=assign address of start Node_number=0 Step 2: {Check list is empty or not} If node=NULL Output “UNDERFLOW” and exit. Step 3: {Scan the list to count the number of node in the list} Repeat while node<>NULL Node=Next[Node] Previous=next[Previous] Node_number=Node_number+1 Step 4: {Initialization once again} Node=start.Next Previous=Assign address of start Step 5: {Scan list for 1 less to size of the list} Repeat while Node_Number<>1 Node=next [Node] Previous=next [Node] Node_number=Node_number-1 Step 6: {Check if last node is arising} If node_number=1 Next[Previous]=Next[Node] Free(node) Step7: Exit. VKD PROGRAM CODE: #include<iostream.h> #include<conio.h> #include<process.h> struct node { int info; node *next, *prev; }; node *temp,*prev,*start=NULL,*ptr; void main() { clrscr(); int ch,item,i,loc; char wish; do {cout<<"\n MENU"; cout<<"\n 1.Insertion Of Node At Beginning"; cout<<"\n 2.Insertion Of Node At Specific Position"; cout<<"\n 3.Deletion Of Node From Beginning"; cout<<"\n 4.Traversal"; cout<<"\n 5.Exit"; cout<<"\n\n Enter Your Choice : "; cin>>ch; switch(ch) {case 1: ptr=new node; if(ptr==NULL) {cout<<"\n OVERFLOW";} else {cout<<"\n Enter the element to be inserted : "; cin>>item; ptr->info=item; if(start==NULL) {ptr->prev=NULL; ptr->next=NULL; start=ptr; } else {ptr->prev=NULL; ptr->next=start; start=ptr; } } break; VKD case 2: ptr=new node; if(ptr==NULL) {cout<<"\n OVERFLOW";} else {cout<<"\n Enter the element to be inserted : "; cin>>item; ptr->info=item; if(start==NULL) {ptr->prev=NULL; ptr->next=NULL; start=ptr; } else {i=1; cout<<"\n Enter the location where the element is to be inserted in the list: "; cin>>loc; temp=start; while(i<loc-1) {temp=temp->next; i++; } ptr->next=temp->next; ptr->prev=temp; temp->next=ptr; } } break; case 3: if(start==NULL) cout<<"\n UNDERFLOW"; else {ptr=start; if(start->next==NULL) {start=NULL; cout<<"\n "<<ptr->info<<" IS DELETED "; delete ptr; } else {start=start->next; start->prev=NULL; cout<<"\n "<<ptr->info<<" IS DELETED FROM THE BEGINNING"; delete ptr; } } break; case 4: if(start==NULL) VKD {cout<<"\nTHE LIST CONTAINS NO ELEMENT";} else {cout<<"\nTHE ELEMENTS OF THE LIST : "; ptr=start; while(ptr!=NULL) {cout<<" "<<ptr->info; ptr=ptr->next; } } break; case 5: exit(0); break; default:cout<<"\n\n\t WRONG CHOICE ENTERED "; } cout<<"\n\t Do you wish to continue......(y/n) "; cin>>wish; }while(wish=='y' || wish=='Y'); getch(); } VKD OUTPUT SCREEN: VKD VKD