Data Stracture Lab
Data Stracture Lab
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
gets(string);
length++;
reverse_string[length - i - 1] = string[i];
{
if (reverse_string[i] != string[i])
flag = 0;
if (flag == 1)
else
getch();
return 0;
}
2. To create a two dimensional array of numbers and calculate & display
the row & column sum and the grand total.
#include <stdio.h>
#include <conio.h>
int main()
int i,j;
//Initialize matrix a
int a[][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
rows = (sizeof(a)/sizeof(a[0]));
cols = (sizeof(a)/sizeof(a[0][0]))/rows;
sumRow = 0;
total_sumRow=total_sumRow+sumRow;
sumCol = 0;
total_sumCol=total_sumCol+sumCol;
getch();
return 0;
}
3. To write a program of matrix multiplication.
#include <stdio.h>
#include <conio.h>
int main()
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
printf("enter the number of row=");
scanf("%d",&r);
scanf("%d",&c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&b[i][j]);
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
mul[i][j]=0;
for(k=0;k<c;k++)
mul[i][j]+=a[i][k]*b[k][j];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
printf("%d\t",mul[i][j]);
printf("\n");
}
getch();
return 0;
}
4. To write a program to insert (Push) an element into the sack and delete
(Pop) an element from the stack using pointer
#include <stdio.h>
#include <conio.h>
int MAXSIZE = 8;
int stack[8];
int isempty() {
if(top == -1)
return 1;
else
return 0;
int isfull() {
if(top == MAXSIZE)
return 1;
else
return 0;
int peek() {
return stack[top];
int pop() {
int data;
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
} else {
}
int push(int data) {
if(!isfull()) {
top = top + 1;
stack[top] = data;
} else {
int main() {
push(3);
push(5);
push(9);
push(1);
push(12);
push(15);
while(!isempty()) {
printf("%d\n",data);
getch();
return 0;
}
5. To write a program to convert an infix expression to a postfix expression
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
//char stack
char stack[25];
int top = -1;
void push(char item) {
stack[++top] = item;
}
char pop() {
return stack[top--];
}
//returns precedence of operators
int precedence(char symbol) {
switch(symbol) {
case '+':
case '-':
return 2;
break;
case '*':
case '/':
return 3;
break;
case '^':
return 4;
break;
case '(':
case ')':
case '#':
return 1;
break;
}
}
//check whether the symbol is operator?
int isOperator(char symbol) {
switch(symbol) {
case '+':
case '-':
case '*':
case '/':
case '^':
case '(':
case ')':
return 1;
break;
default:
return 0;
}
}
//converts infix expression to postfix
void convert(char infix[],char postfix[]) {
int i,symbol,j = 0;
stack[++top] = '#';
for(i = 0;i<strlen(infix);i++) {
symbol = infix[i];
if(isOperator(symbol) == 0) {
postfix[j] = symbol;
j++;
} else {
if(symbol == '(') {
push(symbol);
} else {
if(symbol == ')') {
while(stack[top] != '(') {
postfix[j] = pop();
j++;
}
pop(); //pop out (.
} else {
if(precedence(symbol)>precedence(stack[top])) {
push(symbol);
} else {
while(precedence(symbol)<=precedence(stack[top])) {
postfix[j] = pop();
j++;
}
push(symbol);
}
}
}
}
}
while(stack[top] != '#') {
postfix[j] = pop();
j++;
}
postfix[j]='\0'; //null terminate string.
}
//int stack
int stack_int[25];
int top_int = -1;
void push_int(int item) {
stack_int[++top_int] = item;
}
char pop_int() {
return stack_int[top_int--];
}
//evaluates postfix expression
int evaluate(char *postfix){
char ch;
int i = 0,operand1,operand2;
while( (ch = postfix[i++]) != '\0') {
if(isdigit(ch)) {
push_int(ch-'0'); // Push the operand
} else {
//Operator,pop two operands
operand2 = pop_int();
operand1 = pop_int();
switch(ch) {
case '+':
push_int(operand1+operand2);
break;
case '-':
push_int(operand1-operand2);
break;
case '*':
push_int(operand1*operand2);
break;
case '/':
push_int(operand1/operand2);
break;
}
}
}
return stack_int[top_int];
}
void main() {
char infix[25] = "1*(2+3)",postfix[25];
convert(infix,postfix);
printf("Infix expression is: %s\n" , infix);
printf("Postfix expression is: %s\n" , postfix);
printf("Evaluated expression is: %d\n" , evaluate(postfix));
getch();
}
#include<conio.h>
# define MAX 5
int cqueue_arr[MAX];
int front = -1;
return;
if(front == -1)
front = 0;
rear = 0;
else
if(rear == MAX-1)
rear = 0;
else
rear = rear+1;
}
cqueue_arr[rear] = item ;
void deletion()
if(front == -1)
printf("Queue Underflow\n");
return ;
if(front == rear)
front = -1;
rear=-1;
else
if(front == MAX-1)
front = 0;
else
front = front+1;
}
void display()
if(front == -1)
printf("Queue is empty\n");
return;
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
else
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
front_pos = 0;
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
printf("\n");
int main()
int choice,item;
do
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 :
scanf("%d", &item);
insert(item);
break;
case 2 :
deletion();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Wrong choice\n");
}while(choice!=4);
getch();
return 0;
}
9.Write a program to reverse a linked list.
// Iterative C program to reverse a linked list
#include <stdio.h>
#include <stdlib.h>
/* Link list node */
struct Node {
int data;
struct Node* next;
};
/* Function to reverse the linked list */
static void reverse(struct Node** head_ref)
{
struct Node* prev = NULL;
struct Node* current = *head_ref;
struct Node* next = NULL;
while (current != NULL) {
// Store next
next = current->next;
// Reverse current node's pointer
current->next = prev;
// Move pointers one position ahead.
prev = current;
current = next;
}
*head_ref = prev;
}
/* Function to push a node */
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
/* Function to print linked list */
void printList(struct Node* head)
{
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
}
/* Driver program to test above function*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 85);
printf("Given linked list\n");
printList(head);
reverse(&head);
printf("\nReversed Linked list \n");
printList(head);
getchar();
}
10. To create a circular linked list and insert & delete an element from the
list.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
/* Basic node structure. */
struct node {
int data;
struct node * next;
};
/* Functions declarations */
void createList(struct node ** head, int n);
void displayList(struct node ** head);
void deleteAll(struct node ** head, int key);
int main()
{
int n, key, data, choice;
struct node * head = NULL;
/* Run forever until user chooses 0 */
while(choice != 0)
{
printf("--------------------------------------------\n");
printf(" CIRCULAR LINKED LIST PROGRAM \n");
printf("--------------------------------------------\n");
printf("1. Create List\n");
printf("2. Display list\n");
printf("3. Delete all by key\n");
printf("0. Exit\n");
printf("--------------------------------------------\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter number of nodes to create: ");
scanf("%d", &n);
createList(&head, n);
break;
case 2:
displayList(&head);
break;
case 3:
printf("Enter key to delete from list: ");
scanf("%d", &key);
deleteAll(&head, key);
break;
case 0:
exit(0);
break;
default:
printf("Error! Invalid choice. Please choose between 0-3");
}
printf("\n\n\n\n");
}
getch();
return 0;
}
/**
* Delete all occurrence of an element by key from a
* given circular linked list.
*/
void deleteAll(struct node ** head, int key)
{
int i, count;
struct node *prev, *cur;
if (*head == NULL)
{
printf("List is empty.\n");
return;
}
count = 0;
cur = *head;
prev = cur;
// Find node before head node
while (prev->next != *head)
{
prev = prev->next;
count++;
}
// Iterate till first node
i = 0;
while (i <= count)
{
if (cur->data == key)
{
// Link prev node with next node of current
if (cur->next != cur)
prev->next = cur->next;
else
prev->next = NULL;
// Adjust head node if needed
if (cur == *head)
*head = prev->next;
// Delete current node
free(cur);
// Move current node ahead
if (prev != NULL)
cur = prev->next;
else
cur = NULL;
}
else
{
prev = cur;
cur = cur->next;
}
i++;
}
}
/**
* Create a circular linked list of n nodes.
*/
void createList(struct node ** head, int n)
{
int i, data;
struct node *prevNode, *newNode;
prevNode = NULL;
/* Creates and link n nodes */
for(i=1; i<=n; i++)
{
newNode = malloc(sizeof(struct node));
printf("Enter data of %d node: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
// Link the previous node with newly created node
if (prevNode != NULL)
prevNode->next = newNode;
// Adjust head node
if (*head == NULL)
*head = newNode;
// Move previous node ahead
prevNode = newNode;
}
// Link last node with first node
prevNode->next = *head;
printf("\nCIRCULAR LINKED LIST CREATED SUCCESSFULLY\n");
}
/**
* Display content of the linked list.
*/
void displayList(struct node ** head)
{
struct node *current;
int n = 1;
if(*head == NULL)
{
printf("List is empty.\n");
return;
}
current = *head;
printf("DATA IN THE LIST:\n");
do {
printf("Data %d = %d\n", n, current->data);
current = current->next;
n++;
}while(current != *head);
}