Matrix
Matrix
Matrix
MATRIX
#include<stdio.h>
typedef struct{
int row;
int col;
int value;
}term;
term a[100],b[100];
void transpose(term a[],term b[])
{
int i,j,n;
n=a[0].value;
b[0].row=a[0].col;
b[0].col=a[0].row;
b[0].value=n;
if(n>0)
{
int currentb=1;
for(i=0;i<a[0].col;i++)
{
for (j=1;j<=n;j++)
{
if (a[j].col==i)
{
b[currentb].row=a[j].col;
b[currentb].col=a[j].row;
b[currentb].value=a[j].value;
currentb++;
}
}
}
}
}
void fastTranspose(term a[],term b[])
{
int row_terms[50],start_pos[50],i,j,num_terms;
num_terms = a[0].value;
b[0].row=a[0].col;
b[0].col=a[0].row;
b[0].value=num_terms;
if(num_terms>0){
for(i=0;i<a[0].col;i++)
row_terms[i]=0;
for(i=1;i<a[0].col;i++)
row_terms[a[i].col]++;
start_pos[0]=1;
for(i=1;i<a[0].col;i++)
start_pos[i]=start_pos[i-1]+row_terms[i-1];
for(i=1;i<=num_terms;i++)
{
j=start_pos[a[i].col]++;
b[j].row = a[i].col;
b[j].col = a[i].row;
b[j].value = a[i].value;
}
}
}
void readSparse(term a[])
{
for(int i=1;i<=a[0].value;i++)
{
printf("Enter the term %d (row, column, value): ",i);
scanf("%d%d%d",&a[i].row,&a[i].col,&a[i].value);
}
}
void displaySparse(term a[])
{
for(int i=0;i<=a[0].value;i++)
{
printf("%d %d %d",a[i].row,a[i].col,a[i].value);
printf("\n");
}
}
void main()
{
2. PATTERN MATCHING
#include<stdio.h>
#include<string.h>
int failure[100];
int nfind(char *string, char *pat){
int i,j,start=0;
int lasts=strlen(string)-1;
int lastp=strlen(pat)-1;
int endmatch=lastp;
for(i=0;endmatch<=lasts;endmatch++,start++){
if(string[endmatch]==pat[lastp])
for(j=0,i=start;j<lastp&&string[i]==pat[j];i++,j++);
if(j==lastp)
return start;
}
return -1;
}
int pmatch(char *string,char *pat){
int i=0,j=0;
int lens = strlen(string);
int lenp = strlen(pat);
while(i<lens && j< lenp){
if (string[i]==pat[j]){
i++;j++;}
else if(j==0)i++;
else j=failure[j-1]+1;
}
return ((j==lenp) ? (i-lenp): -1);
}
void fail(char *pat){
int n=strlen(pat);
failure[0] = -1;
for(int j=1;j<n;j++){
int i=failure[j-1];
while ((pat[j]!=pat[i+1])&& i>=0)
i=failure[i];
if (pat[j]==pat[i+1])
failure[j]=i+1;
else
failure[j]=-1;
}
}
void main(){
char string[50],pat[50];
int pos;
printf("Enter the string: ");
scanf("%s",string);
printf("Enter the pattern: ");
scanf("%s",pat);
pos=nfind(string,pat);
if(pos>=0)
printf("\nThe pattern \"%s\" is found in \"%s\" at position
%d",pat,string,pos);
else
printf("\nThe pattern \"%s\" is not found in \"%s\".",pat,string);
fail(pat);
pos=pmatch(string,pat);
if(pos>=0)
printf("\nPattern \"%s\" is found in the string \"%s\" at position
%d",pat,string,pos);
else
printf("\nPattern \"%s\" is not found in the string");
}
3. INFIX TO POSTFIX
#include<stdio.h>
typedef enum{lparen,rparen,plus,minus,times,divide,mod,eos,operand}precedence;
int top = -1;
int isp[]={0,19,12,12,13,13,13,0};
int icp[]={20,19,12,12,13,13,13,0};
char expr[50];
precedence stack[50];
precedence operatorValue(char *symbol, int *n){
*symbol = expr[(*n)++];
switch(*symbol){
case '(': return lparen;
case ')': return rparen;
case '+': return plus;
case '-': return minus;
case '*': return times;
case '/': return divide;
case '%': return mod;
case '\0': return eos;
default: return operand;
}
}
}
}
precedence pop(){
return stack[top--];
}
void Postfix()
{
char symbol;
precedence token;
int n=0;
top=0;
stack[0]=eos;
token = operatorValue(&symbol,&n);
while(token!=eos){
if (token == operand){
printf("%c",symbol);
}
else if (token==rparen){
while(stack[top]!=lparen)
printOperator(pop());
pop();
}
else{
while(isp[stack[top]]>=icp[token]){
printOperator(pop());
}
push(token);
}
token = operatorValue(&symbol,&n);
}
while((token=pop())!=eos)
printOperator(token);
printf("\n");
int main(){
printf("\nEnter the Infix Expression: ");
scanf("%s",expr);
printf("\nThe Postfix Expression: \n");
Postfix();
return 0;
}
4. POSTFIX EVALUATION
#include<stdio.h>
typedef enum{lparen,rparen,plus,minus,times,divide,mod,eos,operand}precedence;
int top=-1;
char expr[50];
int stack[50];
precedence operatorValue(char *symbol,int *n){
*symbol=expr[(*n)++];
switch(*symbol){
case '(':return lparen;
case ')':return rparen;
case '+':return plus;
case '-':return minus;
case '*':return times;
case '/':return divide;
case '%':return mod;
case '\0':return eos;
default:return operand;
}
}
int pop(){
return stack[top--];}
int eval(void){
precedence token;
char symbol;
int op1,op2;
int n=0;
token=operatorValue(&symbol,&n);
while(token!=eos){
if(token==operand)
push(symbol-'0');
else{
op2=pop();
op1=pop();
switch(token){
case plus:push(op1+op2);
break;
case minus:push(op1-op2);
break;
case times:push(op1*op2);
break;
case divide:push(op1/op2);
break;
case mod:push(op1%op2);
break;
}
}
token=operatorValue(&symbol,&n);
}
return pop();
}
int main(){
printf("enter the postfix exp.:-");
scanf("%s",expr);
printf("the evaluated result:\n%d\n",eval());
return 0;
}
5. CIRCULAR QUEUES
#include<stdio.h>
#include<stdlib.h>
typedef struct{
int key;
}element;
int front=-1, rear=-1, capacity;
element *queue;
void copy(element* sourcestart, element* sourceend, element* deststart)
{
while (sourcestart<sourceend){
deststart->key = sourcestart->key;
sourcestart++;
deststart++;
}
}
void queueFull()
{
element* newQueue;
newQueue = (element*)malloc(2*capacity*sizeof(*queue));
if (newQueue == NULL) {
fprintf(stderr, "Memory allocation error during resize.\n");
exit(EXIT_FAILURE);
}
int start=(front+1)%capacity;
if(start < 2)
copy(queue+start, queue+start+capacity-1, newQueue);
else
{
copy(queue+start, queue+capacity , newQueue);
copy(queue, queue+rear+1, newQueue+capacity-start);
}
front=2*capacity-1;
rear=capacity-1;
capacity*=2;
free(queue);
queue=newQueue;
}
void enqueue(element item)
{
rear=(rear+1)%capacity;
if(front==rear || (front == -1 && rear == capacity-1))
queueFull();
(queue+rear)->key = item.key;
}
element dequeue()
{
element item;
if(front==rear)
{
item.key=-1;
return item;
}
front=(front+1)%capacity;
item.key = (queue+front)->key;
return item;
}
void printq()
{
int i;
if(front==rear)
{
printf("\nQueue is Empty\n");
return;
}
for(i=(front+1)%capacity; i!=rear; i=(i+1)%capacity)
printf("%d\t",(queue+i)->key);
printf("%d", (queue+i)->key);
printf("\n");
printf("\nFront: %d Rear: %d\n", front, rear);
}
void main()
{
int choice;
element item;
printf("Enter intial size of queue: ");
scanf("%d",&capacity);
queue = (element *)malloc(capacity*sizeof(element));
if (queue == NULL) {
fprintf(stderr, "Memory allocation error.\n");
exit(EXIT_FAILURE);
}
while(1)
{
printf("\n 1. Add\n 2. Delete\n 3. Display\n 4. Exit\n");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("\nEnter item to insert: ");
scanf("%d",&item.key);
enqueue(item);
break;
}
case 2:
{
item=dequeue();
if(item.key==-1)
printf("\nQueue is Empty.\n");
else
printf("\nItem deleted: %d \n", item.key);
break;
}
case 3:
{
printq();
break;
}
case 4:
{
exit(0);
break;
}
default: printf("\nInvalid input!!\n");
}
}
}
6. STACKS
#include<stdio.h>
#include<stdlib.h>
typedef struct{
int key;
}element;
typedef struct stack{
element data;
struct stack *link;
}stack;
typedef struct stack *stackptr;
stackptr top;
void push(element item){
if(top==NULL){
top = (stackptr)malloc(sizeof(stackptr));
top->data = item;
top->link = NULL;
}
else{
stackptr temp;
temp = (stackptr)malloc(sizeof(stackptr));
temp->data = item;
temp->link = top;
top=temp;
}
}
element pop(){
stackptr temp,ref;
element item;
ref = top;
if (ref==NULL){
item.key = -1;
return item;
}
else{
ref = ref->link;
}
item = top->data;
free(top);
top = ref;
return item;
}
void display(){
stackptr ref = top;
if (ref == NULL){
printf("Stack is empty.");
return;
}
printf("The stack elements are: \n");
while(ref){
printf("%d\t",ref->data);
ref=ref->link;
}
}
int main() {
int choice;
element value;
printf("\nImplementation of Stack using Linked List\n");
while (1) {
printf("\n\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value.key);
push(value);
break;
case 2:
value = pop();
if (value.key==-1){
printf("Stack is empty.");
}
else{
printf("Popped element is :%d\n", value.key);
}
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}
}
#include <stdio.h>
#include <stdlib.h>
struct polyNode {
int coeff;
int expon;
struct polyNode *link;
};
while (a && b) {
switch (COMPARE(a->expon, b->expon)) {
case 1:
attach(a->coeff, a->expon, &rear);
a = a->link;
break;
case -1:
attach(b->coeff, b->expon, &rear);
b = b->link;
break;
case 0:
sum = a->coeff + b->coeff;
if (sum) {
attach(sum, a->expon, &rear);
}
a = a->link;
b = b->link;
}
}
return rear;
}
void printPoly(polyptr a) {
for (; a; a = a->link) {
printf("%dx^%d", a->coeff, a->expon);
if (a->link) {
printf(" + ");
}
}
printf("\n");
}
int main() {
polyptr a, b, c;
readPoly2(&a);
printf("Polynomial 1: ");
printPoly(a);
readPoly2(&b);
printf("Polynomial 2: ");
printPoly(b);
c = padd(a, b);
printf("\nSum of two polynomials: ");
printPoly(c);
return 0;
}
9. DOUBLY LL
#include <stdio.h>
#include <stdlib.h>
int main() {
// Create a head node
nodePointer head = (nodePointer)malloc(sizeof(Node));
printf("Create head node.\n");
head->llink = head;
head->rlink = head;
dinsert(head, node1);
printf("Insert Node 1\n");
// Delete a node
ddelete(head, node2);
printf("After deletion of node 2:\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
treeptr root = NULL;
return 0;
}
#include<stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
typedef struct{
int key;
}element;
element heap[MAX_SIZE];
void insert(element item, int *n){
int i;
if((*n)==MAX_SIZE-1){
printf("Heap Full\n");
return;
}
i=++(*n);
while(i!=1 && item.key>heap[i/2].key){
heap[i]=heap[i/2];
i/=2;
}
heap[i]=item;
}
element deleteHeap(int* n)
{
int parent, child;
element temp, item;
if(*n==0){
printf("Heap Empty\n");
item.key=-1;
return item;
}
item = heap[1];
temp = heap[(*n)--];
parent = 1;
child = 2;
while(child<=*n)
{
if(child<*n && heap[child].key < heap[child+1].key)
child++;
if(temp.key >= heap[child].key)
break;
heap[parent]=heap[child];
parent=child;
child=child*2;
}
heap[parent]=temp;
return item;
}
void display(int n)
{
int i;
for(i=1;i<=n;i++)
{
printf("%d\n",heap[i].key);
}
}
int main()
{
int choice,n=0;
element item;
while(1)
{
printf("Enter\n 1. Insert\n 2. Display\n 3. Delete\n 4. Exit");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter element to insert");
scanf("%d", &item.key);
insert(item, &n);
break;
case 2:
display(n);
break;
case 3:
item = deleteHeap(&n);
if(item.key!=-1)
printf("Element Deleted: %d\n",item.key);
break;
case 4:
exit(0); }
}
}
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 50
#define FALSE 0
#define TRUE 1
node_pointer graph[MAX_VERTICES];
short int visited[MAX_VERTICES];
int n = 0; /* vertices currently in use */
void insert(int vi, int vj);
void readgraph();
void dfs(int v);
void bfs(int v);
void addq(queue_pointer *front, queue_pointer *rear, int v);
int deleteq(queue_pointer *front);
int main() {
readgraph();
return 0;
}
void readgraph() {
int i, vi, vj, no_of_edges;
printf("Enter no. of vertices: ");
scanf("%d", &n);
void dfs(int v) {
node_pointer w;
visited[v] = TRUE;
printf("%5d", v);
for (w = graph[v]; w; w = w->link)
if (!visited[w->vertex])
dfs(w->vertex);
}
void bfs(int v) {
node_pointer w;
queue_pointer front, rear;
front = rear = NULL;
printf("%5d", v);
visited[v] = TRUE;
addq(&front, &rear, v);
while (front) {
v = deleteq(&front);
for (w = graph[v]; w; w = w->link)
if (!visited[w->vertex]) {
printf("%5d", w->vertex);
addq(&front, &rear, w->vertex);
visited[w->vertex] = TRUE;
}
}
}