diff --git a/C++/matrix_addition.cpp b/C++/matrix_addition.cpp new file mode 100644 index 00000000..fca9b6be --- /dev/null +++ b/C++/matrix_addition.cpp @@ -0,0 +1,69 @@ +#include +using namespace std; + +class Matrix{ + public: + int mat[2][2]; + + Matrix() + { + mat[0][0]=1; + mat[0][1]=1; + mat[1][0]=1; + mat[1][1]=1; + } + + Matrix(int a, int b, int c, int d) + { + mat[0][0]=a; + mat[0][1]=b; + mat[1][0]=c; + mat[1][1]=d; + } + + Matrix addmat(Matrix s ,Matrix t) + { + Matrix r; + r.mat[0][0]=s.mat[0][0]+t.mat[0][0]; + r.mat[0][1]=s.mat[0][1]+t.mat[0][1]; + r.mat[1][0]=s.mat[1][0]+t.mat[1][0]; + r.mat[1][1]=s.mat[1][1]+t.mat[1][1]; + return r; + } + + void display() + { + cout< +#include +using namespace std; + +class patient{ + public: + string patientID; + string PName; + + void getPatientInfo(); + void putPatientInfo(); + void makeAppointment(); + void Diagnosis(); + void Billing(); + void printBill(); + + private: + string Address; + string Gender; + int Phone; + string BloodGroup; + string doctor_Name; + string app_date; + string DiagnosisInfo; + string MedicineInfo; + float doctorFee; + float medicine_charge; + float Total; + }; + +void patient :: getPatientInfo() +{ + cout<>patientID; + cout<<"Kindly enter patient name: "; + cin>>PName; + cout<<"Kindly enter patient address: "; + cin>>Address; + cout<<"Kindly enter patient gender: "; + cin>>Gender; + cout<<"Kindly enter patient phone number: "; + cin>>Phone; + cout<<"Kindly enter patient blood group: "; + cin>>BloodGroup; +} + +void patient :: putPatientInfo() +{ + cout<>doctor_Name; + cout<<"Kindly enter Appointment Date: "; + cin>>app_date; +} + +void patient :: Diagnosis() +{ + cout<>DiagnosisInfo; + cout<<"Kindly enter Medicine Info: "; + cin>>MedicineInfo; +} + +void patient :: Billing() +{ + cout<>doctorFee; + cout<<"Kindly enter Medicine Charges: "; + cin>>medicine_charge; + Total=doctorFee+medicine_charge; +} + +void patient :: printBill() +{ + cout<>choice; + + switch(choice) { + + case 1: + obj.getPatientInfo(); + break; + + case 2: + obj.putPatientInfo(); + break; + + case 3: + obj.makeAppointment(); + break; + + case 4: + obj.Diagnosis(); + break; + + case 5: + obj.Billing(); + break; + + case 6: + obj.printBill(); + break; + + case 7: + cout< +#include +#include +#include +using namespace std; + +class str { + public: + char s[25]; + str() { }; + + str(char st[]) + { + strcpy(this->s,st ); + } + + str operator +(str const &obj) + { + str s3; + strcpy(s3.s,this->s); + strcat(s3.s,obj.s); + return s3; + } + + str operator =(str const &obj) + { + strcpy(this->s,obj.s); + return s; + } + + str operator -(str const &obj) + { + int i, j = 0, k = 0,n = 0; + int flag = 0; + str s3; + strcpy(s3.s,s); + + char neww[25]; + for(i = 0 ; s3.s[i] != '\0' ; i++) + { + k = i; + while(s3.s[i] == obj.s[j]) + { + i++,j++; + if(j == strlen(obj.s)) + { + flag = 1; + break; + } + } + j = 0; + + if(flag == 0) + i = k; + else + flag = 0; + + s3.s[n++] = s[i]; + } + + s3.s[n] = '\0'; + return s3; + + } + + str operator <=(str const &obj) + { + str s3; + strcpy(s3.s,this->s); + if(strcmp(s3.s,obj.s)==0) + cout<<"Same"; + else + cout<<"Different"; + return s3; + } + + void operator ++() + { + for(int i = 0;is);i++) + this->s[i]=toupper(this->s[i]); + } + + void operator --() + { + for(int i = 0;is);i++) + this->s[i]=tolower(this->s[i]); + } + + + + + + int length(str const &obj) + { + str s3; + int len; + strcpy(s3.s,this->s); + len=strlen(this->s); + cout<<"Length of string '"<>ch; + + switch(ch) { + case 1: + cout<<"String 1: "<>userid; + cout<<"Kindly enter your password: "; + cin>>password; + cout<<"Kindly enter your phone number: "; + cin>>phone; + cout<= 6)) + cout< +using namespace std; + +class scalar{ + public: + int x[3]; + + scalar() + { + x[0]=1; + x[1]=1; + x[2]=1; + } + + scalar(int a) + { + x[0]=a; + x[1]=1; + x[2]=1; + } + + scalar(int a, int b) + { + x[0]=a; + x[1]=b; + x[2]=1; + } + + scalar(int a, int b, int c) + { + x[0]=a; + x[1]=b; + x[2]=c; + } + + scalar multiplyscalar(int s ,scalar t) + { + scalar r; + r.x[0]=t.x[0]*s; + r.x[1]=t.x[1]*s; + r.x[2]=t.x[2]*s; + return r; + } + + void display() + { + cout< +using namespace std; + +class vector{ + public: + int x[3]; + + vector() + { + x[0]=0; + x[1]=0; + x[2]=0; + } + + vector(int a) + { + x[0]=a; + x[1]=0; + x[2]=0; + } + + vector(int a, int b) + { + x[0]=a; + x[1]=b; + x[2]=0; + } + + vector(int a, int b, int c) + { + x[0]=a; + x[1]=b; + x[2]=c; + } + + vector addvector(vector s,vector t) + { + vector r; + r.x[0]=s.x[0]+t.x[0]; + r.x[1]=s.x[1]+t.x[1]; + r.x[2]=s.x[2]+t.x[2]; + return r; + } + + void display() + { + cout< +#include +#include +int array[1000000]; + +float timedifference_msec(struct timeval t0, struct timeval t1) +{ + return (t1.tv_sec - t0.tv_sec) * 1000.0f + (t1.tv_usec - t0.tv_usec) / 1000.0f; +} + +int main() +{ + struct timeval t0; + struct timeval t1; + float elapsed; + + int i,key,ch; + long size,limit; + printf("Enter number of Elements: "); + scanf("%ld",&size); + int array[size]; + printf("Enter Upper limit in place values: "); + scanf("%ld",&limit); + + int num=size; + int j,x,y,temp; + srand( (unsigned) time(NULL) * getpid()); + gettimeofday(&t0, NULL); + if(array != NULL) + { + for(j = 0; j < num; j++) + { + array[j] = rand()%limit; + } + } + gettimeofday(&t1, NULL); + elapsed = timedifference_msec(t0, t1); + printf("\n"); + printf("List of Numbers:\n"); + for(j = 0; j < num; j++) + { + printf("%d\n",array[j]); + } + printf("Random Generation code executed in %f milliseconds.\n", elapsed); + printf("\n"); + gettimeofday(&t0, NULL); + for(x = 0; x < num - 1; x++) + { + for(y = 0; y < num - x - 1; y++) + { + if(array[y] > array[y + 1]) + { + temp = array[y]; + array[y] = array[y + 1]; + array[y + 1] = temp; + } + } + } + gettimeofday(&t1, NULL); + elapsed = timedifference_msec(t0, t1); + printf("Bubble Sorted List of Numbers:\n"); + for(j = 0; j < num; j++) + { + printf("%d\n",array[j]); + } + printf("Bubble Sort Code executed in %f milliseconds.\n", elapsed); +} diff --git a/C/DFS&BFS_graphs_traversal.c b/C/DFS&BFS_graphs_traversal.c new file mode 100644 index 00000000..36ddd972 --- /dev/null +++ b/C/DFS&BFS_graphs_traversal.c @@ -0,0 +1,103 @@ +/*DFS and BFS Graph Traversal*/ +#include +#define V 7 +int adjMatrix[V][V],visited[V],queue[100]; +int front=0,rear=-1; + +void createMx(int matrix[][V]) +{ + int i,j; + for(i=0;i +#include +#include +int array[1000000]; + +float timedifference_msec(struct timeval t0, struct timeval t1) +{ + return (t1.tv_sec - t0.tv_sec) * 1000.0f + (t1.tv_usec - t0.tv_usec) / 1000.0f; +} + +int main() +{ + struct timeval t0; + struct timeval t1; + float elapsed; + + int i,key,ch,position; + long size,limit; + printf("Enter number of Elements: "); + scanf("%ld",&size); + int array[size]; + printf("Enter Upper limit in place values: "); + scanf("%ld",&limit); + + int num=size; + int j,x,y,temp; + srand( (unsigned) time(NULL) * getpid()); + gettimeofday(&t0, NULL); + if(array != NULL) + { + for(j = 0; j < num; j++) + { + array[j] = rand()%limit; + } + } + gettimeofday(&t1, NULL); + elapsed = timedifference_msec(t0, t1); + printf("\n"); + printf("List of Numbers:\n"); + for(j = 0; j < num; j++) + { + printf("%d\n",array[j]); + } + printf("Random Generation code executed in %f milliseconds.\n", elapsed); + printf("\n"); + + gettimeofday(&t0, NULL); + for (i=1;i=0 && array[j]>position) + { + array[j+1] = array[j]; + j = j - 1; + } + array[j + 1] = position; + } + gettimeofday(&t1, NULL); + elapsed = timedifference_msec(t0, t1); + printf("Insertion Sorted List of Numbers:\n"); + for(j = 0; j < num; j++) + { + printf("%d\n",array[j]); + } + printf("Insertion Sort Code executed in %f milliseconds.\n", elapsed); +} diff --git a/C/SelectionSort.c b/C/SelectionSort.c new file mode 100644 index 00000000..9351b154 --- /dev/null +++ b/C/SelectionSort.c @@ -0,0 +1,75 @@ +// Program - Selection Sort + +#include +#include +#include +int array[1000000]; + +float timedifference_msec(struct timeval t0, struct timeval t1) +{ + return (t1.tv_sec - t0.tv_sec) * 1000.0f + (t1.tv_usec - t0.tv_usec) / 1000.0f; +} + +int main() +{ + struct timeval t0; + struct timeval t1; + float elapsed; + + int i,key,ch,position; + long size,limit; + printf("Enter number of Elements: "); + scanf("%ld",&size); + int array[size]; + printf("Enter Upper limit in place values: "); + scanf("%ld",&limit); + + int num=size; + int j,x,y,temp; + srand( (unsigned) time(NULL) * getpid()); + gettimeofday(&t0, NULL); + if(array != NULL) + { + for(j = 0; j < num; j++) + { + array[j] = rand()%limit; + } + } + gettimeofday(&t1, NULL); + elapsed = timedifference_msec(t0, t1); + printf("\n"); + printf("List of Numbers:\n"); + for(j = 0; j < num; j++) + { + printf("%d\n",array[j]); + } + printf("Random Generation code executed in %f milliseconds.\n", elapsed); + printf("\n"); + + gettimeofday(&t0, NULL); + for(x = 0; x < num - 1; x++) + { + position=x; + for(y = x + 1; y < num; y++) + { + if(array[position] > array[y]) + { + position=y; + } + } + if(position != x) + { + temp=array[x]; + array[x]=array[position]; + array[position]=temp; + } + } + gettimeofday(&t1, NULL); + elapsed = timedifference_msec(t0, t1); + printf("Selection Sorted List of Numbers:\n"); + for(j = 0; j < num; j++) + { + printf("%d\n",array[j]); + } + printf("Selection Sort Code executed in %f milliseconds.\n", elapsed); +} diff --git a/C/binarytrees_traversal.c b/C/binarytrees_traversal.c new file mode 100644 index 00000000..3bc34838 --- /dev/null +++ b/C/binarytrees_traversal.c @@ -0,0 +1,67 @@ +/*pre-order, in-order, post-order traversals on binary trees*/ +#include +#include + +struct node { + int data; + struct node *left; + struct node *right; + }; + +struct node* newnode(int value) { + struct node *new = (struct node*)malloc(sizeof(struct node)); + new->data=value; + new->left=NULL; + new->right=NULL; + return(new); +} + +void preorder(struct node *root) +{ + if(root==NULL) + return; + printf("%d ",root->data); + preorder(root->left); + preorder(root->right); +} + +void inorder(struct node *root) +{ + if(root==NULL) + return; + inorder(root->left); + printf("%d ",root->data); + inorder(root->right); +} + +void postorder(struct node *root) +{ + if(root==NULL) + return; + postorder(root->left); + postorder(root->right); + printf("%d ",root->data); +} + + +void main() +{ + struct node*root; + root=newnode(1); + root->left = newnode(2); + root->right = newnode(3); + root->left->left = newnode(4); + root->left->right = newnode(5); + root->right->left = newnode(6); + root->right->right = newnode(7); + + printf("Pre order traversal: \n"); + preorder(root); + printf("\n"); + printf("\nIn order traversal: \n"); + inorder(root); + printf("\n"); + printf("\nPost order traversal: \n"); + postorder(root); + printf("\n"); +} diff --git a/C/infixtopostfix.c b/C/infixtopostfix.c new file mode 100644 index 00000000..5f5e4412 --- /dev/null +++ b/C/infixtopostfix.c @@ -0,0 +1,177 @@ +/*Infix to Postfix Conversion using array*/ +#include +#include +#include +#include + +char stack[100]; +int top=-1; + +int isEmpty() +{ + return top == -1; +} +int isFull() +{ + return top == 100-1; +} + +char peek() +{ + return stack[top]; +} + +char pop() +{ + if(isEmpty()) + return -1; + + char ch=stack[top]; + top--; + return(ch); +} + +void push(char operator) +{ + if(isFull()) + printf("\nStack overflow"); + + else + { + top++; + stack[top] = operator; + } +} + +int is_operator(char symbol) +{ + if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-') + return 1; + + else + return 0; +} + +int precedence(char symbol) +{ + if(symbol == '^') + return(3); + + else if(symbol == '*' || symbol == '/') + return(2); + + else if(symbol == '+' || symbol == '-') + return(1); + + else + return(0); +} + +void InfixToPostfix(char infix_exp[], char postfix_exp[]) +{ + int i,j; + char x,item; + + push('('); + strcat(infix_exp,")"); + + i=0;j=0; + item=infix_exp[i]; + + while(item != '\0') + { + if(item == '(') + { + push(item); + } + else if( isdigit(item) || isalpha(item)) + { + postfix_exp[j] = item; + j++; + } + else if(is_operator(item) == 1) + { + x=pop(); + while(is_operator(x) == 1 && precedence(x)>= precedence(item)) + { + postfix_exp[j] = x; + j++; + x = pop(); + } + push(x); + push(item); + } + else if(item == ')') + { + x = pop(); + while(x != '(') + { + postfix_exp[j] = x; + j++; + x = pop(); + } + } + + else + { printf("\nInvalid infix Expression.\n"); + getchar(); + exit(1); + } + i++; + + item = infix_exp[i]; + } + + if(top>100) + { + printf("\nInvalid infix Expression.\n"); + getchar(); + exit(1); + } + + postfix_exp[j] = '\0'; +} + +void main() +{ + + + while(1) + { + char infix[100], postfix[100]; + int ch; + printf("\nMENU"); + printf("\n1.Enter Infix Expression\t\t2. Convert Infix to Postfix\t\t3.Exit\t"); + printf("\nEnter your choice: "); + scanf("%d",&ch); + + switch(ch) + { + case 1: + { + printf("Enter Infix expression : "); + scanf("%s",infix); + break; + } + case 2: + { + InfixToPostfix(infix,postfix); + printf("Postfix Expression: "); + printf("%s \n",postfix); + break; + } + + case 3: + { + printf("Succesfully exiting program \n"); + exit(0); + } + + default: + { + printf("Error,wrong choice input\n"); + break; + } + } + } +} diff --git a/C/linearsearch.c b/C/linearsearch.c new file mode 100644 index 00000000..430b91b5 --- /dev/null +++ b/C/linearsearch.c @@ -0,0 +1,104 @@ +/*Linear Search*/ +#include +#include +#include +int array[100]; + +float timedifference_msec(struct timeval t0, struct timeval t1) +{ + return (t1.tv_sec - t0.tv_sec) * 1000.0f + (t1.tv_usec - t0.tv_usec) / 1000.0f; +} + + +int main() +{ + struct timeval t0; + struct timeval t1; + float elapsed; + + while(1) + { + int i=0,ch; + long int size,limit,key; + printf("\n1.Create list size and limit\t\t2.Generate Random Numbers\t\t3. Search\t\t4.Exit\t"); + printf("\nEnter your choice: "); + scanf("%d",&ch); + + switch(ch) + { + case 1: + { + printf("Enter number of Elements: "); + scanf("%ld",&size); + long int array[size]; + printf("Enter Upper limit in place values: "); + scanf("%ld",&limit); + break; + } + + case 2: + { + + int num=size; + int j,x,y,temp; + srand( (unsigned) time(NULL) * getpid()); + gettimeofday(&t0, NULL); + if(array != NULL) + { + for(j = 0; j < num; j++) + { + array[j] = rand()%limit; + } + } + gettimeofday(&t1, NULL); + elapsed = timedifference_msec(t0, t1); + + for(j = 0; j < num; j++) + { + printf("%ld\n",array[j]); + } + printf("Random Generation code executed in %f milliseconds.\n", elapsed); + break; + } + + + + case 3: + { + printf("Enter element to search for: "); + scanf("%ld",&key); + gettimeofday(&t0, NULL); + for (i=0;i +#include + +struct node +{ + int coefficient; + int power; + struct node* next; +}; struct node *input1=NULL,*input2=NULL,*sum=NULL; + +void create(struct node *input) +{ + int size,i; + printf("\nEnter Number of Terms in Polynomial Expression: "); + scanf("%d",&size); + for(i=0;icoefficient); + printf("Kindly Enter Term %d Power: ",i+1); + scanf("%d",&input->power); + input->next=(struct node*)malloc(sizeof(struct node)); + input=input->next; + input->next=NULL; + } +} + +void display(struct node *input) +{ + while(input->next!=NULL) + { + printf(" %dx^%d ",input->coefficient,input->power); + input=input->next; + if (input->next!=NULL) + printf("+"); + } +} + + +void addition(struct node *input1,struct node *input2,struct node *sum) +{ + while(input1->next && input2->next) + { + if(input1->power > input2->power) + { + sum->power=input1->power; + sum->coefficient=input1->coefficient; + input1=input1->next; + } + + else if(input1->power < input2->power) + { + sum->power=input2->power; + sum->coefficient=input2->coefficient; + input2=input2->next; + } + + else + { + sum->power=input1->power; + sum->coefficient=input1->coefficient + input2->coefficient; + input1=input1->next; + input2=input2->next; + } + sum->next=(struct node *)malloc(sizeof(struct node)); + sum=sum->next; + sum->next=NULL; + } + + while(input1->next || input2->next) + { + if(input1->next) + { + sum->power=input1->power; + sum->coefficient=input1->coefficient; + input1=input1->next; + } + + if(input2->next) + { + sum->power=input2->power; + sum->coefficient=input2->coefficient; + input2=input2->next; + } + sum->next=(struct node *)malloc(sizeof(struct node)); + sum=sum->next; + sum->next=NULL; + } +} + +void sort(struct node *input) +{ + struct node *i,*j; + int temp; + for(i=input;i->next!=NULL;i=i->next) + { + for(j=i->next;j!=NULL;j=j->next) + { + if(i->power <= j->power) + { + temp=i->power; + i->power=j->power; + j->power=temp; + temp=i->coefficient; + i->coefficient=j->coefficient; + j->coefficient=temp; + } + } + } +} + +void main() +{ + input1=(struct node*)malloc(sizeof(struct node)); + input2=(struct node*)malloc(sizeof(struct node)); + sum=(struct node*)malloc(sizeof(struct node)); + while(1) + { + int ch; + printf("\nMENU"); + printf("\n1.Insert Polynomial Expression 1\t\t2.Insert Polynomial Expression 2\t\t3. Add\t\t4.Exit\t"); + printf("\nEnter your choice: "); + scanf("%d",&ch); + + switch(ch) + { + case 1: + { + create(input1); + sort(input1); + printf("Polynomial expression 1: \t"); + display(input1); + printf("\n"); + break; + } + + case 2: + { + create(input2); + sort(input2); + printf("Polynomial expression 2: \t"); + display(input2); + printf("\n"); + break; + } + + case 3: + { + addition(input1,input2,sum); + printf("Sum is: \t"); + display(sum); + printf("\n"); + break; + } + + case 4: + { + printf("Succesfully exiting program \n"); + exit(0); + } + + default: + { + printf("Error,wrong choice input\n"); + break; + } + } + } +} + diff --git a/C/postfix_Evaluation.c b/C/postfix_Evaluation.c new file mode 100644 index 00000000..e18f450c --- /dev/null +++ b/C/postfix_Evaluation.c @@ -0,0 +1,135 @@ +/*Postfix Evaluation*/ +#include +#include +#include +#include + +#define N 20 +char stack[N]; +int top=-1; + +int isEmpty() +{ + return top == -1; +} +int isFull() +{ + return top == N-1; +} + +char peek() +{ + return stack[top]; +} + +void push(int item) +{ + if(isFull()) + printf("\nStack overflow"); + + else + { + top++; + stack[top] = item; + } +} + +char pop() +{ + if(isEmpty()) + return -1; + + char ch=stack[top]; + top--; + return(ch); +} + +void EvalPostfix(char stack[]) +{ + + int i,value,data1,data2; + char ch; + + for (i = 0; stack[i] != '\0'; i++) + { + ch = stack[i]; + if (isdigit(ch)) + push(ch - '0'); + + else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') + { + data1 = pop();data2 = pop(); + + switch (ch) + { + case '*': + { + value = data2 * data1; + break; + } + + case '/': + { + value = data2 / data1; + break; + } + + case '+': + { + value = data2 + data1; + break; + } + case '-': + { + value = data2 - data1; + break; + } + } + + push(value); + } + } + printf(" %d \n", pop()); +} + +void main() +{ +while(1) + { + char stack[N]; + int ch; + printf("\nMENU"); + printf("\n1.Enter Postfix Expression\t\t2. Evaluate\t\t3.Exit\t"); + printf("\nEnter your choice: "); + scanf("%d",&ch); + + switch(ch) + { + case 1: + { + printf("Enter Postfix expression : "); + scanf("%s",stack); + break; + } + case 2: + { + printf("EValuation of Postfix Expression: "); + EvalPostfix(stack); + break; + } + + case 3: + { + printf("Succesfully exiting program \n"); + exit(0); + } + + default: + { + printf("Error,wrong choice input\n"); + break; + } + } + } +} + diff --git a/C/queue_via_array.c b/C/queue_via_array.c new file mode 100644 index 00000000..5b206fc3 --- /dev/null +++ b/C/queue_via_array.c @@ -0,0 +1,118 @@ +/*Queue implementation using array*/ +#include +#include + +#define size 3 +int rear=-1; +int front=-1; +int queue[size]; + +int isfull() +{ + if(rear==size-1) + return 1; + else + return 0; +} + +int isempty() +{ + if(rear==-1) + return 1; + else + return 0; +} + +void enqueue(int value) +{ + if(isfull()) + printf("Queue is full\n"); + else + { + if(front==-1) + front=0; + rear++; + queue[rear]=value; + printf("Inserted element is %d",queue[rear]); + printf("\n"); + } +} + +void dequeue() +{ + if(isempty()) + printf("Queue is empty\n"); + else + { + printf("Deleted element is %d",queue[front]); + printf("\n"); + front++; + if(front>rear) + { + front=-1; + rear=-1; + } + } +} + +void display() +{ + if(isempty()) + printf("Queue is empty\n"); + else + { + int i; + printf("\nQueue elements are:\n"); + for(i=front;i<=rear;i++) + { + printf("|___%d___",queue[i]); + } + printf("|"); + } +} + +void main() +{ + while(1) + { + int queue,ch,entry,i; + printf("\n1.Enqueue\t\t2. Dequeue\t\t3. Display\t\t4.Exit\t"); + printf("\nEnter your choice: "); + scanf("%d",&ch); + switch(ch) + { + case 1: + { + printf("Enter element: "); + scanf("%d",&entry); + enqueue(entry); + break; + } + + case 2: + { + dequeue(); + break; + } + + case 3: + { + display(); + break; + } + + case 4: + { + printf("Succesfully exiting program \n"); + exit(0); + } + + default: + { + printf("\nError,wrong choice input"); + printf("\n"); + break; + } + } + } +} diff --git a/C/stack_via_array.c b/C/stack_via_array.c new file mode 100644 index 00000000..824e36eb --- /dev/null +++ b/C/stack_via_array.c @@ -0,0 +1,124 @@ +/*Stack implementation using array*/ +#include +#include +int stack[100]; +int top=-1; + +int main() +{ + int ch,entry,i,x,size=0; + while(1) + { + printf("\n1.Create\t\t2. Push\t\t\t3. Pop\t\t\t4.Find top\n5. Empty check\t\t6. Full check\t\t7. Display\t\t8.Exit\t"); + printf("\nEnter your choice: "); + scanf("%d",&ch); + + switch(ch) + { + case 1: + { + printf("Enter size of stack: "); + scanf("%d",&size); + int stack[size]; + break; + } + + case 2: + { + if(top>=size-1) + { + printf("\nStack overflow"); + } + else + { + printf("Enter element: "); + scanf("%d",&entry); + top++; + stack[top]=entry; + printf("%d succesfully stored\n",entry); + } + break; + } + + case 3: + { + if(top<=-1) + { + printf("\nStack underflow"); + } + else + { + x=stack[top]; + printf("The popped element is: %d",x); + top--; + } + printf("\n"); + break; + } + + + + case 4: + { + printf("Top position is %d",top); + printf("\nTop value is %d",stack[top]); + printf("\n"); + break; + } + + case 5: + { + if(top==-1) + printf("Stack empty"); + else + printf("Stack not empty"); + printf("\n"); + break; + } + + case 6: + { + if(top==size-1) + printf("Stack full"); + else + printf("Stack not full"); + printf("\n"); + break; + } + + case 7: + { + if(top==-1) + { + printf("\nStack underflow"); + } + else + { + for(i=top;i>=0;i--) + { + + printf("\n |\t%d\t|",stack[i]); + + } + printf("\n ________________"); + } + printf("\n"); + break; + } + + case 8: + { + printf("Succesfully exiting program \n"); + exit(0); + } + + default: + { + printf("\nError,wrong choice input"); + printf("\n"); + break; + } + } + } +} + diff --git a/C/stack_via_linkedlist.c b/C/stack_via_linkedlist.c new file mode 100644 index 00000000..a5715a3c --- /dev/null +++ b/C/stack_via_linkedlist.c @@ -0,0 +1,117 @@ +/*Stack implementation using linked list*/ +#include +#include + +struct node { + int data; + struct node *next; + }; + struct node *top=NULL; + +int main() +{ + int ch,entry,tempdata,count=0; + while(1) + { + printf("\n1. Push\t\t\t2. Pop\t\t\t3.Find top\t\t4. Display\t\t5.Exit\t"); + printf("\nEnter your choice: "); + scanf("%d",&ch); + + switch(ch) + { + case 1: + { + printf("Enter element: "); + scanf("%d",&entry); + struct node *input; + input=(struct node*)malloc(sizeof(struct node)); + input->data=entry; + if(top==NULL) + { + input->next=NULL; + } + else + { + input->next=top; + } + top=input; + printf("%d is inserted\n",input->data); + break; + } + + case 2: + { + if(top==NULL) + { + printf("Stack Underflow\n"); + } + else + { + struct node *temp=top; + int tempdata =top->data; + top=top->next; + free(temp); + printf("Popped element is %d\n",tempdata); + } + + break; + } + case 3: + { + count=0; + struct node *temp=top; + while(temp->next!=NULL) + { + temp=temp->next; + count++; + } + if(top==NULL) + { + printf("Stack Underflow\n"); + } + else + { + struct node *temp=top; + printf("Top position is %d\n",count); + printf("Top value is %d\n",temp->data); + } + break; + } + case 4: + { + if(top==NULL) + { + printf("Stack Underflow\n"); + } + else + { + struct node *temp=top; + while(temp->next!=NULL) + { + printf("\n |\t%d\t|",temp->data); + temp=temp->next; + } + printf("\n |\t%d\t|\n",temp->data); + printf(" ________________"); + + } + break; + } + + case 5: + { + printf("Succesfully exiting program \n"); + exit(0); + } + + default: + { + printf("\nError,wrong choice input"); + printf("\n"); + break; + } + } + } +} + +