Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.58 KB | None | 0 0
  1. //ARRAY MANIPULATION
  2. #include<iostream>
  3. #include<math.h>
  4. using namespace std;
  5. void maxx(int*,int);
  6. int main(){
  7. int n,f,s,m;
  8. int a[100];
  9. cout<<"Enter the number of elements in array : ";
  10. cin>>n;
  11. int arr[n];
  12. int arr2[n];
  13. double arr1[n];
  14. double avg=0,Md=0;
  15. for(int i=0;i<n;i++){
  16. cin>>arr[i];
  17. arr2[i]=arr[i];
  18. }
  19. for(int i=0;i<n;i++){
  20. avg=avg+arr[i];
  21. }
  22. avg=avg/n;
  23. cout<<"Mean : "<<avg<<endl;
  24. for(int i=0;i<n;i++){
  25. arr1[i]=arr[i]-avg;
  26. arr1[i]=arr[i]*arr1[i];
  27. Md=Md+arr1[i];
  28. }
  29. Md=Md/n;
  30. Md=sqrt(Md);
  31. cout<<"Mean Deviation : "<<Md;
  32. maxx(&arr[0],n);
  33. cout<<"enter the number to be searched : ";
  34. cin>>f;
  35. cout<<"Position of the number: ";
  36. for(int i=0;i<n;i++){
  37. if(arr2[i]==f){
  38. cout<<i<<",";
  39. }
  40. }
  41.  
  42. cout<<endl<<"Enter the index of the number to be deleted : ";
  43. cin>>f;
  44. if(f<n){
  45. for(int i=0;i<f;i++){
  46. arr2[i]=arr2[i];
  47. }
  48. for(int i=f;i<n-1;i++){
  49. arr2[i]=arr2[i+1];
  50. }
  51.  
  52. for(int i=0;i<n-1;i++){
  53. cout<<endl<<arr2[i];
  54. }
  55. }
  56. else{
  57. cout<<"Index is out of Range.";
  58. }
  59. cout<<endl;
  60.  
  61. cout<<"enter a number to check: ";
  62. cin>>s;
  63. cout<<endl;
  64. for(int i=0;s!=0;i++){
  65. a[i]=s%10;
  66. s=s/10;
  67. m=i+1;
  68. }
  69. for(int i=0;i<m;i++){
  70. cout<<a[i];
  71. }
  72. for(int i=0;i<(m+1)/2;i++){
  73. if(a[i]==a[m-i-1]){
  74. if(i==(m-1)/2){
  75. cout<<endl<<"number is palindrome number.";
  76. break;
  77. }
  78. }
  79. else{
  80. cout<<endl<<"number is not palindrome number";
  81. break;
  82. }
  83. }
  84. return 0;
  85. }
  86. void maxx(int *a,int n){
  87. int temp;
  88. for(int i=0;i<n;i++){
  89. for(int j=i+1;j<n;j++){
  90. if(a[i]>a[j]){
  91. temp=a[j];
  92. a[j]=a[i];
  93. a[i]=temp;
  94. }
  95. }
  96. }
  97. cout<<endl<<"Max elements : "<<a[n-1]<<endl<<"Min element :"<<a[0]<<endl;
  98. }
  99.  
  100.  
  101.  
  102.  
  103. //PALINDROME
  104.  
  105. #include<iostream>
  106. #include<stdio.h>
  107. #include<string.h>
  108. using namespace std;
  109. int main()
  110. {
  111. int i,j,l,c=0;
  112. char s[20];
  113. cout<<"Enter a string :-";
  114. cin>>s;
  115. l=strlen(s);
  116. for(i=0,j=l-1;i<l;i++,j--)
  117. {
  118. if(s[i]!=s[j])
  119. {
  120. cout<<"Not a Palindorme string.";
  121. c++;
  122. break;
  123. }
  124. }
  125. if(c==0)
  126. cout<<"Palindrome string.";
  127. return 0;
  128. }
  129.  
  130.  
  131. //MATRIX MANIPULATION
  132. #include<bits/stdc++.h>
  133. using namespace std;
  134. int main(){
  135. int arr1[3][3];
  136. int arr2[3][3];
  137. int add[3][3];
  138. int sub[3][3];
  139. int mul[3][3];
  140. int n;
  141. cout<<"enter the elements of Matrix 1: ";
  142. for(int i=0;i<3;i++){
  143. for(int j=0;j<3;j++){
  144. cin>>arr1[i][j];
  145. }
  146. }
  147.  
  148. cout<<"enter the elements of Matrix 2: ";
  149. for(int i=0;i<3;i++){
  150. for(int j=0;j<3;j++){
  151. cin>>arr2[i][j];
  152. }
  153. }
  154.  
  155. cout<<"The sum of Matrix 1 & 2: \n";
  156. for(int i=0;i<3;i++){
  157. for(int j=0;j<3;j++){
  158. add[i][j]=arr1[i][j]+arr2[i][j];
  159. cout<<add[i][j]<<"\t";
  160. }
  161. cout<<endl;
  162. }
  163.  
  164. cout<<endl<<"The subtraction of Matrix 1 & 2: \n";
  165. for(int i=0;i<3;i++){
  166. for(int j=0;j<3;j++){
  167. sub[i][j]=arr1[i][j]-arr2[i][j];
  168. cout<<sub[i][j]<<"\t";
  169. }
  170. cout<<endl;
  171. }
  172.  
  173. cout<<"The multiplication of Matrix 1 & 2: \n";
  174. for(int i=0;i<3;i++){
  175. int sum=0;
  176. for(int j=0;j<3;j++){
  177. for(int k=0;k<3;k++){
  178. sum=sum+arr1[i][k]*arr2[k][j];
  179. }
  180. mul[i][j]=sum;
  181. cout<<mul[i][j]<<"\t";
  182. }
  183. cout<<endl;
  184. }
  185. return 0;
  186. }
  187.  
  188. //MERGE TWO SORTED ARRAYS INTO ONE.
  189. #include<iostream>
  190. using namespace std;
  191. int main(){
  192. int a[10],b[10], c[10], i=0,temp,j;
  193. cout<<"Enter the elements of first array:-\n";
  194. for(i=0;i<5;i++)
  195. {
  196. cin>>a[i];
  197. }
  198. cout<<"Enter the elements of second array :-\n";
  199. for(i=0;i<5;i++)
  200. {
  201. cin>>b[i];
  202. }
  203. //sorting arrays
  204. for(i=1;i<5;i++){
  205. for(j=0;j<5;j++){
  206. if(a[i]<a[j]){
  207. temp=a[i];
  208. a[i]=a[j];
  209. a[j]=temp;
  210. }
  211. if(b[i]<b[j]){
  212. temp=b[i];
  213. b[i]=b[j];
  214. b[j]=temp;
  215. }
  216. }
  217. }
  218. cout<<"First sorted array:-\n";
  219. for(i=0;i<5;i++){
  220. cout<<a[i]<<" ";
  221. }
  222. cout<<"\nSecond sorted array:-\n";
  223. for(i=0;i<5;i++){
  224. cout<<b[i]<<" ";
  225. }
  226. for(i=0;i<5;i++){
  227. c[i]=a[i];
  228. c[i+5]=b[i];
  229. }
  230. //Sorting merged arrays
  231. for(i=1;i<10;i++){
  232. for(j=0;j<10;j++){
  233. if(c[i]<c[j]){
  234. temp=c[i];
  235. c[i]=c[j];
  236. c[j]=temp;
  237. }
  238. }
  239. }
  240. cout<<"\nMerged Sorted array:\n";
  241. for(i=0;i<10;i++){
  242. cout<<c[i]<<" ";
  243. }
  244. return 0;
  245. }
  246.  
  247.  
  248.  
  249. //STACK BASICS
  250. #include<iostream>
  251. #define MAX 5
  252. int top,status;
  253. using namespace std;
  254. void push(int stack[], int item){
  255. if(top==(MAX-1))
  256. status=0;
  257. else {
  258. status =1;
  259. top++;
  260. stack[top]=item;
  261. }
  262. }
  263. int pop(int stack[]){
  264. int ret;
  265. if(top==-1){
  266. ret=0;
  267. status=0;
  268. }
  269. else{
  270. status=1;
  271. ret=stack[top];
  272. --top;
  273. }
  274. return ret;
  275. }
  276. void display(int stack[]){
  277. int i;
  278. cout<<"The Stack is :";
  279. if(top==-1)
  280. cout<<"empty\n";
  281. else{
  282. cout<<endl;
  283. for(i=top;i>=0;--i)
  284. cout<<stack[i]<<endl;
  285. cout<<endl;
  286. }
  287. }
  288. int main(){
  289. int stack[MAX],item,ch;
  290. top=-1;
  291. do{
  292. do{
  293. cout<<"\nMain Menu\n"<<"1.Push an element.\n"<<"2.Pop an element.\n"<<"3.Exit!\n"<<"Enter your choice :-";
  294. cin>>ch;
  295. if(ch<1 || ch>3)
  296. cout<<"\nInvalid Choice, Please try again!!";
  297. }while(ch<1 || ch>3);
  298. switch(ch) {
  299. case 1:
  300. cout<<"Enter the element to be pushed :-";
  301. cin>>item;
  302. push(stack,item);
  303. if (status){
  304. cout<<"\nAfter Pushing ";
  305. display (stack);
  306. if (top == (MAX-1))
  307. cout<<"\nThe Stack is Full";
  308. }
  309. else
  310. cout<<"\nStack overflow on Pushn\n";
  311. break;
  312. case 2:
  313. item = pop (stack);
  314. if (status){
  315. cout<<"\After Popping: ";
  316. display (stack);
  317. }
  318. else
  319. cout<<"\nStack underflow on Pop\n";
  320. break;
  321. default:
  322. cout<<"\nEND OF EXECUTION";
  323. }
  324. }while (ch != 3);
  325. return 0;
  326. }
  327.  
  328.  
  329. //INFIX TO POSTFIX
  330. #include <iostream>
  331. #include <stack>
  332. #include <string>
  333. using namespace std;
  334. void print(stack<char>&stk);
  335. int main(){
  336. stack<char>stk;
  337. string S;
  338. cin>> S;
  339. S = '(' + S + ')';
  340. for(int i = 0; i <S.length(); i++){
  341. if(S[i] == '(')
  342. stk.push(S[i]);
  343. else if(S[i] == '+' || S[i] == '-'){
  344. if(stk.top() == '*' || stk.top() == '/' || stk.top() == '^')
  345. print(stk);
  346. stk.push(S[i]);
  347. }
  348. else if(S[i] == '*' || S[i] == '/'){
  349. if(stk.top() == '^')
  350. print(stk);
  351. stk.push(S[i]);
  352. }
  353. else if(S[i] == '^')
  354. stk.push(S[i]);
  355. else if(S[i] == ')') {
  356. print(stk);
  357. stk.pop();
  358. }
  359. else
  360. cout<< S[i];
  361. }
  362. return 0;
  363. }
  364. void print(stack<char>&stk){
  365. while(stk.top() != '(')
  366. std::cout<<stk.top(), stk.pop();
  367. }
  368.  
  369. //POSTFIX EVALUATION
  370. #include<iostream>
  371. #include<stack>
  372. #include<string.h>
  373. using namespace std;
  374. int main()
  375. { int i=0,c,a,b,len;
  376. stack<int>stk;
  377. string p;
  378. cout<<"Enter a postfix expression:- ";
  379. cin>>p;
  380. for(i=0;i<p.length();i++)
  381. { if(p[i]=='+' || p[i]=='-' || p[i]=='*' || p[i]=='/')
  382. {
  383. b=stk.top();
  384. stk.pop();
  385. a=stk.top();
  386. stk.pop();
  387. if(p[i]=='+')
  388. c=a+b;
  389. else if(p[i]=='-')
  390. c=a-b;
  391. else if(p[i]=='*')
  392. c=a*b;
  393. else if(p[i]=='/')
  394. c=a/b;
  395. stk.push(c);
  396. }
  397. else{
  398. int t = p[i] - '0';
  399. stk.push(t);
  400. }
  401. }
  402. cout<<stk.top();
  403. return 0;
  404. }
  405.  
  406. //FIBONACCI (RECURSIVE)
  407. #include<iostream>
  408. using namespace std;
  409. int fib(int );
  410. int main(){
  411. int n;
  412. cout<<"enter the length of series : ";
  413. cin>>n;
  414. for(int i=0;i<n;i++){
  415. cout<<fib(i)<<"\t";
  416. }
  417. return 0;
  418. }
  419. int fib(int n){
  420. int f;
  421. if(n==0||n==1){
  422. return 1;
  423. }
  424. else{
  425. f=fib(n-1)+fib(n-2);
  426. return f;
  427. }
  428. }
  429.  
  430. //FIBONACCI (NON-RECURSIVE)
  431. #include<iostream>
  432. using namespace std;
  433. void fib(int );
  434. int main(){
  435. int n;
  436. cout<<"Enter the length of series : ";
  437. cin>>n;
  438. fib(n);
  439. return 0;
  440. }
  441. void fib(int n){
  442. int i;
  443. int temp;
  444. int x=0;
  445. int y=1;
  446. if(n==1){
  447. cout<<n<<"\t";
  448. }
  449. else{
  450. cout<<"1"<<"\t";
  451. for(i=2;i<=n;i++){
  452. cout<<x+y<<"\t";
  453. temp=y;
  454. y=x+y;
  455. x=temp;
  456. }
  457. }
  458. }
  459.  
  460. // Swap 2 top elements of a stack.
  461. #include<iostream>
  462. using namespace std;
  463. class stck{
  464. int stack[10];
  465. int tos=-1;
  466. public:
  467. void push(int x){
  468. if(tos==9){
  469. cout<<"stack is full.\n";
  470. }
  471. else{
  472. tos=tos+1;
  473. stack[tos]=x;
  474. }
  475. }
  476. int pop(){
  477. int x;
  478. if(tos<0){
  479. cout<<"stack is empty.\n";
  480. }
  481. else{
  482. x=stack[tos];
  483. tos=tos-1;
  484. return x;
  485. }
  486. }
  487. void swap(){
  488. int x,y;
  489. x=pop();
  490. y=pop();
  491. push(x);
  492. push(y);
  493. }
  494. void print(){
  495. for(int i=0;i<=tos;i++){
  496. cout<<stack[i]<<"\t";
  497. }
  498. }
  499. };
  500. int main(){
  501. stck s;
  502. int n,m;
  503. cout<<"number of elements to be pushed : ";
  504. cin>>m;
  505. cout<<"NUMBERS -- ";
  506. for(int i=0;i<m;i++){
  507. cin>>n;
  508. s.push(n);
  509. }
  510. s.print();
  511. cout<<endl;
  512. cout<<"After swap"<<endl;
  513. s.swap();
  514. s.print();
  515. return 0;
  516. }
  517.  
  518.  
  519.  
  520. //REVERSE THE STACK
  521. #include<iostream>
  522. #include<stack>
  523. using namespace std;
  524. int main(){
  525. stack<int>s,r;
  526. int n;
  527. cout<<"Enter the size of stack-- ";
  528. cin>>n;
  529. cout<<"Enter the elements -- ";
  530. for(int i=0;i<n;i++){
  531. int x;
  532. cin>>x;
  533. s.push(x);
  534. }
  535. cout<<endl<<"After reversing numbers --"<<"\t";
  536. for(int i=0;i<n;i++){
  537. int z;
  538. z=s.top();
  539. r.push(z);
  540. s.pop();
  541. }
  542. for(int i=0;i<n;i++){
  543. cout<<r.top();
  544. r.pop();
  545. cout<<"\t";
  546. }
  547. return 0;
  548. }
  549.  
  550.  
  551. //QUEUE(INSERT AND DELETE)
  552. #include<iostream>
  553. using namespace std;
  554. #define MAX 50
  555. int q[MAX];
  556. int display();
  557. int insert();
  558. int del();
  559. int search();
  560. int menu();
  561. int rear = - 1;
  562. int front = - 1;
  563. int main()
  564. {
  565. int c;
  566. while(1){
  567. cout<<"\n1.Insert an element\n2. Delete an element\n3. Search an element\n4. Display the queue\n5.Exit \nEnter your choice: ";
  568. cin>>c;
  569. if(c==1)
  570. insert();
  571. if(c==2)
  572. del();
  573. if(c==3)
  574. search();
  575. if(c==4)
  576. display();
  577. if(c==5)
  578. exit(1);
  579. else if(c<1 || c>5)
  580. cout<<"Invalid Choice";
  581. }
  582. return 0;
  583. }
  584. insert(){
  585. int add_item;
  586. if (rear == MAX - 1)
  587. cout<<"Queue Overflow \n";
  588. else{
  589. if (front == - 1)
  590. front = 0;
  591. cout<<"Insert the element in queue : ";
  592. cin>>add_item;
  593. rear = rear + 1;
  594. q[rear] = add_item;
  595. }
  596. cout<<endl;
  597. }
  598. del(){
  599. if (front == - 1 || front > rear){
  600. cout<<"\n\n\tQueue Underflow \n\n";
  601. return 0 ;
  602. }
  603. else{
  604. cout<<"\n\nElement deleted from queue is :"<<q[front]<<"\n\n";
  605. front = front + 1;
  606. }
  607. }
  608. display(){
  609. int i;
  610. if (front == - 1)
  611. cout<<"Queue is empty \n";
  612. else{
  613. cout<<"\n\n\tQueue is : \n\t";
  614. for (i = front; i <= rear; i++)
  615. cout<<q[i]<<" ";
  616. cout<<"\n\n";
  617. }
  618. }
  619. search(){
  620. int i,s,x=0;
  621. cout<<"Enter the element to be searched:";
  622. cin>>s;
  623. for(i=front;i<=rear;i++){
  624. if(q[i]==s)
  625. cout<<"\n\tElement is present in the queue\n\n";
  626. else
  627. x++;
  628. }
  629. if(x==1)
  630. cout<<"\n\tElement is not present in the queue!\n\n";
  631. }
  632.  
  633. //QUEUE WITH TWO STACKS
  634. #include<iostream>
  635. #include<stack>
  636. using namespace std;
  637. class queue {
  638. stack<int> s1, s2;
  639. public :
  640. void enqueue(int element);
  641. int dequeue();
  642. void displayQueue();
  643. };
  644. void queue :: enqueue(int element) {
  645. s1.push(element);
  646. }
  647. int queue :: dequeue() {
  648. while (!s1.empty()) {
  649. s2.push(s1.top());
  650. s1.pop();
  651. }
  652. int ret = s2.top();
  653. s2.pop();
  654. while (!s2.empty()) {
  655. s1.push(s2.top());
  656. s2.pop();
  657. }
  658. return ret;
  659. }
  660. void queue :: displayQueue() {
  661. cout<<"\nDisplaying the queue :-\n";
  662. while (!s1.empty()) {
  663. s2.push(s1.top());
  664. s1.pop();
  665. }
  666. while (!s2.empty()) {
  667. cout<<s2.top()<<" ";
  668. s1.push(s2.top());
  669. s2.pop();
  670. }
  671. }
  672. int main() {
  673. queue q;
  674. q.enqueue(5);
  675. q.enqueue(11);
  676. q.enqueue(1);
  677. q.enqueue(7);
  678. q.enqueue(13);
  679. q.enqueue(11);
  680. q.displayQueue();
  681. int dq_element = q.dequeue();
  682. cout<<"\nAfter dequeing :-";
  683. q.displayQueue();
  684. cout<<endl;
  685. return 0;
  686. }
  687.  
  688.  
  689. //LINKED LIST
  690. //NODE BEFORE LAST NODE
  691. #include<iostream>
  692. using namespace std;
  693. void insert(int);
  694. void print(void);
  695. int printadd_before_last_element();
  696. class node{
  697. public:
  698. int data;
  699. node *next;
  700. };
  701. node *head;
  702. int main(){
  703. int n,x;
  704. cout<<"Enter the total number to be inserted in the linklist-- ";
  705. cin>>n;
  706. for(int i=0;i<n;i++){
  707. cout<<"Enter the number -- ";
  708. cout<<endl;
  709. cin>>x;
  710. insert(x);
  711. }
  712. cout<<"\nList: "<<"\n";
  713. print();
  714. cout<<"Address of element before last element -- ";
  715. printadd_before_last_element();
  716. return 0;
  717. }
  718.  
  719.  
  720. void insert(int x){
  721. node *temp=new node();
  722. node *temp1;
  723. temp1=head;
  724. temp->data=x;
  725. temp->next=NULL;
  726. if(head==NULL){
  727. head=temp;
  728. }
  729. else{
  730. while(temp1->next!=NULL){
  731. temp1=temp1->next;
  732. }
  733. temp1->next=temp;
  734. }
  735. }
  736. void print()
  737. {
  738. node *temp=head;
  739. while(temp!=NULL){
  740. cout<<temp->data<<"\t";
  741. temp=temp->next;
  742. }
  743. cout<<"\n\n";
  744. }
  745. int printadd_before_last_element(){
  746. node* s1=head;
  747. node* s2;
  748. if(s1==NULL)
  749. return 0;
  750. if(s1->next==NULL)
  751. return 0;
  752. while(s1->next!=NULL){
  753. s2=s1;
  754. s1=s1->next;
  755. }
  756. cout<<s2;
  757. }
  758.  
  759.  
  760. //DOUBLE LINKED LIST
  761. #include<iostream>
  762. using namespace std;
  763. void insatbeg(int);
  764. void insatend(int);
  765. void del_beg();
  766. void del_end();
  767. void display();
  768. struct node
  769. { int data;
  770. struct node *pre, *nex;
  771. }
  772. *head=NULL;
  773. int main(){
  774. int choice,val;
  775. while(1)
  776. { cout<<"\n"<<"1. Insert at begining \n"<<"2. Insert at end\n"<<"3.Delete at begining\n"<<"4.Delete at end\n"<<"5.display\n"<<"6.Exit\n";
  777. cin>>choice;
  778. switch(choice)
  779. { case 1:
  780. cout<<"Enter the value to be inserted :-";
  781. cin>>val;
  782. insatbeg(val);
  783. break;
  784. case 2:
  785. cout<<"Enter the value to be inserted :-";
  786. cin>>val;
  787. insatend(val);
  788. break;
  789. case 3:
  790. del_beg();
  791. break;
  792. case 4:
  793. del_end();
  794. break;
  795. case 5:
  796. display();
  797. break;
  798. case 6:
  799. exit(1);
  800. }
  801. cout<<endl;
  802. }
  803. return 0;
  804. }
  805. void insatbeg(int x)
  806. { struct node *newNode= new node();
  807. newNode->data=x;
  808. newNode->pre =NULL;
  809. if(head==NULL)
  810. { newNode->nex= NULL;
  811. head = newNode;
  812. }
  813. else
  814. { newNode->nex=head;
  815. head = newNode;
  816. }
  817. cout<<"Inerted Successfully!"<<endl;
  818. }
  819.  
  820. void insatend(int x)
  821. { struct node *newNode = new node();
  822. newNode->data=x;
  823. newNode->nex=NULL;
  824. if(head==NULL)
  825. { newNode->pre=NULL;
  826. head=newNode;
  827. }
  828. else
  829. { struct node *temp=head;
  830. while(temp->nex!=NULL)
  831. { temp=temp->nex;
  832. }
  833. temp->nex=newNode;
  834. newNode->pre=temp;
  835. }
  836. cout<<"Inesertion successful!"<<endl;
  837. }
  838. void del_beg(){
  839. if(head==NULL){
  840. cout<<endl<<"List is empty deletion not possible!"<<endl;
  841. return ;
  842. }
  843. else{
  844. node *temp=head;
  845. if(temp->pre==temp->nex){
  846. head=NULL;
  847. }
  848. else{
  849. head=temp->nex;
  850. head->pre=NULL;
  851. }
  852. display();
  853. }
  854. }
  855. void del_end()
  856. { if(head==NULL)
  857. cout<<endl<<"List is Empyt!!!Deletion is not possible."<<endl;
  858. else{ node *temp=head;
  859. if(temp->pre==NULL && temp->nex==NULL)
  860. { head=NULL;
  861.  
  862. }
  863. else{
  864. while(temp->nex->nex!=NULL)
  865. { temp=temp->nex;
  866. }
  867. temp->nex=NULL;
  868. }
  869. display();
  870. }
  871. }
  872. void display()
  873. { if(head==NULL)
  874. { cout<<endl<<"List is empty!"<<endl<<endl;
  875. return;
  876. }
  877. else{
  878. node *temp=head;
  879. cout<<endl;
  880. cout<<"NULL <--";
  881. while(temp->nex!=NULL)
  882. { cout<<temp->data<<"<===>";
  883. temp=temp->nex;
  884. } cout<<temp->data<<" --> NULL";
  885. } cout<<endl<<endl;
  886. }
  887. // circular single linked list.
  888. #include<iostream>
  889. using namespace std;
  890. void insert(int);
  891. void del();
  892. void display();
  893. struct node{
  894. int data;
  895. struct node *next;
  896. }*head=NULL;
  897. int main()
  898. { while(1)
  899. { int choice, val;
  900. cout<<"Menu: \n"<<"1.Insert\n"<<"2.Delete\n"<<"3.Display\n"<<"4.Exit\n";
  901. cin>>choice;
  902. switch(choice)
  903. { case 1:
  904. cout<<"Enter the value to be inserted :-";
  905. cin>>val;
  906. insert(val);
  907. break;
  908. case 2:
  909. del();
  910. break;
  911. case 3:
  912. display();
  913. break;
  914. case 4:
  915. exit(1);
  916. }
  917. cout<<endl;
  918. }
  919. return 0;
  920. }
  921. void insert(int x){
  922. struct node *newNode = new node();
  923. newNode->data=x;
  924. if(head==NULL){
  925. head=newNode;
  926. newNode->next=head;
  927. }
  928. else{
  929. node *temp=head;
  930. while(temp->next!=head){
  931. temp=temp->next;
  932. }
  933. newNode->next=head;
  934. head=newNode;
  935. temp->next=head;
  936. }
  937. cout<<endl;
  938. }
  939. void del(){
  940. if(head==NULL){
  941. cout<<"List is empty!";
  942. }
  943. else{
  944. struct node *temp1=head;
  945. struct node *temp2=head;
  946. if(temp1->next==head){
  947. head==NULL;
  948. }
  949. else{
  950. while(temp1->next!=head){
  951. temp1=temp1->next;
  952. }
  953. head=temp2->next;
  954. temp1->next=head;
  955. }
  956. display();
  957. }
  958. }
  959. void display(){
  960. if(head==NULL){
  961. cout<<"\nList is empty!\n";
  962. }
  963. else{
  964. struct node *temp=head;
  965. while(temp->next!=head){
  966. cout<<temp->data<<"-->";
  967. temp=temp->next;
  968. }
  969. cout<<temp->data<<"-->"<<head->data;
  970. }
  971. }
  972.  
  973. // inorder, preorder and postorder binary tree traversal.
  974. //Write a program to insert ,find, min, max in a binary tree.
  975. #include<iostream>
  976. #include<stdlib.h>
  977. using namespace std;
  978. struct node{
  979. struct node *lchd;
  980. int data;
  981. struct node *rchd;
  982. };
  983. const int MAX=10;
  984. struct node *stack[MAX];
  985. int top=-1;
  986. void push_stack(struct node *item){
  987. if(top==(MAX-1)){
  988. cout<<"stack overflow."<<endl;
  989. }
  990. else{
  991. top=top+1;
  992. stack[top]=item;
  993. }
  994. }
  995. struct node* pop_stack(){
  996. struct node *item;
  997. if(top==-1){
  998. cout<<"stack is empty"<<endl;
  999. }
  1000. else{
  1001. item=stack[top];
  1002. top--;
  1003. return item;
  1004. }
  1005. }
  1006. int stack_empty(){
  1007. if(top==-1)
  1008. return 1;
  1009. else
  1010. return 0;
  1011. }
  1012. void pre(struct node *root){
  1013. struct node *ptr =root;
  1014. if(ptr==NULL){
  1015. cout<<"Tree is empty.";
  1016. return;
  1017. }
  1018. push_stack(ptr);
  1019. while(!stack_empty()){
  1020. ptr=pop_stack();
  1021. cout<<ptr->data<<endl;
  1022. if(ptr->rchd!=NULL){
  1023. push_stack(ptr->rchd);
  1024. }
  1025. if(ptr->lchd!=NULL){
  1026. // cout<<ptr->data;
  1027. push_stack(ptr->lchd);
  1028. }
  1029. }
  1030. }
  1031. void in(struct node *root){
  1032. struct node *ptr=root;
  1033. if(ptr==NULL){
  1034. cout<<"stack is empty"<<endl;
  1035. return;
  1036. }
  1037. while(1){
  1038. while(ptr->lchd!=NULL){
  1039. push_stack(ptr);
  1040. ptr=ptr->lchd;
  1041. }
  1042. while(ptr->rchd==NULL){
  1043. cout<<ptr->data<<endl;
  1044. if(stack_empty()){
  1045. return;
  1046. }
  1047. ptr=pop_stack();
  1048. }
  1049. cout<<ptr->data<<endl;
  1050. ptr=ptr->rchd;
  1051. }
  1052. cout<<endl;
  1053. }
  1054. void post(struct node *root){
  1055. struct node *ptr,*q;
  1056. ptr=root;
  1057. if(ptr==NULL){
  1058. cout<<"stack is empty"<<endl;
  1059. return;
  1060. }
  1061. q=root;
  1062. while(1){
  1063. while(ptr->lchd!=NULL){
  1064. push_stack(ptr);
  1065. ptr=ptr->lchd;
  1066. }
  1067. while(ptr->rchd==NULL||ptr->rchd==q){
  1068. cout<<ptr->data<<endl;
  1069. q=ptr;
  1070. if(stack_empty())
  1071. return;
  1072. ptr=pop_stack();
  1073. }
  1074. push_stack(ptr);
  1075. ptr=ptr->rchd;
  1076. }
  1077. cout<<endl;
  1078. }
  1079. struct node* search(struct node* root, int value){
  1080. if (root == NULL || root->data == value)
  1081. return root;
  1082. if (root->data<value)
  1083. return search(root->rchd,value);
  1084. if(root->data>value)
  1085. return search(root->lchd,value);
  1086. }
  1087. struct node* newNode(int item)
  1088. {
  1089. struct node *temp = (struct node *)malloc(sizeof(struct node));
  1090. temp->data = item;
  1091. temp->lchd = NULL;
  1092. temp->rchd = NULL;
  1093. return temp;
  1094. }
  1095. struct node* insert(struct node* node,int value){
  1096. if(node==NULL){
  1097. return newNode(value);
  1098. }
  1099. if(value<node->data){
  1100. node->lchd=insert(node->lchd,value);
  1101. }
  1102. else if(value>node->data){
  1103. node->rchd=insert(node->rchd,value);
  1104. }
  1105. return node;
  1106. }
  1107. struct node* search_min(struct node* root){
  1108. if(root->lchd==NULL){
  1109. return root;
  1110. }
  1111. else{
  1112. search_min(root->lchd);
  1113. }
  1114. }
  1115. struct node* search_max(struct node* root){
  1116. if(root->rchd==NULL){
  1117. return root;
  1118. }
  1119. else{
  1120. search_max(root->rchd);
  1121. }
  1122. }
  1123. bool search_tree(struct node* root, int value){
  1124. struct node *temp = (struct node *)malloc(sizeof(struct node));
  1125. temp=search(root,value);
  1126. cout<<"searching for "<<value<<" node."<<endl;
  1127. if(temp!=NULL){
  1128. cout<<"node is in binary tree."<<endl;
  1129. return true;
  1130. }
  1131. else{
  1132. cout<<"node is not in binary tree."<<endl;
  1133. return false;
  1134. }
  1135. }
  1136. int main(){
  1137. struct node *root=NULL;
  1138. int value;
  1139. root=insert(root,30);
  1140. insert(root,50);
  1141. insert(root,40);
  1142. insert(root,20);
  1143. insert(root,10);
  1144. insert(root,5);
  1145. insert(root,15);
  1146. insert(root,45);
  1147.  
  1148. cout<<"inorder"<<endl;
  1149. in(root);
  1150. cout<<"preorder"<<endl;
  1151. pre(root);
  1152. cout<<"postorder"<<endl;
  1153. post(root);
  1154. cout<<"Enter the data of node to be search -- ";
  1155. cin>>value;
  1156. search_tree(root,value);
  1157. cout<<"Enter the data of node to be search -- ";
  1158. cin>>value;
  1159. search_tree(root,value);
  1160. cout<<"Maximum node -- "<<search_max(root)->data<<endl;
  1161. cout<<"Minimum node -- "<<search_min(root)->data;
  1162. return 0;
  1163. }
  1164.  
  1165.  
  1166. // AVL TREE
  1167. #include<iostream>
  1168. #include<stdlib.h>
  1169. using namespace std;
  1170. struct Node{
  1171. int key;
  1172. struct Node *left;
  1173. struct Node *right;
  1174. int height;
  1175. };
  1176. int max(int a, int b);
  1177. int height(struct Node *N)
  1178. { if (N == NULL)
  1179. return 0;
  1180. return N->height;
  1181. }
  1182. int max(int a, int b){
  1183. return (a > b)? a : b;
  1184. }
  1185. struct Node* newNode(int key){
  1186. struct Node* node = new Node;
  1187. node->key = key;
  1188. node->left = NULL;
  1189. node->right = NULL;
  1190. node->height = 1;
  1191. return(node);
  1192. }
  1193. struct Node *rightRotate(struct Node *y){
  1194. struct Node *x = y->left;
  1195. struct Node *T2 = x->right;
  1196. x->right = y;
  1197. y->left = T2;
  1198. y->height = max(height(y->left), height(y->right))+1;
  1199. x->height = max(height(x->left), height(x->right))+1;
  1200. return x;
  1201. }
  1202. struct Node *leftRotate(struct Node *x){
  1203. struct Node *y = x->right;
  1204. struct Node *T2 = y->left;
  1205. y->left = x;
  1206. x->right = T2;
  1207. x->height = max(height(x->left), height(x->right))+1;
  1208. y->height = max(height(y->left), height(y->right))+1;
  1209. return y;
  1210. }
  1211. int getBalance(struct Node *N)
  1212. { if (N == NULL)
  1213. return 0;
  1214. return height(N->left) - height(N->right);
  1215. }
  1216. struct Node* insert(struct Node* node, int key)
  1217. {
  1218. /* 1. Perform the normal BST insertion */
  1219. if (node == NULL)
  1220. return(newNode(key));
  1221. if (key < node->key)
  1222. node->left = insert(node->left, key);
  1223. else if (key > node->key)
  1224. node->right = insert(node->right, key);
  1225. else // Equal keys are not allowed in BST
  1226. return node;
  1227. /* 2. Update height of this ancestor node */
  1228. node->height = 1 + max(height(node->left),
  1229. height(node->right));
  1230. /* 3. Get the balance factor of this ancestor
  1231. node to check whether this node became
  1232. unbalanced */
  1233. int balance = getBalance(node);
  1234. // If this node becomes unbalanced, then
  1235. // there are 4 cases
  1236. // Left Left Case
  1237. if (balance > 1 && key < node->left->key)
  1238. return rightRotate(node);
  1239. // Right Right Case
  1240. if (balance < -1 && key > node->right->key)
  1241. return leftRotate(node);
  1242. // Left Right Case
  1243. if (balance > 1 && key > node->left->key)
  1244. { node->left = leftRotate(node->left);
  1245. return rightRotate(node);
  1246. }
  1247. // Right Left Case
  1248. if (balance < -1 && key < node->right->key)
  1249. { node->right = rightRotate(node->right);
  1250. return leftRotate(node);
  1251. }
  1252. /* return the (unchanged) node pointer */
  1253. return node;
  1254. }
  1255. // A utility function to print preorder traversal
  1256. // of the tree.
  1257. // The function also prints height of every node
  1258. void preOrder(struct Node *root)
  1259. {
  1260. if(root != NULL)
  1261. {
  1262. printf("%d ", root->key);
  1263. preOrder(root->left);
  1264. preOrder(root->right);
  1265. }
  1266. }
  1267. /* Drier program to test above function*/
  1268. int main()
  1269. {
  1270. struct Node *root = NULL;
  1271. /* Constructing tree given in the above figure */
  1272. root = insert(root, 20);
  1273. root = insert(root, 10);
  1274. root = insert(root, 30);
  1275. root = insert(root, 50);
  1276. root = insert(root, 40);
  1277. root = insert(root, 25);
  1278. cout<<"Preorder traversal of AVL tree is \n";
  1279. preOrder(root);
  1280. return 0;
  1281. }
  1282.  
  1283.  
  1284. //SORTING PROGRAMS
  1285. Insertion Sort & Selection Sort ā€“
  1286. #include<iostream>
  1287. using namespace std;
  1288. #define Max 100
  1289. void insertion_sort(int *,int);
  1290. void selection_sort(int *,int);
  1291. int main(){
  1292. int arr[Max],n,ch;
  1293. cout<<"Enter the number of elements -- ";
  1294. cin>>n;
  1295. for(int i=0;i<n;i++){
  1296. cin>>arr[i];
  1297. }
  1298. cout<<"1.insertion_sort.\n"<<"2.selection_sort.\nā€<<"4.exit\n";
  1299. cin>>ch;
  1300. switch(ch){
  1301. case 1:
  1302. insertion_sort(arr,n);
  1303. break;
  1304. case 2:
  1305. selection_sort(arr,n);
  1306. break;
  1307. case 3:
  1308. exit(1);
  1309. }
  1310. cout<<"sorted list is --";
  1311. for(int i=0;i<n;i++){
  1312. cout<<*(arr+i)<<" ";
  1313. }
  1314. cout<<endl;
  1315. return 0;
  1316. }
  1317. void insertion_sort(int *arr,int n){
  1318. int k,j;
  1319. for(int i=1;i<n;i++){
  1320. k=*(arr+i);
  1321. for( j=i-1;j>=0&&(k<*(arr+j));j--)
  1322. *(arr+j+1)=*(arr+j);
  1323. *(arr+j+1)=k;
  1324. }
  1325. }
  1326. void selection_sort(int *arr,int n){
  1327. int min,temp;
  1328. for(int i=0;i<n-1;i++){
  1329. min =i;
  1330. for(int j=i+1;j<n;j++){
  1331. if(*(arr+min)>*(arr+j))
  1332. min=j;
  1333. }
  1334. if(i!=min){
  1335. temp=*(arr+i);
  1336. *(arr+i)=*(arr+min);
  1337. *(arr+min)=temp;
  1338. }
  1339. }
  1340. }
  1341. //Bubble Sort:
  1342. #include<iostream>
  1343. using namespace std;
  1344. int main()
  1345. {
  1346. int a[50],n,i,j,temp;
  1347. cout<<"Enter the size of array: ";
  1348. cin>>n;
  1349. cout<<"Enter the array elements: ";
  1350.  
  1351. for(i=0;i<n;++i)
  1352. cin>>a[i];
  1353.  
  1354. for(i=1;i<n;++i)
  1355. {
  1356. for(j=0;j<(n-i);++j)
  1357. if(a[j]>a[j+1])
  1358. {
  1359. temp=a[j];
  1360. a[j]=a[j+1];
  1361. a[j+1]=temp;
  1362. }
  1363. }
  1364.  
  1365. cout<<"Array after bubble sort:";
  1366. for(i=0;i<n;++i)
  1367. cout<<" "<<a[i];
  1368.  
  1369. return 0;
  1370. }
  1371.  
  1372.  
  1373. //Quick Sort:
  1374. #include <iostream>
  1375. using namespace std;
  1376. voidquick_sort(int[],int,int);
  1377. int partition(int[],int,int);
  1378. int main()
  1379. {
  1380. int a[50],n,i;
  1381. cout<<"How many elements?";
  1382. cin>>n;
  1383. cout<<"\nEnter array elements:";
  1384. for(i=0;i<n;i++)
  1385. cin>>a[i];
  1386. quick_sort(a,0,n-1);
  1387. cout<<"\nArray after sorting:";
  1388. for(i=0;i<n;i++)
  1389. cout<<a[i]<<" ";
  1390. return 0;
  1391. }
  1392. Void quick_sort(int a[],intl,int u)
  1393. { int j;
  1394. if(l<u)
  1395. { j=partition(a,l,u);
  1396. quick_sort(a,l,j-1);
  1397. quick_sort(a,j+1,u);
  1398. }
  1399. }
  1400. int partition(int a[],intl,int u)
  1401. {
  1402. intv,i,j,temp;
  1403. v=a[l];
  1404. i=l;
  1405. j=u+1;
  1406. do
  1407. {
  1408. do
  1409. i++;
  1410. while(a[i]<v&&i<=u);
  1411. do
  1412. j--;
  1413. while(v<a[j]);
  1414. if(i<j)
  1415. { temp=a[i];
  1416. a[i]=a[j];
  1417. a[j]=temp;
  1418. }
  1419. }while(i<j);
  1420. a[l]=a[j];
  1421. a[j]=v;
  1422. return(j);
  1423. }
  1424.  
  1425.  
  1426. //Merge Sort:
  1427. #include<iostream>
  1428. using namespace std;
  1429. void merge_sort(int [],int ,int );
  1430. void merge(int [],int,int ,int );
  1431. int main()
  1432. {
  1433. int n;
  1434. cout<<"Enter the size of the array"<<endl;
  1435. cin>>n;
  1436. int a[n];
  1437. cout<<"Enter the elements in the array"<<endl;
  1438. for(int i=1;i<=n;i++)
  1439. {
  1440. cin>>a[i];
  1441. }
  1442. cout<<"sorting using merge sort"<<endl;
  1443. int p=1,r=n;
  1444. merge_sort(a,p,r);
  1445. cout<<"sorted form"<<endl;
  1446. for(int i=1;i<=n;i++)
  1447. {
  1448. cout<<"a["<<i<<"]="<<a[i]<<endl;
  1449. }
  1450. return 0;
  1451. }
  1452. void merge_sort(int a[],int p,int r)
  1453. {
  1454. int q;
  1455. if(p<r)
  1456. {
  1457. q=(p+r)/2;
  1458. merge_sort(a,p,q);
  1459. merge_sort(a,q+1,r);
  1460. merge(a,p,q,r);
  1461. }
  1462. }
  1463. void merge(int a[],int p,int q,int r)
  1464. {
  1465. cout<<"Entered merge"<<endl;
  1466. int n1=q-p+1;
  1467. int n2=r-q;
  1468. int L[n1+1];
  1469. int R[n2+1];
  1470. for(int i=1;i<=n1;i++)
  1471. {
  1472. L[i]=a[p+i-1];
  1473. }
  1474. for(int j=1;j<=n2;j++)
  1475. {
  1476. R[j]=a[q+j];
  1477. }
  1478. L[n1+1]=999;
  1479. R[n2+1]=999;
  1480. int i=1, j=1;
  1481. for(int k=p;k<=r;k++)
  1482. {
  1483. if(L[i]<=R[j])
  1484. {
  1485. a[k]=L[i];
  1486. i=i+1;
  1487. }
  1488. else
  1489. {
  1490. a[k]=R[j];
  1491. j=j+1;
  1492. }
  1493. }
  1494. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement