Professional Documents
Culture Documents
Program of Data Structure in C
Program of Data Structure in C
#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)
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
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() */
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 */
} 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(); }
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
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"); }
// 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.