Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 26

1. program to show stack operations.

#include <stdio.h> #include <conio.h> void push(int st[],int data,int &top); void disp(int st[],int &top); int pop(int st[],int &top); int flg=0; int top=-1,tos=-1; int st[50]; void push(int st[],int data,int &top) { if(top==50-1) flg=0; else { flg=1; top++; st[top]=data; } } int pop(int st[],int &top) { int pe; if(top==-1) { pe=0; flg=0; } else { flg=1; pe=st[top]; top--; } return(pe); } void disp(int st[],int &top) { int i; if(top==-1) { printf("\nStack is Empty");

} else { for(i=top;i>=0;i--) printf("\t%d",st[i]); } } void main() { int dt,opt; int q=0; clrscr(); printf("This Program Is Used to Perform PUSH & POP operations On Stack"); printf("\n\n\tMain Menu........."); printf("\n\n1.Push"); printf("\n\n2.Pop"); printf("\n\n3.Exit"); do { printf("\n\n\tEnter Your Choice 1-3:"); scanf("%d",&opt); switch(opt) { case 1: printf("\nEnter the Element to be Push:"); scanf("%d",&dt); push(st,dt,tos); if(flg==1) { printf("\nAfter Inserting the Element, Stack is:\n\n"); disp(st,tos); if(tos==50-1)

printf("\nStack is Now Full"); }

else printf("\nStack Overflow Insertion Not Possible"); break; case 2: dt=pop(st,tos); if(flg==1)

{ printf("\n\tData Deleted From the Stack is:%d\n",dt); printf("\n\tAfter Deleting the Element from the stack is:\n\n"); disp(st,tos); } else printf("\nStack Empty,Deletio Not Possible:"); break; case 3: q=1; break; default:printf("\nWrong Choice Enter 1-3 Only"); } }while(q!=1); }

OUTPUT Main Menu......... 1.push 2.pop 3.exit Enter your choice 1-3:1 Enter the element to be push:4 After inserting the elements,stack is: 4 Enter your choice 1-3:1 Enter the element to be push:7 After inserting the elements,stack is: 74 Enter your choice 1-3:1 Enter the element to be push:4

2. program to show queue operations.


#include <stdio.h> #include <conio.h> # define MAX 5 int queue_arr[MAX]; int rear = -1; int front = -1; main() { int choice; while(1) { printf("1.Insert\n"); printf("2.Delete\n"); printf("3.Display\n"); printf("4.Quit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1 : insert(); break; case 2 : del(); break; case 3: display(); break; case 4: exit(1); default: printf("Wrong choice\n"); }/*End of switch*/ }/*End of while*/ }/*End of main()*/ insert() { int added_item; if (rear==MAX-1)

printf("Queue Overflow\n"); else { if (front==-1) /*If queue is initially empty */ front=0; printf("Input the element for adding in queue : "); scanf("%d", &added_item); rear=rear+1; queue_arr[rear] = added_item ; } }/*End of insert()*/ del() { if (front == -1 || front > rear) { printf("Queue Underflow\n"); return ; } else { printf("Element deleted from queue is : %d\n", queue_arr[front]); front=front+1; } }/*End of del() */ display() { int i; if (front == -1) printf("Queue is empty\n"); else { printf("Queue is :\n"); for(i=front;i<= rear;i++) printf("%d ",queue_arr[i]); printf("\n"); } }/*End of display() */

program to display binary tree


#include<stdio.h> #include<conio.h>

struct node { int info; struct node *right; struct node *left; }; struct node * Create_Tree(char *List,int lower,int upper) { struct node *Node; int mid=(lower+upper)/2; Node=(struct node*)malloc(sizeof(struct node)); Node->info=List[mid]; if(lower>=upper) { Node->left=NULL; Node->right=NULL; return(Node); } /*Test for the Left Child*/ if(lower<=mid-1) { Node->left=Create_Tree(List,lower,mid- } else { Node->left=NULL; } if(upper>=mid+1) { Node->right=Create_Tree(List,mid+1,upp } else { Node->right=NULL; }

return(Node); } /* Function Closed */ void display(struct node *T,int Level) { int i; if(T) { display(T->right,Level+1); printf("\n"); for(i=0;i<Level;i++) printf(" "); printf("%d",T->info); printf("\n"); display(T->left,Level+1); } } /* Functions to Read the Tree in Particular Order */ pre_order(struct node *Node) { if(Node) { printf(" %d",Node->info); pre_order(Node->left); pre_order(Node->right); } } in_order(struct node *Node) { if(Node) { pre_order(Node->left); printf(" %d",Node->info); /* Root */ pre_order(Node->right); } } post_order(struct node *Node) { if(Node) { pre_order(Node->left);

pre_order(Node->right); printf(" %d",Node->info); /* Root */ } } /* Function main */ void main() { int info; int number=0; char ans='y'; char List[10]; struct node *T=(struct node*)malloc(sizeof(struct node)); T=NULL; clrscr(); while(ans=='y') { printf("Type the Data for the Node :"); scanf("%d",&info); List[number]=info; number++; fflush(stdin); printf("Want to Continue .... (y/n) ?:"); scanf("%c",&ans); } /* Create and Display the Tree */ number--; T=Create_Tree(List,0,number); display(T,1); printf("\n Pre Order Read "); pre_order(T); printf("\nIn Order Read "); in_order(T); printf("\n Post Order Read "); post_order(T); } /* main */

program to show array elements using quick sort


#include<stdio.h> #include<conio.h> int n,i=1; void display(int a[]) { int i; printf(\n); for(i=0;i<n;i++) printf(\t%d,a[i]); } int partition(int a[],int lb,int ub) { int up,down; int temp,pivot; up=ub; down=lb+1; pivot=a[lb]; do { while((a[down]<pivot) && (down<=ub)) down++; while((a[up]>pivot) && (up>lb)) up; if(down<up) { temp=a[down]; a[down]=a[up]; a[up]=temp; } display(a); }while(down<up); //interchange pivot and a[up] a[lb]=a[up]; a[up]=pivot; display(a); return up; } void quicksort(int a[],int lb,int ub) { int j; if(lb<ub) { printf(\nIteration %d\n,i); display(a); i++; j=partition(a,lb,ub); quicksort(a,lb,j-1); quicksort(a,j+1,ub); }

} void main() { int a[20],i; clrscr(); printf(\nEnter number of elements: ); scanf(%d,&n); printf(\nEnter the unsorted numbers: ); for(i=0;i<n;i++) scanf(%d,&a[i]); quicksort(a,0,n-1); printf(\nThe sorted list: ); display(a); getch(); }

program to show array elements using shell sort


Shell sorting was first introduced by Donald Shell. It generalized as exchanging sort, such as bubble or insertion sorting, by allowing the comparison and exchange of elements that lie far apart. The running time of Shell sort is heavily dependent on the gap sequence it uses. For many practical variants, determining their time complexity remains an open problem. Steps for solution of shell sorting: step.1: Set up a inc number. inc number is set according to given elements in the list. step.2: mark each elements which is comes in inc element. For example, if list contains 10 elements then and we assume inc is 3 then, marking of elements such as next marking element, add last element +3 to get next element and so on. Then marking element would be 1st element, 4th element(add 3), 7th element(add 3), 10th element. 89 46 99 12 33 14 69 41 33 28 1 2 3 4 5 6 7 8 9 10 [index number]

step.3: sort marking elements such as smallest to greater is set as left to right and not change remain element. For example, we apply this step in above example: 12 46 99 28 33 14 69 41 33 89 step.4: reduce inc number to one i.e. if inc number is earlier is 3 then now it would be 3-1 = 2. step.5: Repeat step 2,3 and 4 till all the elements not sorted. Let's understand shell sorting using example: 35 12 14 9 15 45 32 95 40 5 //assume inc=3 //Now marking 1st element, 1+3=4th element, //4+3=7th element, 7+3=10th element 35 12 14 9 15 45 32 95 40 5 //step-3 i.e. sorting of marked elements 5 12 14 9 15 45 32 95 40 35 //now inc=3-1=2 //new marking is 1st element, 1+2=3th element, //3+2=5 element, 5+2=7th element, 7+2=9th //element 5 12 14 9 15 45 32 95 40 35

//now sorting of marked elements 5 12 14 9 15 45 32 95 40 35 //Now inc=2-1=1 //Now every elements all marked because inc=1 5 12 14 9 15 45 32 95 40 35 //sorting of marked elements 5 9 12 14 15 32 35 40 45 95 /*c program for sorting array using shell sorting method*/ #include<stdio.h> #include<conio.h> int main() { int arr[30]; int i,j,k,tmp,num; printf("Enter total no. of elements : "); scanf("%d", &num); for(k=0; k<num; k++) { printf("\nEnter %d number : ",k+1); scanf("%d",&arr[k]); } for(i=num/2; i>0; i=i/2) { for(j=i; j<num; j++) { for(k=j-i; k>=0; k=k-i) { if(arr[k+i]>=arr[k]) break; else { tmp=arr[k]; arr[k]=arr[k+i]; arr[k+i]=tmp; } } } } printf("\t**** Shell Sorting ****\n"); for(k=0; k<num; k++) printf("%d\t",arr[k]); getch(); return 0; } OUTPUT Enter total no. of elements : 7 Enter 1 number : 8 Enter 2 number : 3 Enter 3 number : 7

Enter 4 number : 9 Enter 5 number : 1 Enter 6 number : 24 Enter 7 number : 2 **** Shell Sorting **** 1 2 3 7 8 9 24

program to display link list operations


/*OPERATIONS ON SINGLY LINKED LIST*/ #include<stdio.h> #include<conio.h> struct link { int item; struct link *next; }; typedef struct link node; void addfirst(); void addlast(); void addmid(); void delfirst(); void dellast(); void delmid(); void display(); node *head=NULL; void main() { int ch; clrscr(); do { printf("\nSINGLY LINKED LIST OPERATIONS\n"); printf("\n1.Addfirst\n2.AddMid\n3.AddLast\n4.DeleteFirst\n5.DeleteMiddle\n6.DeleteLast\n7.D isplay\n8.Exit\n"); printf("Enter your option:\t"); scanf("%d",&ch); switch(ch) { case 1: addfirst(); display(); break; case 2: addmid(); display(); break; case 3: addlast(); display(); break; case 4: delfirst(); display(); break;

case 5: delmid(); display(); break; case 6: dellast(); display(); break; case 7: display(); break; case 8: exit(0); break; default: printf("Invalid Choice\n"); } } while(ch<=8); getch(); } void addfirst() { node *temp; temp=(node *)malloc(sizeof(node)); printf("Enter the data....\t"); scanf("%d",&temp->item); temp->next=head; head=temp; } void addmid() { int i=1,pos; node *cur=head,*temp; printf("\nEnter the position\t"); scanf("%d",&pos); while(pos!=i+1&&cur!=NULL) { cur=cur->next; i++; } if(pos==i+1) { temp=(node *)malloc(sizeof(node)); printf("Enter the data..."); scanf("%d",&temp->item); temp->next=cur->next; cur->next=temp;

} } void addlast() { node *temp,*cur=head; temp=(node *)malloc(sizeof(node)); printf("\nEnter the data...."); scanf("%d",&temp->item); while(cur->next!=NULL) { cur=cur->next; } temp->next=cur->next; cur->next=temp; } void delfirst() { node *temp=head; head=head->next; printf("Deleted item is %d\n",temp->item); free(temp); } void delmid() { int i=1,pos; node *cur=head,*temp; printf("Enter the position to be deleted\t"); scanf("%d",&pos); while(pos!=i+1&&cur->next!=NULL) { cur=cur->next; i++; } if(pos==i+1) { temp=cur->next; cur->next=temp->next; printf("Deleted item is %d\n",temp->item); free(temp); } } void dellast() { node *temp,*cur=head; while(cur->next->next!=NULL)

{ cur=cur->next; } temp=cur->next; cur->next=NULL; printf("Deleted item is %d\n",temp->item); free(temp); } void display() { node *cur=head; printf("\nHead->"); while(cur!=NULL) { printf("\t%d",cur->item); cur=cur->next; } printf("<-NULL\n"); }

program to show array elements using shell insertion sort


#include <string.h> #include <stdio.h> #include <stdlib.h> void shell_sort(char *chars, int c) { register int i, j, space, k; char x, a[5]; a[0]=9; a[1]=5; a[2]=3; a[3]=2; a[4]=1; for(k=0; k < 5; k++) { space = a[k]; for(i=space; i < c; ++i) { x = chars[i]; for(j=i-space; (x < chars[j]) && (j >= 0); j=j-space) chars[j+space] = chars[j]; chars[j+space] = x; } } } int main() { char string[300]; printf("Enter a string:"); gets(string); shell_sort(string, strlen(string)); printf("The sorted string is: %s.\n", string); return 0; }

program to show array elements using selection sort.


#include<stdio.h> #include<conio.h> main() { int a[100], i, j, t, min, n; printf("\nSelection Sort Program in C\n"); printf("\nEnter the number of values\n"); scanf("%d",&n); printf("\nEnter the values:\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=1;i<=n;i++) { min=i; for(j=i+1;j<=n;j++) { if (a[min] > a[j]) min=j; } if(min!=i) { t=a[i]; a[i]=a[min]; a[min]=t; } } printf("\nSorted list in ascending order:\n"); for(i=1;i<=n;i++) printf("%d\n",a[i]); getch(); }

program to search an array elements.(using binary search)


#include<stdio.h> main() { int a[20],i,j,d,t,x,l=0,low,mid,high; printf("\nEnter the number of elements\n"); scanf("%d",&d); printf("Enter the numbers\n"); for(i=0;i<d;i++) scanf("%d",&a[i]); for(i=0;i<d;i++) { for(j=i+1;j<d;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } printf("\nThe sorted list :"); for(i=0;i<d;i++) printf("%d ",a[i]); printf("\nEnter the number to be searched\n"); scanf("%d",&x); low=0; high=d-1; while(low<=high) { mid=(low+high)/2; if(x<a[mid]) high=mid-1; else if(x>a[mid]) low=mid+1; else { if(x==a[mid]) { l++; printf("The item %d is found at location %d\n",x,mid+1); exit(0); } } } if(l==0) { printf("Item not found\n"); }

Program to insert an element into array


#include<stdio.h> #include<conio.h> void main() { int a[20],i,j,n; clrscr(); printf("Enter the no of element you want to insert in the array : "); scanf("%d",&n); printf(" Enter the Array element : "); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("The array is : "); for(i=0;i<n;i++) { printf("%d",a[i]); } printf(" Enter the element which you want to insert"); scanf("%d",&j); printf("The array is : "); for(i=0;i<n+1;i++) { if(i==n) a[i]=j; printf("%d",a[i]); } getch(); }

program to delete an element from array.


1. #include <iostream> 2. // Returns true if text contains ch 3. inline bool contains(char *text, char ch) { 4. while (*text) { 5. if (*text++ == ch) { 6. return true; 7. } 8. } 9. return false; 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. } int main() { char arr[] = "abc \n \t\ndef"; char spaces[] = " \t\n"; // Create another array the same length as arr char temp[sizeof arr]; // Index of where to insert character in new array int temp_index = 0; // Loop through each character until null-terminator is reached for (int i = 0; arr[i]; ++i) { if (!contains(spaces, arr[i])) { // Character isn't in the spaces array temp[temp_index++] = arr[i]; } } // Add null-terminator temp[temp_index] = '\0'; // Replace old array with new one

28. 29. 30. 31. 32. 33. 34. 35.

strcpy_s(arr, sizeof(arr), temp);

// Display the array std::cout << arr; // Outputs "abcdef" // Pause std::cin.ignore(); return 0; }

5 SQL commands
Create a Database CREATE DATABASE database_name Unless the database already exists, it will be created. Delete a Database DROP DATABASE database_name You almost never want to do this, at least not without some deep thought. Create a table CREATE TABLE if not exists database_name.table_name (fieldname1 type1, fieldname2, type2 ...) You need to specify what fields the table has, and what kind of data they hold. Here's an example: CREATE TABLE if not exists db.people (First text, Last text, Age int) There's one more refinement when creating tables including a primary key. An autoincrementing primary key assures that adding a new record to an existing table won't erase a prior record. Like this: CREATE TABLE if not exists db.people (First text, Last text, Age int, pk int not null auto_increment primary key) The field "pk" (any name will do) provides some important properties to the table each record is now unique, and the key also allows the database engine to operate more efficiently. When you insert data into a table that has a primary key, you don't normally specify the key in your entry. The database engine takes care of that it automatically creates the key field and gives it a unique value. The use of the phrase "if not exists" is meant to avoid trying to create a table that already exists, which would produce an error. To replace an existing table with a new design, first delete the prior table. Delete a table DROP TABLE database_name.table_name Again, as before, this can be misused or applied accidentally. Suppose you want to keep the table's structure (the field definitions) but erase the data in the table. Here's how: Truncate a table

TRUNCATE TABLE database_name.table_name All data is deleted, but the table's basic structure remains. Drop (delete) a field (column ) from a table ALTER TABLE database_name.table_name DROP COLUMN fieldname This deletes the field and associated data. (The term "column" is often used as a synonym for "field".) Delete a record from a table DELETE FROM database_name.table_name WHERE (record identifiers) This deletes a specific record from a table. Be careful to specify which record unambiguously to avoid deleting more records than you intend. If each record has a unique primary key, use that. Otherwise, list the values of each field for the intended record, like this: DELETE FROM database_name.table_name WHERE Name = 'John Doe' and Address = '123 Elm Street' and ... (so forth) Retrieve specific records and fields from a table SELECT * FROM db.people The above means "provide all the records and fields in the table 'db.people'". But we might want to specify the retrieval of particular fields, and we might also want to specify which records to retrieve. Like this: SELECT Last,Age FROM db.people WHERE AGE > 60 The field list "Last,Age" specifies which fields to include in the result, and the "WHERE AGE > 60" part specifies which records to retrieve. Insert records into a table INSERT INTO db.people (First,Last,Age) VALUES("John","Doe",31) The above creates a new record in the table "db.people". Notice that a specifier for the primary key field isn't included. That's intentional it lets the database engine manage the key. Here's an INSERT example that reads from one table and writes to another: INSERT INTO db_name.dest_table (First,Last,Age) SELECT First,Last,Age from db_name.source_table The above method can be used to accumulate data of various kinds: INSERT INTO db_name.geriatric_table (First,Last,Age) SELECT First,Last,Age from db_name.source_table WHERE Age > 65

INSERT INTO db_name.teens_table (First,Last,Age) SELECT First,Last,Age from db_name.source_table WHERE Age < 19 I emphasize that such selective queries can be as complex as one wishes: INSERT INTO db_name.dest_table (First,Last,Age) SELECT First,Last,Age from db_name.source_table WHERE (Age < 18 and IQ > 120) OR (AGE >= 22 AND HairColor = "UnforeseeableFuchsia") Copy a table CREATE TABLE database_name.copy_name LIKE database_name.original_name INSERT database_name.copy_name SELECT * FROM database_name.original_name There are a number of ways to copy a table, but most of them are incomplete. The first line above copies the original table's structure, and the second line copies the data, leaving nothing out. Other copying methods may not copy every detail.

You might also like