Você está na página 1de 32

Data Structure Program List :

Sorting Programs.
1. 2. 3. 4. 5. Bubble Sort Selection Sort Merge sort Quick Sort Insertion Sort

/* Program to Perform Bubble Sort */ #include<iostream.h> #include<math.h> #include<conio.h> main() { int arr[50],temp,i,j; clrscr(); cout<<Enter any Value less Than 50"<<endl; cin>>n; cout<<Enter The Values into ARRAY "<<endl; for(i=0;i<n;i++) { cout<<Enter Element no <<i+1<<endl; cin>>arr[i]; } for(i=0;i<n-1;i++) { for(j=0;j<n-i-1;j++) { if(arr[j] >a[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } cout<<Sorted Series --"<<endl; for(i=0;i<n;i++) { Cout<<arr[i])<<endl; } getch(); }

/* Program to Perform Sorting using Selection - Sort */ #include<iostream.h> #include<math.h> #include<conio.h> void getdata(int arr[],int n) { int i; cout<<enter the data:<<endl; for(i=0;i<n;i++)

{ Cin>>arr[i]; }} void sort(int arr[],int n) { int i,q,j,k,temp; for(i=0;i<(n-1);i++) { q=i; for(k=i+1;k<n;k++) /*q indicates min element of arr within limits*/ { if(arr[q]>arr[k]) { q=k; } } temp = arr[q]; arr[q] = arr[i]; arr[i] = temp; cout<<"\nP"<<i+1<<endl; for(j=0;j<n;j++) { Cout<<arr[j]<<endl; } }} void main() { int n,arr[20]; clrscr(); cout<<enter number of elements:<<endl; cin>>n; getdata(arr,n); sort(arr,n); getch (); }

/* program To Perform Merge sort */ #include<iostream.h> #include<math.h> #include<conio.h> void getdata(int arr[],int n) { int i; cout<<"\nenter the data<<endl;

for(i=0;i<n;i++) { Cin>>arr[i]; }} void display(int arr[],int n) { int i; cout<<endl; for(i=0;i<n;i++) { Cout<<arr[i]<<endl; } getchar(); } void sort(int arr[],int low,int mid,int high) { int i,j,k,l,b[20]; l=low; i=low; j=mid+1; while((l<=mid)&&(j<=high)) { if(arr[l]<=arr[j]) { b[i]=arr[l]; l++; } else { b[i]=arr[j]; j++; } i++; } if(l>mid) { for(k=j;k<=high;k++) { b[i]=arr[k]; i++; } } else { for(k=l;k<=mid;k++) { b[i]=arr[k];

i++; } } for(k=low;k<=high;k++) { arr[k]=b[k]; } } void partition(int arr[],int low,int high) { int mid; if(low<high) { mid=(low+high)/2; partition(arr,low,mid); partition(arr,mid+1,high); sort(arr,low,mid,high); }} void main() { int arr[20]; int n; clrscr(); cout<<Enter number of data:"<<endl; cin>>n; getdata(arr,n); partition(arr,0,n-1); display(arr,n); getch (); } /* Program to perform Sorting using Quick sort */ #include<iostream.h> #include<math.h> #include<conio.h> void getdata(int arr[],int n) { int i; cout<<"\nenter the data:<<endl; for(i=0;i<n;i++) { Cin>>arr[i]; }} void display(int arr[],int n) { int i; cout<<endl; for(i=0;i<n;i++)

{ Cout<<arr[i]; } } void sort(int arr[],int left,int right) { int i,j,pivot,temp; pivot=arr[left]; i=left+1; j=right; while(i<=j) { while((arr[i]<pivot)&&(i<=(right+1))) { i++; } while(arr[j]>=pivot) { j--; } if(i<=j){ temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; i++; j--; }} arr[left]=arr[i-1]; arr[i-1]=pivot; if(left<j) { sort(arr,left,j-1); } if(i<right) { sort(arr,i,right); } } void main() { int n,arr[20]; clrscr(); cout<<enter number of elements:"<<endl; cin>>n; getdata(arr,n); display(arr,n); sort(arr,0,n-1); cout<<The sorted data is:<<endl;

display(arr,n); getch(); } // Quick Sort Ends // Program to Sort Using Radix sort. #define NUMELTS 100 # include<iostream.h> #include<conio.h> #include<math.h> void radixsort(int a[],int); void main() { int n,a[20],i; clrscr(); cout<< enter the number :"<<endl; cin>>n; cout<< ENTER THE DATA -"<<endl; for(i=0;i<n;i++) { Cout<<i+1<<endl; Cin>>a[i]; } radixsort(a,n); getch(); } void radixsort(int a[],int n) { int rear[10],front[10],first,p,q,exp,k,i,y,j; struct { int info; int next; }node[NUMELTS]; for(i=0;i<n-1;i++) { node[i].info=a[i]; node[i].next=i+1; } node[n-1].info=a[n-1]; node[n-1].next=-1; first=0; for(k=1;k<=2;k++) { //consider only 2 digit number

for(i=0;i<10;i++) { front[i]=-1; rear[i]=-1; } while(first!=-1) { p=first; first=node[first].next; y=node[p].info; exp=pow(10,k-1); j=(y/exp)%10; q=rear[j]; if(q==-1) front[j]=p; else node[q].next=p; rear[j]=p; } for(j=0;j<10&&front[j]==-1;j++) ; first=front[j]; while(j<=9) { for(i=j+1;i<10&&front[i]==-1;i++) ; if(i<=9) { p=i; node[rear[j]].next=front[i]; } j=i; } node[rear[p]].next=-1; } //copy into original array for(i=0;i<n;i++) { a[i]=node[first].info; first=node[first].next; } clrscr(); for(i=0;i<n;i++) cout<<i+1<< <<a[i]<<endl; } // Radix sort ends

/* Program to sort using Insertion sort Technique */ #include<iostream.h> #include<math.h> #include<conio.h> void getdata(int arr[],int n) { int i; cout<<enter the data:<<endl; for(i=0;i<n;i++) { Cin>>arr[i]; }} void sort(int arr[],int n) { int j,k,temp; for(j=1;j<n;j++) { for(k=0;k<j;k++) { if(arr[k]>arr[j]) { temp=arr[j]; arr[j]=arr[k]; arr[k]=temp; }} Cout<<"\nP<<j; for(k=0;k<n;k++) { Cout<<arr[k]; }}}

void main() { int n,arr[20]; clrscr(); cout<<enter number of elements:"<<endl; cin>>n; getdata(arr,n); sort(arr,n); getch(); } // Insertion Sort END

// Searching
1. Binary Search 2. Sequential Search. /* Program to Perform Binary Search */ #include<iostream.h> #include<math.h> #include<conio.h>
int BinarySearch(int [], int, int); int main() { const int NUMEL = 10; int nums[NUMEL] = {5,10,22,32,45,67,73,98,99,101}; int item, location; cout << "Enter the item you are searching for: "; cin >> item; location = BinarySearch(nums, NUMEL, item); if (location > -1) cout << "The item was found at index location " << location << endl; else cout << "The item was not found in the list\n"; return 0; } // this function returns the location of key in the list // a -1 is returned if the value is not found int BinarySearch(int list[], int size, int key) { int left, right, midpt;

left = 0; right = size - 1; while (left <= right) { midpt = (int) ((left + right) / 2); if (key == list[midpt]) { return midpt; } else if (key > list[midpt]) left = midpt + 1; else right = midpt - 1; } return -1; }

// Binary Search End

/* Program to Perform searching using Linear Search */ #include<iostream.h> #include<math.h> #include<conio.h>


int LinearSearch(int [], int, int); int main() { const int NUMEL = 10;

int nums[NUMEL] = {5,10,22,32,45,67,73,98,99,101}; int item, location; cout << "Enter the item you are searching for: "; cin >> item; location = LinearSearch(nums, NUMEL, item); if (location > -1) cout << "The item was found at index location " << location << endl; else cout << "The item was not found in the list\n"; return 0; } // this function returns the location of key in the list // a -1 is returned if the value is not found int LinearSearch(int list[], int size, int key) { int i; for (i = 0; i < size; i++) { if (list[i] == key) return i; } return -1; }

Data Structure Programs for DS operations. 1. Program to implement Stack operation. 2. Conversion for expression into Prefix 3. Conversion for expression into Postfix 4.

// Program to Convert Postfix conversion #include<iostream.h>


#include #include #include #include <string.h> <iomanip.h> <fstream.h> "StackType.h"

// function prototypes int precedence(char symbol); int main() { StackType<char> theStack(5); string aLine, postfixExpression; int i = 0; ifstream infixFile("infix.txt"); infixFile >> aLine; while(!infixFile.eof())

// priming read

int length = aLine.length(); postfixExpression = "";

for(i = 0; i < length; i++) { char lineChar = aLine[i]; //IF symbol is an operand (variable) append it to postfixExpression if (lineChar == 'A' || lineChar == 'B' || lineChar == 'C' || lineChar == 'D' || lineChar == 'E') postfixExpression += lineChar; //IF symbol is an operator ( + , determine to push or pop , * , / )

else if(lineChar == '+' || lineChar == '-' || lineChar == '*' || lineChar == '/') { while(theStack.getTop() != '(' && precedence(theStack.getTop()) >= precedence(lineChar) && ! theStack.isEmpty()) { postfixExpression += theStack.pop(); // pop if top is greater than current } theStack.push(lineChar); // push if current is greater than top } // IF symbol is ( push to stack else if(lineChar == '(') theStack.push(lineChar); // IF symbol is ) determine to pop and put into expression or discard the parenthesis else if(lineChar == ')') { while(theStack.getTop() != '(') { postfixExpression += theStack.pop(); // put operators into expression } theStack.pop(); // pop ( from stack and discard } } while(!theStack.isEmpty()) { postfixExpression += theStack.pop(); // pop reamining symbols from stack } cout << postfixExpression << endl; // display result infixFile >> aLine; // continuation read } system("pause");

return 0;

// This function determines the precedence of the symbol int precedence(char symbol) { if(symbol == '+' || symbol == '-') return 1; else if(symbol == '*' || symbol == '/') return 2; else return -1; }

// Postfix End.

/* Prefix conversion */
#include "string" #include "stdafx.h" #include "stdafx.cpp" #include <fstream> using namespace std; void main() { int i; int string; prefixExp; char token; int value, value1, value2;

stack<int> s; //Declare a stack of int ifstream myfile; ofstream outfile; myfile.open("Prefix.txt", ios::in); //opening streams outfile.open("output.txt", ios::out); while (!myfile.eof()) //while not end of input file { stack<char> operators; stack<char> flags; string line; string outputLine; getline(myfile, line); //read the next line of file and store into line while((i < prefixExp.size()) && (token != '=')) { if(isdigit(token)) { value = token - '0'; s.push(value); } else { value2 = s.top(); s.pop(); value1 = s.top(); s.pop(); switch(token) { case '+': value = value1 + value2; break; case '-': value = value1 - value2; break; case '*': value = value1*value2; break; case '/': value = value1/value2; break; case 'M': value = value1%value2 } s.push(value); } i++; token = prefixExp[i]; } value = s.top(); s.pop(); cout << prefixExp << " " << value << endl; break; }

//

Prefix End

/* Program for Priority Queue */ #include<iostream.h>

#include <cstdlib.h> #include <string.h> class heap { private:

int *array; public: int count; int capacity; heap(int n) { array = new int[n+1]; capacity = n; count = 0; } void put(int d) { int *tmparray; int root=0; count++; array[count]=d; root=array[1]; int i=count; while(i/2>0 && array[i/2]<array[i]){ int x =0;

array[x]=array[i]; array[i]=array[i/2]; array[i/2]=array[x]; i=i/2;

} if(count >= capacity){ int k=count; tmparray=new int [k*2]; for (int j=1;j<=count;j++){ tmparray[j]=array[j]; } capacity=capacity*2; delete(array); array=tmparray; }

} int pop() { int first = array[1]; int j = 1; //cout<<"count = " <<count<<endl; int i=1; array[i]=array[count];

count--; //int i=1; //cout<<"i = " <<i<<endl; //cout<<"count = " <<count<<endl; while(i*2+1<=(count*2)+1){ cout<<"i = "<<i<<endl; if(array[i*2+1]>array[(i*2)]){ int c =0; array[c]=array[i]; array[i]=array[i*2+1]; array[i*2+1]=array[c]; i++; }else if(array[i*2]>array[i*2+1]){ int c =0; array[c]=array[i]; array[i]=array[i*2]; array[i*2]=array[c]; i=i*2; } } // cout<<"after dump = ";dump(); if(count != 0){ return first; }else{ cout<<"error"<<endl; return 0;

} } void dump() { for (int i=1; i<= count; i++) { cout << " array[i] << endl; } cout << endl; } }; int main(void) { heap myheap(4); DUMP: node at index [" << i << "] = " <<

string cmd; int d; while (true) { cin >> cmd >> d;

cout << "MAIN: cmd = " << cmd << ", d = " << d << endl; if (cmd == "put") { myheap.put(d); } else if (cmd == "pop") { int i = myheap.pop(); cout << "pop returns: " << i << endl;

} else if (cmd == "dump") myheap.dump(); else if (cmd == "quit") exit(0); } }

// Program to imlement Circular queue


#include<iostream.h> #include<conio.h> #define NIL -1 #define MAX 5 typedef struct { int terms[MAX]; int front,rear; }que; class queue { que *q; public: queue() { q->front=NIL; q->rear=NIL; } void create(int); int del(void); void display(void); }; void queue::create(int x) { int i; que *t=q; if(((t->front==0) && (t->rear==MAX-1))||(t->front==t->rear+1)) { cout<<"OVERFLOW"; getch(); return; } if(t->front==NIL) t->front=t->rear=0; else if(t->rear==MAX-1) t->rear=0; else t->rear++; t->terms[t->rear]=x; return;

} int queue::del(void) { int n; que *t=q; if((q->front==NIL)&&(q->rear==NIL)) { cout<<"UNDERFLOW"; return(-1); } n=t->terms[t->front]; if(t->front==t->rear) t->front=t->rear=NIL; else if(t->front==MAX-1) t->front=0; else t->front++; return(n); } int main() { int ch,n,x,i; queue qt; do { clrscr(); cout<<"1->Insert \n"; cout<<"2->Delete \n"; cout<<"3->Exit\n"; cout<<"Enter your choice:"; cin>>ch; switch(ch) { case '1': cout<<"Enter the term to insert:"; cin>>x; qt.create(x); getch(); break; case '2': x=qt.del(); if(x!=-1) cout << "Deleted term is " << x; getch(); break; case '3': cout<<"Exiting"; getch(); break; default: cout<<"Not a valid choice"; getch(); break; } }while(ch!=3); getch(); return(0); } // Circular Queue ends.

//Program to implement Push POP and Display operation #include<iostream.h>


#include<conio.h> // Creating a NODE Structure struct node { int data; struct node *next; }; // Creating a class STACK class stack { struct node *top; public: stack() // constructure { top=NULL; } void push(); // to insert an element void pop(); // to delete an element void show(); // to show the stack }; // PUSH Operation void stack::push() { int value; struct node *ptr; cout<<"\nPUSH Operation\n"; cout<<"Enter a number to insert: "; cin>>value; ptr=new node; ptr->data=value; ptr->next=NULL; if(top!=NULL) ptr->next=top; top=ptr; cout<<"\nNew item is inserted to the stack!!!"; getch(); } // POP Operation void stack::pop() { struct node *temp; if(top==NULL) { cout<<"\nThe stack is empty!!!"; getch(); return; } temp=top; top=top->next;

cout<<"\nPOP Operation........\nPoped value is "<<temp->data; delete temp; getch();

// Show stack void stack::show() { struct node *ptr1=top; cout<<"\nThe stack is\n"; while(ptr1!=NULL) { cout<<ptr1->data<<" ->"; ptr1=ptr1->next; } cout<<"NULL\n"; getch(); } // Main function int main() { clrscr(); stack s; int choice; while(1) { cout<<"\n-----------------------------------------------------------"; cout<<"\n\t\tSTACK USING LINKED LIST\n\n"; cout<<"1:PUSH\n2:POP\n3:DISPLAY STACK\n4:EXIT"; cout<<"\nEnter your choice(1-4): "; cin>>choice; switch(choice) { case 1: s.push(); break; case 2: s.pop(); break; case 3: s.show(); break; case 4: exit(0); break; default: cout<<"Please enter correct choice(1-4)!!"; getch(); break; } } return 0; }

// Stack Program Ends.

// Program for Singly linked list #include<iostream.h> #include<conio.h> #include<stdlib.h> class list { struct { int data; node *link; }*p; public: void inslast(int); void insbeg(int); void insnext(int,int); void delelement(int); void delbeg(); void dellast(); void disp(); int seek(int); list(){p=NULL;} ~list(); }; void list::inslast(int x) { node *q,*t; if(p==NULL) { p=new node; p->data=x; p->link=NULL; } else { q=p;

while(q->link!=NULL) q=q->link; t=new node; t->data=x; t->link=NULL; q->link=t; } cout<<" Inserted successfully at the end.."; disp(); } void list:: insbeg(int x) { node *q; q=p; p=new node; p->data=x; p->link=q; cout<<" Inserted successfully at the begining.."; disp(); } void list::delelement(int x) { node *q,*r; q=p; if(q->data==x) { p=q->link; delete q; return; } r=q; while(q!=NULL) { if(q->data==x) { r->link=q->link; delete q; return; } r=q; q=q->link; } cout<<" Element u entered "<<x<<" }

is not found..";

void list:: delbeg() { cout<<"The list before deletion:"; disp(); node *q; q=p; if(q==NULL) { cout<<"No data is present.."; return; } p=q->link; delete q; return; } void list:: dellast() { cout<<"The list before deletion:"; disp(); node *q,*t; q=p; if(q==NULL) { cout<<"There is no data in the list.."; return; } if(q->link==NULL) { p=q->link; delete q; return; } while(q->link->link!=NULL) q=q->link; q->link=NULL; return; } list::~list() { node *q; if(p==NULL) return; while(p!=NULL) { q=p->link;

delete p; p=q; } } void list::disp() { node *q; q=p; if(q==NULL) { cout<<"No data is in the list.."; return; } cout<<"The items present in the list are :"; while(q!=NULL) { cout<<" "<<q->data; q=q->link; } } void list :: insnext(int value,int position) { node *temp,*temp1; temp=p; if(temp1==NULL) { temp1= new node; temp1->data=value; temp1->link=NULL; p=temp1; return; } for(int i=0;((i<position)&&(temp->link!=NULL)) ;i++) { if(i==(position-1)) { temp1= new node; temp1->data= value; temp1->link=temp->link; temp->link=temp1; } temp=temp->link; } //cout<<" Inserted successfully at the position.."<<position; disp();

} int list::seek(int value) { node *temp; temp=p; int position=0; while(temp!=NULL) { if(temp->data==value) return position+1; else { temp=temp->link; position=position+1; } } cout<<"Element "<<value<<" not found"; return 0; } void main() { list l; int ch,v,p,ps; do { clrscr(); cout<<"Operations on List.."; cout<<"1.Insertion 2.Deletion 3.Display 4.Seek 5.Exit"; cout<<"Enter ur choice:"; cin>>ch; switch(ch) { case 1: cout<<"1.Insertion at beginning 2.Insertion at the end"; cout<<"3.Insertion after the mentioned position"; cout<<"Enter ur choice:"; cin>>ps; cout<<"Enter the value to insert:"; cin>>v; switch(ps) { case 1: l.insbeg(v); break;

case 2: l.inslast(v); break; case 3: cout<<"Enter the position to insert the value:"; cin>>p; l.insnext(v,p); break; default: cout<<"The choice is invalid"; return; } break; case 2: cout<<"1.Delete the first element 2.Delete the last element"; cout<<"3.Enter the element to delete from the list"; cout<<"Enter ur choice:"; cin>>ps; switch(ps) { case 1: l.delbeg(); cout<<"The list after deletion:"; l.disp(); break; case 2: l.dellast(); cout<<"The list after deletion:"; l.disp(); break; case 3: l.disp(); cout<<"Enter the element to delete : "; cin>>v; l.delelement(v); cout<<"The list after deletion:"; l.disp(); break; default: cout<<"The option is invalid..."; break; } break; case 3: l.disp(); break;

case 4: l.disp(); cout<<"Enter the element to search:"; cin>>v; cout<<"The position of the element "<< v<<" is "<<l.seek(v); getch(); break; case 5: exit(1); default: cout<<"The option is invalid..."; return; } getch(); }while(ch!=5); getch(); return; }

Você também pode gostar