Anda di halaman 1dari 21

Merge Two Arrays

#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[100],b[100],c[100],n,m,i,j,k,s; cout<<"------ Merging Two Array's ------"; cout<<"\n\nEnter No. of Elements in First Array : " ; cin>>n; cout<<"\nEnter Elements in Sorted Order:\n"; for(i=1;i<=n;i++) { cin>>a[i]; } cout<<"\nEnter No. of Elements in Second Array : " ; cin>>m; cout<<"\nEnter Elements in Sorted Order:\n"; for(i=1;i<=m;i++) { cin>>b[i]; } i=1,j=1; for(k=1;k<=n+m;k++) { if(a[i]<b[j]) Array B { c[k]=a[i]; i++; if(i>n) goto b; } else { c[k]=b[j]; j++;

// Compare Elements of Array A and

if(j>m) goto a; } } a: for(s=i;s<=n;s++) Array A to C { k++; c[k]=a[s]; } b: for(s=j;s<=m;s++) Array B to C { k++; c[k]=b[s]; }

// Copy the Remaining Elements of

// Copy the Remaining Elements of

cout<<"\n\nAfter Merging Two Arrays:\n"; for(k=1;k<=n+m;k++) { cout<<c[k]<<endl; } getch(); }

Output :-

Linked List Traversing


#include<iostream.h> #include<conio.h> struct node { int info; node *link; }; void main() { clrscr(); cout<<"\n ------- Linked List Traversing -------\n\n"; node a[100];

node *start=&a[1]; int n,i,item; cout<<"Enter No. of Nodes in Linked List : " ; cin>>n; cout<<"\nEnter Info of Nodes :\n"; for(i=1;i<=n;i++) { cout<<"Node "<<i<<" : "; cin>>a[i].info; a[i].link=&a[i+1]; } a[n].link=0; cout<<"\nAfter Traversing : \n"; i=1; while(start!=0) { cout<<"Node "<<i<<" : "<<a[i].info<<endl; i++; start=start->link; } getch(); }

Output :-

Linked List Searching


#include<iostream.h> #include<conio.h> struct node { int info; node *link; }; void main() { clrscr(); cout<<"\n ------- Linked List Searching -------\n\n"; node a[100]; node *start=&a[1]; int n,i,item,s=0; cout<<"Enter No. of Nodes in Linked List : " ; cin>>n; cout<<"Enter Info of Nodes :\n"; for(i=1;i<=n;i++) { cout<<"Node "<<i<<" : "; cin>>a[i].info;

a[i].link=&a[i+1]; } a[n].link=0; cout<<"\nEnter Item you want to Search : "; cin>>item; i=1; while(start!=0) { if(item==a[i].info) { cout<<"Item is Found at Position : "<<i; s=1; break; } else { i++; start=start->link; } } if(s==0) { cout<<"Item is Not Found"; } getch(); }

Output :-

Insert Element in Queue


#include<iostream.h> #include<conio.h> void main() { clrscr(); int queue[100],loc,front=1,n=100,rear,item; cout<<"\n------ Queue Insertion using Array ------"; cout<<"\n\nEnter Value of Rear : "; cin>>rear; if(front==1 && rear==n) { cout<<"\nQueue is Full"; getch(); return; } cout<<"\nEnter Elements in Queue :\n"; for(loc=front;loc<=rear;loc++) { cin>>queue[loc]; }

ins: cout<<"\nEnter Item you want to Insert : "; cin>>item; rear=rear+1; queue[rear]=item; // Increment the Rear // Insert Element at Rear

cout<<"\nQueue After Insertion :\n"; for(loc=front;loc<=rear;loc++) { cout<<queue[loc]<<endl; } cout<<"\nRear : "<<rear; cout<<"\n\nItem "<<item<<" is Inserted at Rear\n\n"; cout<<"\n\n\nWant to Insert more elements ?????\n\n"; cout<<" -----> Press 1 to Continue\n"; cout<<" -----> Press any Key to Exit\n"; char choice; cin>>choice; if(choice=='1' && front==1 && rear<n) { cout<<endl<<endl; goto ins; } else if(choice=='1' && front==1 && rear==n) { cout<<"\nQueue is Full"; getch(); return; } else { return; } }

Output :-

Delete Element from Queue


#include<iostream.h> #include<conio.h> void main() { clrscr(); int queue[100],loc,front,rear,item; cout<<"\n------ Queue Deletion using Array ------"; cout<<"\n\nEnter Value of Front : ";

cin>>front; if(front==0) { cout<<"\nQueue is already Empty"; getch(); return; } cout<<"\nEnter Value of Rear : "; cin>>rear; cout<<"\nEnter Elements in Queue :\n"; for(loc=front;loc<=rear;loc++) { cin>>queue[loc]; } del: item=queue[front]; Queue front=front+1; // Store the Front Element of // Increment the Front

cout<<"\nQueue After Deletion :\n"; for(loc=front;loc<=rear;loc++) { cout<<queue[loc]<<endl; } cout<<"\nItem "<<item<<" is Deleted from Front\n\n"; cout<<"\n\n\nWant to Delete more elements ?????\n\n"; cout<<" -----> Press 1 to Continue\n"; cout<<" -----> Press any Key to Exit\n"; char choice; cin>>choice; if(choice=='1' && front<=rear) { cout<<endl<<endl; goto del; } else if(choice=='1' && front>rear) {

cout<<"\nQueue is Empty"; getch(); return; } else { return; } }

Output :-

Double linked list program in data structure

#include < iostream.h > #include < conio.h > #include < stdlib.h > struct node { int data; node *prev, *next; }*q, *temp, *start=NULL; int c1, c2 ; void create(); void display(); void insert(); void del(); void main() { clrscr(); while(1) { clrscr(); cout << " \t\t ******* MAIN MENU ******\n" ; cout << " press 1 for adding data \n" ; cout << " press 2 for displaying data \n " ; cout << " press 3 for insertion \n " ; cout << " press 4 for deletion \n " ; cout << " press 0 for exit\n " ; char ch; ch=getch(); switch(ch) { case '1': clrscr(); create(); break; case '2': clrscr(); display(); getch(); break; case '3': clrscr(); insert(); break; case '4': clrscr(); del(); break; case '0': exit(1); } } } void create() { temp = new node;

temp -> next = NULL; cout << "\nEnter data\n " ; cin >> temp -> data ; if(start == NULL) { start = temp; temp -> prev = NULL; } else { q= start; while(q->next != NULL) { q = q->next; } q->next = temp; temp->prev = q; } } void display() { q=start; while(q!=NULL) { cout<<q->data<<endl; q = q->next; } } void insert() { cout << " Press 1 for insertion at start\n " ; cout << "Press 2 for insertion at middle\n " ; cout << "Press 3 for insertion at end\n " ; int choice; cin>>choice; switch(choice) { case 1: clrscr(); temp = new node; cout<<"Enter data \n"; cin>>temp->data; start->prev =temp; temp->next = start; start = temp; temp -> prev = NULL; break; case 2: clrscr(); cout<<"Enter the data aftre which u want to add this\n"; int ch; cin>>ch; q= start; while(q->next!=NULL) { if(q->data == ch)

{ temp = new node; cout<<"Enter data \n"; cin>>temp->data; q->next->prev = temp; temp->next = q->next; temp->prev = q; q->next = temp; } q = q->next; } break; case 3: clrscr(); temp = new node; cout<<"Enter data\n"; cin>> temp->data; temp->next = NULL; q = start; while(q->next != NULL) { q= q->next; } q->next = temp; temp->prev = NULL; } } void del() { clrscr(); cout<<"Enter the data you want to delete \n"; int num; cin>>num; q = start; if (start->data == num) start = start -> next; else { while(q != NULL) { if(q->next->data == num) { temp = q->next; q->next = temp->next; temp->next->prev = q; delete temp; } q = q->next; } } } //An example of a simple double linked list using OOP techniques #include <iostream>

using namespace std; struct Node { double value; Node *N,*P; Node(double y) { value = y; N = P = NULL; } }; class doubleLinkedList { Node *front; Node *back; public: doubleLinkedList() { front = NULL; back = NULL; } ~doubleLinkedList(){ destroyList();} void appendNodeFront(double x); void appendNodeBack(double x); void dispNodesForward(); void dispNodesReverse(); void destroyList(); }; void doubleLinkedList::appendNodeFront(double x) { Node *n = new Node(x); if( front == NULL) { front = n; back = n; } else { front->P = n; n->N = front; front = n; } } void doubleLinkedList::appendNodeBack(double x) { Node *n = new Node(x); if( back == NULL) { front = n; back = n; } else { back->N = n; n->P = back; back = n; }

} void doubleLinkedList::dispNodesForward() { Node *temp = front; cout << "\n\nNodes in forward order:" << endl; while(temp != NULL) { cout << temp->value << " "; temp = temp->N; } } void doubleLinkedList::dispNodesReverse() { Node *temp = back; cout << "\n\nNodes in reverse order :" << endl; while(temp != NULL) { cout << temp->value << " "; temp = temp->P; } } void doubleLinkedList::destroyList() { Node *T = back; while(T != NULL) { Node *T2 = T; T = T->P; delete T2; } front = NULL; back = NULL; } int main() { doubleLinkedList *list = new doubleLinkedList(); //append nodes to front of the list for( int i = 1 ; i < 4 ; i++) list->appendNodeFront(i*1.1); list->dispNodesForward(); list->dispNodesReverse(); //append nodes to back of the list for( int i = 1 ; i < 4 ; i++) list->appendNodeBack(11.0 - (1.1 * i)); cout << endl << endl; list->dispNodesForward(); list->dispNodesReverse(); cout << endl << endl; delete list; return 0; }

/* Program's output Nodes in forward order: 3.3 2.2 1.1 Nodes in reverse order : 1.1 2.2 3.3

Nodes in forward order: 3.3 2.2 1.1 9.9 8.8 Nodes in reverse order : 7.7 8.8 9.9 1.1 2.2 */
# include <iostream.h> # include <process.h> # include <conio.h> int display_menu(); class circularqueue { int arr[10]; int front,rear; int size; public: circularqueue() { front=0; rear=0; size=10; } void display(); void enqueue(); void delete_element(); };

7.7 3.3

void circularqueue :: display() { cout<<endl; if(front!=0 && rear!=0) { int i=front; cout<<"arr["<<i<<"] :"<<arr[i]<<endl; while(i!=rear) { i=(i % size)+1; cout<<"arr["<<i<<"] :"<<arr[i]<<endl; } } else { cout<<"Queue is empty"<<endl; } getch(); } void circularqueue :: enqueue()

{ cout<<endl; if(front==0 && rear==0) { cout<<"Enter Number to enqueue at Position arr["<<rear+1<<"] :"; cin>>arr[1]; rear=1; front=1; } else { int next=(rear % size)+1;

if(next==front) { cout<<"Queue is Full ..."; getch(); } else { cout<<"Enter Number to enqueue at Position arr["<<next<<"] :"; cin>>arr[next]; rear=next; } } } void circularqueue :: delete_element() { cout<<endl; if(rear==0 && front==0) { cout<<"Queue is empty ..."; getch(); return; } if(rear==front) { rear=0; front=0; } else { front=(front % size)+1; } } void main() { circularqueue cq1; while(1) { switch(display_menu()) { case 1: cq1.enqueue(); break;

case 2: cq1.delete_element(); break; case 3: cq1.display(); break; case 4: exit(1); } } } int display_menu() { int c; clrscr(); cout<<endl; cout<<"| 1 | : Enqueue element"<<endl; cout<<"| 2 | : Delete element"<<endl; cout<<"| 3 | : Display"<<endl; cout<<"| 4 | : Exit"<<endl; cout<<"Enter your Choice :"; cin>>c; return c; }

/* Circular Queues */ #include<iostream.h> #include<conio.h> const int MAX = 5; class cqueue { int a[MAX],front,rear; public : cqueue() { front=rear=-1; } void insert(int ); int deletion(); void display(); }; void cqueue :: insert(int val) { if((front==0 && rear==MAX-1) || (rear+1==front)) cout<<" Circular Queue is Full "; else { if(rear==MAX-1) rear=0; else rear++; a[rear]=val;

} if(front==-1) front=0; } int cqueue :: deletion() { int k; if(front==-1) cout<<"Circular Queue is Empty "; else { k=a[front]; if(front==rear) front=rear=-1; else { if(front==MAX-1) front=0; else front++; } } return k; } void cqueue :: display() { int i; if(front==-1) cout<<"Circular Queue is Empty "; else { if(rear < front) { for(i=front;i<=MAX-1;i++) cout<<a[i]<<" "; for(i=0;i<=rear;i++) cout<<a[i]<<" "; } else { for(i=front;i<=rear;i++) cout<<a[i]<<" "; cout<<endl; } } }

void main() { cqueue c1; int ch,val; char op; do { clrscr(); cout<<"-----------Menu------------"; cout<<"1.Insertion 2.Deletion 3.Display 4.Exit "; cout<<"Enter Your Choice <1..4> ?"; cin>>ch; switch(ch) { case 1 : cout<<"Enter Element to Insert ?"; cin>>val; c1.insert(val); break; case 2 : val=c1.deletion(); cout<<"Deleted Element :"<<val<<endl; break; case 3 : c1.display(); break; } cout<<"Do you want to continue<Y/N> ?"; cin>>op; }while(op=='Y' || op=='y'); getch(); }

Anda mungkin juga menyukai