Cs3311-To Print
Cs3311-To Print
h>
#include<conio.h>
#include<stdlib.h>
#define max 5
static int stack[max];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return (stack[top--]);
}
void view()
{
int i;
if (top < 0)
printf("\n Stack Empty \n"); else
{
printf("\n Top-->");
for(i=top; i>=0; i--)
{
printf("%4d", stack[i]);
}
printf("\n");
}
}
void main()
{
int ch=0, val;
while(ch != 4)
{
printf("\n STACKOPERATION \n");
printf("1.PUSH ");
printf("2.POP ");
printf("3.VIEW ");
printf("4.QUIT \n"); printf("Enter Choice : ");
scanf("%d", &ch); switch(ch)
{
case 1:
if(top < max-1)
{
printf("\nEnter Stack element : ");
scanf("%d", &val);
push(val);
}
else
printf("\n Stack Overflow \n"); break;
case 2:
if(top < 0)
printf("\n Stack Underflow \n"); else
{
val = pop();
printf("\n Popped element is %d\n", val);
}
break; case 3:
view(); break;
case 4:
exit(0); default:
printf("\n Invalid Choice \n");
}
}
}
#include <stdio.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue operation");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int cqueue[5];
int front = -1, rear = -1, n=5;
void enqueue(int val){
if ((front == 0 && rear == n-1) || (front == rear+1)) {
printf("Queue Overflow \n");
return;
}
if (front == -1) {
front = 0;
rear = 0;
}
else {
if (rear == n - 1)
rear = 0;
else
rear = rear + 1;
}
cqueue[rear] = val ;
}
void dequeue(){
if (front == -1) {
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d ", cqueue[front]);
if (front == rear) {
front = -1;
rear = -1;
}
else {
if (front == n - 1)
front = 0;
else
front = front + 1;
}
}
void display(){
int f = front, r = rear;
if (front == -1) {
printf("Queue is empty");
return;
}
printf("Queue elements are :\n");
if (f <= r) {
while (f <= r){
printf("%d", cqueue[f]);
f++;
}
}
else {
while (f <= n - 1) {
printf("%d",cqueue[f]);
f++;
}
f = 0;
while (f <= r) {
printf("%d",cqueue[f]);
f++;
}
}
int menu(){
int choice;
printf("\n 1.Add value to the list");
printf("\n 2. Delete value to the list");
printf("\n 3. Travesre/View List");
printf("\n 4. exit");
printf("\n Please enter your choice: \t");
scanf("%d",&choice);
return(choice);
}
int main(){
int value;
while(1){
switch(menu()){
case 1:
printf("Input for insertion: ");
scanf("%d",&value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("invalid choice");
}
}
}
#include <stdio.h>
#include <conio.h>
void create(); void insert(); void search(); void deletion(); void display();
int i, e, n, pos; static int b[50];
void main()
{
int ch;
char g = 'y'; create();
do
{
printf("\n List Operations");
printf("\n 1.Deletion\n 2.Insert\n 3.Search\n 4.Exit\n");
printf("Enter your choice: "); scanf("%d", &ch);
switch(ch)
{
case 1:
deletion(); break;
case 2:
insert(); break;
case 3:
search(); break;
case 4:
exit(0); default:
printf("\n Enter the correct choice:");
}
printf("Do you want to continue: ");
fflush(stdin);
scanf("\n %c",&g);
} while(g=='y' || g=='Y');
getch();
}
void create()
{
printf("\n Enter the number of elements:"); scanf("%d",&n);
printf("\n Enter list elements: "); for(i=0; i<n; i++)
scanf("%d", &b[i]);
}
void deletion()
{
printf("\n enter the position you want to delete: "); scanf("%d", &pos);
if(pos >= n)
printf("\n Invalid location"); else
{
for(i=pos+1; i<n; i++) b[i-1] = b[i];
n--;
printf("List elements after deletion"); display();
}
}
void search()
{
int flag = 0;
printf("\n Enter the element to be searched: "); scanf("%d", &e);
for(i=0; i<n; i++)
{
if(b[i] == e)
{
flag = 1;
printf("Element is in the %d position", i); break;
}
}
if(flag == 0)
printf("Value %d is not in the list", e);
}
void insert()
{
printf("\n Enter the position you need to insert: ");
scanf("%d", &pos);
if(pos >= n)
printf("\n Invalid location");
else
{
++n;
for(i=n; i>pos; i--)
b[i] = b[i-1];
printf("\n Enter the element to insert: ");
scanf("%d", &e);
b[pos] = e;
}
printf("\n List after insertion:");
display();
}
void display()
{
for(i=0; i<n; i++) printf("\n %d", b[i]);
}
#include <stdio.h>
#include <stdlib.h>
struct node
{
int label;
struct node *next;
};
void main()
{
int ch, fou=0; int k;
struct node *h, *temp, *head, *h1;
head = (struct node*) malloc(sizeof(struct node));
head->label = -1;
head->next = NULL;
while(-1)
{
printf("\n\n SINGLY LINKED LIST OPERATIONS \n");
printf("1->Add ");
printf("2->Delete ");
printf("3->View ");
printf("4->Exit \n");
printf("Enter your choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("\n Enter label after which to add : ");
scanf("%d", &k);
h = head; fou = 0;
if (h->label == k) fou = 1;
while(h->next != NULL)
{
if (h->label == k)
{
fou=1;
break;
}
h = h->next;
}
if (h->label == k) fou = 1;
if (fou != 1)
printf("Node not found\n"); else
{
temp=(struct node *)(malloc(sizeof(struct node)));
printf("Enter label for new node : "); scanf("%d", &temp->label);
temp->next = h->next; h->next = temp;
}
break;
case 2:
printf("Enter label of node to be deleted\n"); scanf("%d", &k);
fou = 0;
h = h1 = head;
while (h->next != NULL)
{
h = h->next;
if (h->label == k)
{
fou = 1;
break;
}
}
if (fou == 0)
printf("Sorry Node not found\n");
else
{
while (h1->next != h)
h1 = h1->next;
h1->next = h->next;
free(h);
printf("Node deleted successfully \n");
}
break;
case 3:
printf("\n\n HEAD -> ");
h=head;
while (h->next != NULL)
{
h = h->next;
printf("%d -> ",h->label);
}
printf("NULL");
break;
case 4:
exit(0);
}
}
}
#include<stdio.h>
#include<conio.h>
struct Node
{
int data;
struct Node *next;
}*top = NULL;
void push(int);
void pop();
void display();
void main()
{
int choice, value;
printf("\n:: Stack using Linked List ::\n");
while(1)
{
printf("\n****** MENU ******\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4:
exit(0);
default:
printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf("\nInsertion is Success!!!\n");
}
void pop()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else
{
struct Node *temp = top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}
void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include<alloc.h>
struct node
{
int label;
struct node *next;
};
void main()
{
int ch=0; int k;
struct node *h, *temp, *head;
head = (struct node*) malloc(sizeof(struct node));
head->next = NULL;
while(1)
{
printf("\n Queue using Linked List \n"); printf("1->Insert ");
printf("2->Delete "); printf("3->View "); printf("4->Exit \n");
printf("Enter your choice : "); scanf("%d", &ch);
switch(ch)
{
case 1:
temp=(struct node *)(malloc(sizeof(struct node)));
printf("Enter label for new node : ");
scanf("%d", &temp->label);
h = head;
while (h->next != NULL)
h = h->next;
h->next = temp;
temp->next = NULL;
break;
case 2:
h = head->next;
head->next = h->next;
printf("Node deleted \n");
free(h);
break;
case 3:
printf("\n\nHEAD -> ");
h=head;
while(h->next!=NULL)
{
h = h->next;
printf("%d -> ",h->label);
}
printf("NULL \n");
break;
case 4:
exit(0);
}
}
}
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
struct link
{
int coeff;int
pow;
struct link *next;
};
struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
Void main()
{
poly1 = (struct link *)malloc(sizeof(struct link));
poly2 = (struct link *) malloc(sizeof(struct link));
poly = (struct link *) malloc(sizeof(struct link));
printf("Enter 1stPolynomial:");
create(poly1);
printf("\nEnter 2ndPolynomial:");
create(poly2);
printf("\nPoly1: ");
show(poly1);
printf("\nPoly2: ");
show(poly2);
polyadd(poly1, poly2, poly);
printf("\nAdded Polynomial: ");
show(poly);
}
#include <stdio.h>
#include <conio.h>#include
<string.h>#define MAX 20
while(stack[top] != '#')
{
postfix[j] = pop();
j++;
}
postfix[j] = '\0';
}
Void main()
{
char infix[20],postfix[20];clrscr();
printf("Enter the valid infix string: ");
gets(infix);
convertip(infix, postfix);
printf("The corresponding postfix string is: ");puts(postfix);
getch();
}
char pop()
{
char a;
a = stack[top];
top--;
return a;
}
#include <stdio.h>
#include <conio.h>
struct stack
{
int top; float a[50];
}s;
void main()
{
char pf[50]; float d1,d2,d3;
int i;
clrscr();
s.top = -1;
printf("\n\n Enter the postfix expression: ");
gets(pf);
for(i=0; pf[i]!='\0'; i++)
{
switch(pf[i])
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
s.a[++s.top] = pf[i]-'0';
break;
case '+':
d1 = s.a[s.top--];
d2 = s.a[s.top--];
s.a[++s.top] = d1 + d2;
break;
case '-':
d2 = s.a[s.top--];
d1 = s.a[s.top--];
s.a[++s.top] = d1 - d2;
break;
case '*':
d2 = s.a[s.top--];
d1 = s.a[s.top--];
s.a[++s.top] = d1*d2;
break;
case '/':
d2 = s.a[s.top--];
d1 = s.a[s.top--];
s.a[++s.top] = d1 / d2;
break;
}
}
printf("\n Expression value is %5.2f", s.a[s.top]);
getch();
}
#include <stdio.h>
#include <stdlib.h>
struct node
{
int key;
struct node *left;
struct node *right;
};
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL; return temp;
}
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left); printf("%d ", root->key); inorder(root->right);
}
}
struct node* insert(struct node* node, int key)
{
if (node == NULL)
return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
if (found == -1)
printf("Element not found");
getch();
}
#include<stdio.h>
void main()
{
int i, j, k, n, temp, a[20], p=0;
printf("Enter total elements: ");
scanf("%d",&n);
printf("Enter array elements: ");
for(i=0; i<n; i++)
scanf("%d", &a[i]);