0% found this document useful (0 votes)
2 views42 pages

Data Structure Lab

The document contains several C programming exercises related to data structures, including creating and manipulating two-dimensional arrays, matrix multiplication, stack operations, infix to postfix conversion, evaluating postfix expressions, and queue operations. Each section provides source code examples for implementing these concepts, along with explanations of the functionality. The programs cover fundamental data structure operations such as push, pop, insert, and delete, along with error handling for overflow and underflow conditions.

Uploaded by

bookstore454
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views42 pages

Data Structure Lab

The document contains several C programming exercises related to data structures, including creating and manipulating two-dimensional arrays, matrix multiplication, stack operations, infix to postfix conversion, evaluating postfix expressions, and queue operations. Each section provides source code examples for implementing these concepts, along with explanations of the functionality. The programs cover fundamental data structure operations such as push, pop, insert, and delete, along with error handling for overflow and underflow conditions.

Uploaded by

bookstore454
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Data structure Lab

1. To create a two-dimensional array of numbers and calculate & display the row & column, sum and
the grand total.
Source Code.
/*To create a two-dimensional array of numbers and calculate & display the row & column sum and the
grand total*/
#include <stdio.h>
int main()
{
int rows, cols, sumRow, sumCol;
//Initialize matrix a
int a[][3] =
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
//Calculates number of rows and columns present in given matrix
rows = (sizeof(a)/sizeof(a[0]));
cols = (sizeof(a)/sizeof(a[0][0]))/rows;
//Calculates sum of each row of given matrix
for(int i = 0; i < rows; i++)
{
sumRow = 0;
for(int j = 0; j < cols; j++){
sumRow = sumRow + a[i][j];
}
printf("Sum of %d row: %d\n", (i+1), sumRow);
}

//Calculates sum of each column of given matrix


for(int i = 0; i < cols; i++)
{
sumCol = 0;
for(int j = 0; j < rows; j++)
{
sumCol = sumCol + a[j][i];
}
printf("Sum of %d column: %d\n", (i+1), sumCol);
}
return 0;
}
Output:
2. To write a program of matrix multiplication.
Source Code.
#include <stdio.h>
#include <conio.h>
int main()
{
int a[10][10], b[10][10], mul[10][10], r, c, i, j, k;
system("cls");
printf("enter the number of row = ");
scanf ("%d", &r);
printf("Enter the number of column = ");
scanf("%d", &c);
printf("enter the first matrix element = \n");
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("enter the second matrix element = \n");
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");
}
return 0;
}
Output:

3. To write a program of matrix multiplication.


Source Code:
#include<stdio.h>
#include<stdlib.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);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
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");
}
return 0;
}
Output:

4. To write a program to insert (Push) an element and delete (Pop) an element from the stack using
pointer.
Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX 3

int st[MAX], top=-1;


void push(int st[], int val);
int pop(int st[]);
int peek(int st[]);
void display(int st[]);

int main(int argc, char *argv[])


{
int val, option;
do
{
printf("\n ******MAIN MENU******");
printf("\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. PEEK");
printf("\n 4. DISPLAY");
printf("\n 5. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the number to be puished on the stack: ");
scanf("%d", &val);
push(st, val);
break;
case 2:
val = pop(st);
if(val != -1)
printf("\n The value deleted from the stack is: %d", val);
break;
case 3:
val = peek(st);
if(val != -1)
printf("\n The value stored at the top of the stack is: %d", val);
break;
case 4:
display(st);
break;
}
}
while(option != 5);
return 0;
}
void push(int st[], int val)
{
if(top == MAX-1)
{
printf("\n STACK OVERFLOW");
}
else
{
top++;
st[top] = val;
}
}
int pop(int st[])
{
int val;
if(top == -1)
{
printf("\n STACK UNDERFLOW");
return -1;
}
else
{
val = st[top];
top--;
return val;
}
}
void display(int st[])
{
int i;
if(top == -1)
printf("\n STACK IS EMPTY");
else
{
for(i=top; i>=0; i--)
printf("\n %d", st[i]);
printf("\n");
}
}
int peek(int st[])
{
if(top == -1)
{
printf("\n STACK IS EMPTY");
return -1;
}
else
return (st[top]);
}
Output:

5. To write a program to convert an infix expression to a postfix expression.


Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
char st[MAX];
int top=-1;
void push(char st[], char);
char pop(char st[]);
void InfixtoPostfix(char source[], char target[]);
int getPriority(char);
int main()
{
char infix[100], postfix[100];
printf("\n Enter any Infix Expression: ");
gets(infix);
strcpy(postfix, "");
InfixtoPostfix(infix, postfix);
printf("\n The corresponding postfix expression is: ");
puts(postfix);
getch();
return 0;
}
void InfixtoPostfix(char source[], char target[])
{
int i=0, j=0;
char temp;
strcpy(target, "");
while(source[i]!='\0')
{
if(source[i]=='(')
{
push(st, source[i]);
i++;
}
else if(source[i] == ')')
{
while((top!=-1) && (st[top]!='('))
{
target[j] = pop(st);
j++;
}
if(top==-1)
{
printf("\n INCORRECT EXPRESSION");
exit(i);
}
temp = pop(st); //remove parenthesis
i++;
}
else if(isdigit(source[i]) || isalpha(source[i]))
{
target[j] = source[i];
j++;
i++;
}
else if (source[i] == '+' || source[i] == '-' || source[i] == '*' || source[i] == '/' || source[i] == '%')
{
while( (top!=-1) && (st[top]!='(') && (getPriority(st[top]) > getPriority(source[i])))
{
target[j] = pop(st);
j++;
}
push(st, source[i]);
i++;
}
else
{
printf("\n INCORRECT ELEMENT IN EXPRESSION");
exit(1);
}
}
while((top!=-1) && (st[top]!='('))
{
target[j] = pop(st);
j++;
}
target[j]='\0';
}
int getPriority(char op)
{
if(op=='/' || op == '*' || op == '%')
return 1;
else if(op=='+' || op=='-')
return 0;
}
void push(char st[], char val)
{
if(top==MAX-1)
printf("\n STACK OVERFLOW");
else
{
top++;
st[top]=val;
}
}
char pop(char st[])
{
char val=' ';
if(top==-1)
printf("\n STACK UNDERFLOW");
else
{
val=st[top];
top--;
}
return val;
}
Output:

6. To evaluate a postfix expression.


Source Code:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#define MAX 100
float st[MAX];
int top=-1;
void push(float st[], float val);
float pop(float st[]);
float evaluatePostfixExp(char exp[]);
int main()
{
float val;
char exp [100];
printf("\n Enter any Postfix expression: ");
gets(exp);
val = evaluatePostfixExp(exp);
printf("\n Value of the Postfix expression = %.2f", val);
getch();
return 0;
}
float evaluatePostfixExp(char exp[])
{
int i=0;
float op1, op2, value;
while(exp[i] != '\0')
{
if(isdigit(exp[i]))
push(st, (float)(exp[i]-'0'));
else
{
op2 = pop(st);
op1 = pop(st);
switch(exp[i])
{
case '+':
value = op1 + op2;
break;
case '-':
value = op1 - op2;
break;
case '/':
value = op1 / op2;
break;
case '*':
value = op1 * op2;
break;
case '%':
value = (int)op1 % (int)op2;
break;
}
push(st, value);
}
i++;
}
return(pop(st));
}
void push(float st[], float val)
{
if(top==MAX-1)
printf("\n STACK OVERFLOW");
else
{
top++;
st[top]=val;
}
}
float pop(float st[])
{
float val=-1;
if(top==-1)
printf("\n STACK UNDERFLOW");
else
{
val=st[top];
top--;
}
return val;
}
Output:

7. To write a program to insert an element in the queue and delete an element from the queue using
pointer.
Source code:
#include <stdio.h>
#include <conio.h>
#define MAX 10
int queue[MAX];
int front = -1, rear = -1;
void insert(void);
int delete_element(void);
int peek(void);
void display(void);
int main()
{
int option, val;
do
{
printf("\n\n ******MAIN MENU******");
printf("\n 1. Insert an element");
printf("\n 2. Delete an element");
printf("\n 3. Peek");
printf("\n 4. Display the Queue");
printf("\n 5. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1:
insert();
break;
case 2:
val = delete_element();
if (val != -1)
printf("\n The number deleted is: %d", val);
break;
case 3:
val = peek();
if (val != -1)
printf("\n The first value in queue is: %d", val);
break;
case 4:
display();
break;
}
}
while(option != 5);
getch();
return 0;
}
void insert()
{
int num;
printf("\n Enter the number to be inserted in the queue: ");
scanf("%d", &num);
if(rear == MAX-1)
printf("\n OVERFLOW");
else if(front == -1 && rear == -1)
front = rear = 0;
else
rear++;
queue[rear] = num;
}
int delete_element()
{
int val;
if(front == -1 || front>rear)
{
printf("\n UNDERFLOW");
return -1;
}
else
{
val = queue[front];
front++;
if(front > rear)
front = rear = -1;
return val;
}
}
int peek()
{
if(front==-1 || front>rear)
{
printf("\n QUEUE IS EMPTY");
return -1;
}
else
{
return queue[front];
}
}
void display()
{
int i;
printf("\n");
if(front == -1 || front > rear)
printf("\n QUEUE IS EMPTY");
else
{
for(i = front; i <= rear; i++)
printf("\t %d", queue[i]);
}
}
Output:

8. To create a circular queue and add an element and delete an element from a circular queue.
Source Code:
#include <stdio.h>
#include <conio.h>
#define MAX 10
int queue[MAX];
int front=-1, rear=-1;
void insert(void);
int delete_element(void);
int peek(void);
void display(void);
int main()
{
int option, val;
do
{
printf("\n ******MAIN MENU******");
printf("\n 1. Insert an element");
printf("\n 2. Delete an element");
printf("\n 3. Peek");
printf("\n 4. Display the queue");
printf("\n 5. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1:
insert();
break;
case 2:
val = delete_element();
if(val!=-1)
printf("\n The number is deleted is: %d", val);
break;
case 3:
val = peek();
if(val!=-1)
printf("\n The first value in queue is: %d", val);
break;
case 4:
display();
break;
}
}
while(option!=5);
getch();
return 0;
}
void insert()
{
int num;
printf("\n Enter the number to be inserted in the queue: ");
scanf("%d", &num);
if(front==0 && rear==MAX-1)
printf("\n OVERFLOW");
else if(front==-1 && rear==-1)
{
front=rear=0;
queue[rear]=num;
}
else if(rear==MAX-1 && front!=0)
{
rear=0;
queue[rear]=num;
}
else
{
rear++;
queue[rear]=num;
}
}
int delete_element()
{
int val;
if(front==-1 && rear==-1)
{
printf("\n UNDERFLOW");
return -1;
}
val=queue[front];
if(front==-1 && rear==-1)
front=rear=-1;
else
{
if(front==MAX-1)
front=0;
else
front++;
}
return val;
}
int peek()
{
if(front==-1 && rear==-1)
{
printf("\n QUEUE IS EMPTY");
return -1;
}
else
{
return queue[front];
}
}
void display()
{
int i;
printf("\n");
if(front==-1 && rear==-1)
printf("\n QUEUE IS EMPTY");
else
{
if(front<rear)
{
for(i=front; i<=rear; i++)
printf("\t %d", queue[i]);
}
else
{
for(i=front; i<MAX; i++)
printf("\t %d", queue[i]);
for(i=0; i<=rear; i++)
printf("\t %d", queue[i]);
}
}
}
Output:

9. To write a program of a structure containing an item name along with the unit price. The user
enters the item name and quantity to be purchased. Program print outs total price of item with name
using pointer in a structure or array in a structure.
Source Code:
#include <stdio.h>
void main()
{
struct date
{
int day;
int month;
int year;
};
struct details
{
char name[20];
int price;
int code;
int qty;
struct date mfg;
};
struct details item[50];
int n, i;
printf("Enter number of items:");
scanf("%d", &n);
fflush(stdin);
for (i = 0; i < n; i++)
{
fflush(stdin);
printf("Item name: \n");
scanf("%s", item[i].name);
fflush(stdin);
printf("Item code: \n");
scanf("%d", &item[i].code);
fflush(stdin);
printf("Quantity: \n");
scanf("%d", &item[i].qty);
fflush(stdin);
printf("price: \n");
scanf("%d", &item[i].price);
fflush(stdin);
printf("Manufacturing date(dd-mm-yyyy): \n");
scanf("%d-%d-%d", &item[i].mfg.day,
&item[i].mfg.month, &item[i].mfg.year);
}
printf(" ***** INVENTORY ***** \n");
printf("------------------------------------------------------------------\n");
printf("S.N.| NAME | CODE | QUANTITY | PRICE | MFG.DATE \n");
printf("------------------------------------------------------------------\n");
for (i = 0; i < n; i++)
printf("%d %-15s %-d %-5d %-5d %d/%d/%d \n", i + 1, item[i].name, item[i].code,
item[i].qty,
item[i].price, item[i].mfg.day, item[i].mfg.month, item[i].mfg.year);
printf("------------------------------------------------------------------\n");
}
Output:

10. To create a single linked list and -


(a)insert a node in the list (before header node, in between two nodes, end of the list) and (b)delete a
node from the list(first node, last node, in between two nodes).
Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start = NULL;
struct node *create_ll(struct node*);
struct node *display(struct node*);
struct node *insert_beg(struct node*);
struct node *insert_end(struct node*);
struct node *insert_before(struct node*);
struct node *insert_after(struct node*);
struct node *delete_beg(struct node*);
struct node *delete_end(struct node*);
struct node *delete_node(struct node*);
struct node *delete_after(struct node*);
struct node *delete_list(struct node*);
struct node *sort_list(struct node*);

int main(int argc, char *argv[])


{
int option;
do{
printf("\n \n ******MAIN MENU******");
printf("\n 1: Create a list");
printf("\n 2: Display the list");
printf("\n 3: Add a node at the beginning");
printf("\n 4: Add a node at the end");
printf("\n 5: Add a node before a given node");
printf("\n 6: Add a node after a given node");
printf("\n 7: Delete a node from the beginning");
printf("\n 8: Delete a node from the end");
printf("\n 9: Delete a given node");
printf("\n 10: Delete a node after a given node");
printf("\n 11: Delete the entire list");
printf("\n 12: Sort the list");
printf("\n 13: EXIT");
printf("\n \n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1: start = create_ll(start);
printf("\n LINKED LIST CREATED");
break;
case 2: start = display(start);
break;
case 3: start = insert_beg(start);
break;
case 4: start = insert_end(start);
break;
case 5: start = insert_before(start);
break;
case 6: start = insert_after(start);
break;
case 7: start = delete_beg(start);
break;
case 8: start = delete_end(start);
break;
case 9: start = delete_node(start);
break;
case 10: start = delete_after(start);
break;
case 11: start = delete_list(start);
printf("\n LINKED LIST DELETED");
break;
case 12: start = sort_list(start);
break;
}
}
while(option != 13);
getch();
return 0;
}
struct node *create_ll(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter -1 to end");
printf("\n Enter the data: ");
scanf("%d", &num);
while(num != -1)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> data=num;
if(start==NULL)
{
new_node -> next = NULL;
start = new_node;
}
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next = new_node;
new_node->next=NULL;
}
printf("\n Enter the data: ");
scanf("%d", &num);
}
return start;
}
struct node *display(struct node *start)
{
struct node *ptr;
ptr = start;
while(ptr != NULL)
{
printf("\t %d", ptr -> data);
ptr = ptr -> next;
}
return start;
}
struct node *insert_beg(struct node *start)
{
struct node *new_node;
int num;
printf("\n Enter the data: ");
scanf("%d", &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
new_node -> next = start;
start = new_node;
return start;
}
struct node *insert_end(struct node *start)
{
struct node *ptr, *new_node;
int num;
printf("\n Enter the data: ");
scanf("%d", &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
new_node -> next = NULL;
ptr = start;
while(ptr -> next != NULL)
ptr = ptr -> next;
ptr -> next = new_node;
return start;
}
struct node *insert_before(struct node *start)
{
struct node *new_node, *ptr, *preptr;
int num, val;
printf("\n Enter the data: ");
scanf("%d", &num);
printf("\n Enter the value before which the data has to be inserted: ");
scanf("%d", &val);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
ptr = start;
while(ptr -> data != val)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr -> next = new_node;
new_node -> next = ptr;
return start;
}
struct node *insert_after(struct node *start)
{
struct node *new_node, *ptr, *preptr;
int num, val;
printf("\n Enter the data: ");
scanf("%d", &num);
printf("\n Enter the value after which the data has to be inserted: ");
scanf("%d", &val);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
ptr = start;
preptr = ptr;
while(preptr -> data != val)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr -> next=new_node;
new_node -> next = ptr;
return start;
}
struct node *delete_beg(struct node *start)
{
struct node *ptr;
ptr = start;
start = start -> next;
free(ptr);
return start;
}
struct node *delete_end(struct node *start)
{
struct node *ptr, *preptr;
ptr = start;
while(ptr -> next != NULL)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr -> next = NULL;
free(ptr);
return start;
}
struct node *delete_node(struct node *start)
{
struct node *ptr, *preptr;
int val;
printf("\n Enter the value of the node which has to be deleted: ");
scanf("%d", &val);
ptr = start;
if(ptr -> data == val)
{
start = delete_beg(start);
return start;
}
else
{
while(ptr -> data != val)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr -> next = ptr -> next;
free(ptr);
return start;
}
}
struct node *delete_after(struct node *start)
{
struct node *ptr, *preptr;
int val;
printf("\n Enter the value after which the node has to deleted: ");
scanf("%d", &val);
ptr = start;
preptr = ptr;
while(preptr -> data != val)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr -> next=ptr -> next;
free(ptr);
return start;
}
struct node *delete_list(struct node *start)
{
struct node *ptr; //lines 252-254 were modified from original code to the unresponsiveness in output
window
if(start != NULL)
{
ptr = start;
while(ptr != NULL)
{
printf("\n %d is to be deleted next", ptr -> data);
start = delete_beg(ptr);
ptr = start;
}
}
return start;
}
struct node *sort_list(struct node *start)
{
struct node *ptr1, *ptr2;
int temp;
ptr1 = start;
while(ptr1 -> next != NULL)
{
ptr2 = ptr1 -> next;
while(ptr2 != NULL)
{
if(ptr1 -> data > ptr2 -> data)
{
temp = ptr1 -> data;
ptr1 -> data = ptr2 -> data;
ptr2 -> data = temp;
}
ptr2 = ptr2 -> next;
}
ptr1 = ptr1 -> next;
}
return start;
}
Output:

(c) Concatenate two lists


Source code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
struct node *create_list(struct node *);
struct node *concat( struct node *start1,struct node *start2);
struct node *addatbeg(struct node *start, int data);
struct node *addatend(struct node *start,int data);
void display(struct node *start);
int main()
{
struct node *start1=NULL,*start2=NULL;
start1=create_list(start1);
start2=create_list(start2);
printf("\nFirst list is : ");
display(start1);
printf("\nSecond list is : ");
display(start2);
start1=concat(start1, start2);
printf("\nConcatenated list is : ");
display(start1);
return 0;
}/*End of main()*/
struct node *concat( struct node *start1,struct node *start2)
{
struct node *ptr;
if(start1==NULL)
{
start1=start2;
return start1;
}
if(start2==NULL)
return start1;
ptr=start1;
while(ptr->link!=NULL)
ptr=ptr->link;
ptr->link=start2;
return start1;
}
struct node *create_list(struct node *start)
{
int i,n,data;
printf("\nEnter the number of nodes : ");
scanf("%d",&n);
start=NULL;
if(n==0)
return start;
printf("Enter the element to be inserted : ");
scanf("%d",&data);
start=addatbeg(start,data);
for(i=2;i<=n;i++)
{
printf("Enter the element to be inserted : ");
scanf("%d",&data);
start=addatend(start,data);
}
return start;
}/*End of create_list()*/
void display(struct node *start)
{
struct node *p;
if(start==NULL)
{
printf("\nList is empty\n");
return;
}
p=start;
while(p!=NULL)
{
printf("%d ", p->info);
p=p->link;
}
printf("\n");
}/*End of display() */
struct node *addatbeg(struct node *start,int data)
{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=start;
start=tmp;
return start;
}/*End of addatbeg()*/

struct node *addatend(struct node *start, int data)


{
struct node *p,*tmp;
tmp= (struct node *)malloc(sizeof(struct node));
tmp->info=data;
p=start;
while(p->link!=NULL)
p=p->link;
p->link=tmp;
tmp->link=NULL;
return start;
}/*End of addatend()*/
Output:

11. Write a program to create a doubly linked list and -


(a) insert a node in the list (before header node, in between two nodes, end of the list); (b) delete a
node from the list (first node, last node, in between two nodes).
Source Code:
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct node
{
struct node *next;
int data;
struct node *prev;
};
struct node *start = NULL;
struct node *create_ll(struct node *);
struct node *display(struct node *);
struct node *insert_beg(struct node *);
struct node *insert_end(struct node *);
struct node *insert_before(struct node *);
struct node *insert_after(struct node *);
struct node *delete_beg(struct node *);
struct node *delete_end(struct node *);
struct node *delete_before(struct node *);
struct node *delete_after(struct node *);
struct node *delete_list(struct node *);
int main()
{
int option;
do
{
printf("\n **********MAIN MENU**********");
printf("\n 1: Create a doubly linked list");
printf("\n 2: Display the list");
printf("\n 3: Add a node at the beginning");
printf("\n 4: Add a node at the end");
printf("\n 5: Add a node before a given node");
printf("\n 6: Add a node after a given node");
printf("\n 7: Delete a node from the beginning");
printf("\n 8: Delete a node from the end");
printf("\n 9: Delete a node before a given node");
printf("\n 10: Delete a node after a given node");
printf("\n 11: Delete the entire list");
printf("\n 12: EXIT");
printf("\n \n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1: start = create_ll(start);
printf("\n DOUBLY LINKED LIST CREATED");
break;
case 2: start = display(start);
break;
case 3: start = insert_beg(start);
break;
case 4: start = insert_end(start);
break;
case 5: start = insert_before(start);
break;
case 6: start = insert_after(start);
break;
case 7: start = delete_beg(start);
break;
case 8: start = delete_end(start);
break;
case 9: start = delete_before(start);
break;
case 10: start = delete_after(start);
break;
case 11: start = delete_list(start);
break;
}
}
while(option != 12);
getch();
return 0;
}
struct node *create_ll(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter -1 to end");
printf("\n Enter the data: ");
scanf("%d", &num);
while(num != -1)
{
if(start == NULL)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> prev = NULL;
new_node -> data = num;
new_node -> next = NULL;
start = new_node;
}
else
{
ptr = start;
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data=num;
while(ptr->next!=NULL)
ptr = ptr->next;
ptr->next = new_node;
new_node->prev=ptr;
new_node->next=NULL;
}
printf("\n Enter the data: ");
scanf("%d", &num);
}
return start;
}
struct node *display(struct node *start)
{
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
printf("\t %d", ptr->data);
ptr = ptr->next;
}
return start;
}
struct node *insert_beg(struct node *start)
{
struct node *new_node;
int num;
printf("\n Enter the data: ");
scanf("%d", &num);
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = num;
start->prev = new_node;
new_node->next = start;
new_node->prev = NULL;
start = new_node;
return start;
}
struct node *insert_end(struct node *start)
{
struct node *ptr, *new_node;
int num;
printf("\n Enter the data: ");
scanf("%d", &num);
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = num;
ptr=start;
while(ptr->next != NULL)
ptr = ptr ->next;
ptr->next = new_node;
new_node->prev = ptr;
new_node->next = NULL;
return start;
}
struct node *insert_before(struct node *start)
{
struct node *new_node, *ptr;
int num, val;
printf("\n Enter the data: ");
scanf("%d", &num);
printf("\n Enter the value before which the data has to be inserted: ");
scanf("%d", &val);
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = num;
ptr = start;
while(ptr->data != val)
ptr = ptr->next;
new_node->next = ptr;
new_node->prev = ptr->prev;
ptr->prev->next = new_node;
ptr->prev = new_node;
return start;
}
struct node *insert_after(struct node *start)
{
struct node *new_node, *ptr;
int num, val;
printf("\n Enter the data: ");
scanf("%d", &num);
printf("\n enter the value after which the data has to be inserted: ");
scanf("%d", &val);
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = num;
ptr = start;
while(ptr->data != val)
ptr = ptr->next;
new_node->prev = ptr;
new_node->next = ptr->next;
ptr->next->prev = new_node;
ptr-> next = new_node;
return start;
}
struct node *delete_beg(struct node *start)
{
struct node *ptr;
ptr = start;
start = start->next;
start->prev = NULL;
free(ptr);
return start;
}
struct node *delete_end(struct node *start)
{
struct node *ptr;
ptr = start;
while(ptr->next != NULL)
ptr = ptr->next;
ptr->prev->next = NULL;
free(ptr);
return start;
}
struct node *delete_after(struct node *start)
{
struct node *ptr, *temp;
int val;
printf("\n Enter the value after which the node has to be deleted: ");
scanf("%d", &val);
ptr = start;
while(ptr->data != val)
ptr = ptr->next;
temp = ptr->next;
ptr->next = temp->next;
temp->next->prev = ptr;
free(temp);
return start;
}
struct node *delete_before(struct node *start)
{
struct node *ptr, *temp;
int val;
printf("\n Enter the value before which the node has to be deleted: ");
scanf("%d", &val);
ptr = start;
while(ptr->data != val)
ptr = ptr->next;
temp = ptr->prev;
if(temp == start)
{
start = delete_beg(start);
}
else
{
ptr->prev = temp->prev;
temp->prev->next = ptr;
}
free(temp);
return start;
}
struct node *delete_list(struct node *start)
{
while(start != NULL)
start = delete_beg(start);
return start;
}
Output:

12. To create a circular linked list and insert & delete an element from the list.
Source Code:
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start = NULL;
struct node *create_cll(struct node *);
struct node *display(struct node *);
struct node *insert_beg(struct node *);
struct node *insert_end(struct node *);
struct node *delete_beg(struct node *);
struct node *delete_end(struct node *);
struct node *delete_after(struct node *);
struct node *delete_list(struct node *);
int main()
{
int option;
do
{
printf("\n\n **********MAIN MENU**********");
printf("\n 1: Create a list");
printf("\n 2: Display the list");
printf("\n 3: Add a node at the beginning");
printf("\n 4: Add a node at the end");
printf("\n 5: Delete a node from the beginning");
printf("\n 6: Delete a node from the end");
printf("\n 7: Delete a node after a given node");
printf("\n 8: Delete the entire list");
printf("\n 9: EXIT");
printf("\n \n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1: start = create_cll(start);
printf("\n CIRCULR LINKED LIST CREATED");
break;
case 2: start = display(start);
break;
case 3: start = insert_beg(start);
break;
case 4: start = insert_end(start);
break;
case 5: start = delete_beg(start);
break;
case 6: start = delete_end(start);
break;
case 7: start = delete_after(start);
break;
case 8: start = delete_list(start);
printf("\n CIRCULR LINKED LIST DELETED");
break;
}
}
while(option != 9);
getch();
return 0;
}
struct node *create_cll(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter -1 to end");
printf("\n Enter the data: ");
scanf("%d", &num);
while(num!=-1)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = num;
if(start == NULL)
{
new_node->next = new_node;
start = new_node;
}
else
{
ptr = start;
while(ptr->next != start)
ptr = ptr->next;
ptr->next = new_node;
new_node->next = start;
}
printf("\n Enter the data: ");
scanf("%d", &num);
}
return start;
}
struct node *display(struct node *start)
{
struct node *ptr;
ptr=start;
while(ptr->next != start)
{
printf("\t %d", ptr->data);
ptr = ptr->next;
}
printf("\t %d", ptr->data);
return start;
}
struct node *insert_beg(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter the data: ");
scanf("%d", &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = num;
ptr = start;
while(ptr->next != start)
ptr = ptr->next;
ptr->next = new_node;
new_node->next = start;
start = new_node;
return start;
}
struct node *insert_end(struct node *start)
{
struct node *ptr, *new_node;
int num;
printf("\n Enter the data: ");
scanf("%d", &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = num;
ptr = start;
while(ptr->next != start)
ptr = ptr->next;
ptr->next = new_node;
new_node->next = start;
return start;
}
struct node *delete_beg(struct node *start)
{
struct node *ptr;
ptr = start;
while(ptr->next != start)
ptr = ptr->next;
ptr->next = start->next;
free(start);
start = ptr->next;
return start;
}
struct node *delete_end(struct node *start)
{
struct node *ptr, *preptr;
ptr = start;
while(ptr->next != start)
{
preptr = ptr;
ptr = ptr->next;
}
preptr->next = ptr->next;
free(ptr);
return start;
}
struct node *delete_after(struct node *start)
{
struct node *ptr, *preptr;
int val;
printf("\n Enter the value after which the node has to deleted: ");
scanf("%d", &val);
ptr = start;
preptr = ptr;
while(preptr->data != val)
{
preptr = ptr;
ptr = ptr->next;
}
preptr->next = ptr->next;
if(ptr == start)
{
start = preptr->next;
}
free(ptr);
return start;
}
struct node *delete_list(struct node *start)
{
struct node *ptr;
ptr = start;
while(ptr->next != start)
{
start = delete_end(start);
}
free(start);
return start;
}
Output:
13. Write a program to merge two shorted linked list.
Source Code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
struct node *create(struct node *start);
struct node *insert_s(struct node *start,int data);
struct node *insert(struct node *start,int data);
void display(struct node *start );
void merge(struct node *p1,struct node *p2);
int main()
{
struct node *start1=NULL,*start2=NULL;
start1=create(start1);
start2=create(start2);
printf("List1 : ");
display(start1);
printf("List2 : ");
display(start2);
merge(start1, start2);
return 0;
}/*End of main()*/
void merge(struct node *p1,struct node *p2)
{
struct node *start3;
start3=NULL;
while(p1!=NULL && p2!=NULL)
{
if(p1->info < p2->info)
{
start3=insert(start3,p1->info);
p1=p1->link;
}
else if(p2->info < p1->info)
{
start3=insert(start3,p2->info);
p2=p2->link;
}
else if(p1->info==p2->info)
{
start3=insert(start3,p1->info);
p1=p1->link;
p2=p2->link;
}
}
/*If second list has finished and elements left in first list*/
while(p1!=NULL)
{
start3=insert(start3,p1->info);
p1=p1->link;
}
/*If first list has finished and elements left in second list*/
while(p2!=NULL)
{
start3=insert(start3,p2->info);
p2=p2->link;
}
printf("Merged list is : ");
display(start3);
}
struct node *create(struct node *start )
{
int i, n,data;
printf("Enter the number of nodes : ");
scanf("%d",&n);
start=NULL;
for(i=1;i<=n;i++)
{
printf("Enter the element to be inserted : ");
scanf("%d", &data);
start=insert_s(start, data);
}
return start;
}/*End of create_slist()*/

struct node *insert_s(struct node *start, int data)


{
struct node *p,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
/*list empty or data to be added in beginning */
if(start==NULL || data<start->info)
{
tmp->link=start;
start=tmp;
return start;
}
else
{
p=start;
while(p->link!=NULL && p->link->info < data)
p=p->link;
tmp->link=p->link;
p->link=tmp;
}
return start;
}/*End of insert_s()*/
struct node *insert(struct node *start,int data)
{
struct node *p,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
/*If list is empty*/
if(start==NULL)
{
tmp->link=start;
start=tmp;
return start;
}
else /*Insert at the end of the list*/
{
p=start;
while(p->link!=NULL)
p=p->link;
tmp->link=p->link;
p->link=tmp;
}
return start;
}/*End of insert()*/
void display(struct node *start)
{
struct node *p;
if(start==NULL)
{
printf("List is empty\n");
return;
}
p=start;
while(p!=NULL)
{
printf("%d ",p->info);
p=p->link;
}
printf("\n");
}/*End of display()*/
Output:
14. Write a program to reverse a linked list.
Source Code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *next;
};
void create(struct node **);
void reverse(struct node **);
void release(struct node **);
void display(struct node *);
int main()
{
struct node *p = NULL;
int n;
printf("Enter data into the list\n");
create(&p);
printf("Displaying the nodes in the list:\n");
display(p);
printf("Reversing the list...\n");
reverse(&p);
printf("Displaying the reversed list:\n");
display(p);
release(&p);
return 0;
}
void reverse(struct node **head)
{
struct node *p, *q, *r;
p = q = r = *head;
p = p->next->next;
q = q->next;
r->next = NULL;
q->next = r;
while (p != NULL)
{
r = q;
q = p;
p = p->next;
q->next = r;
}
*head = q;
}
void create(struct node **head)
{
int c, ch;
struct node *temp, *rear;
do
{
printf("Enter number: ");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
rear->next = temp;
}
rear = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
} while (ch != 0);
printf("\n");
}
void display(struct node *p)
{
while (p != NULL)
{
printf("%d\t", p->num);
p = p->next;
}
printf("\n");
}

void release(struct
node **head)
{
struct node *temp =
*head;
*head = (*head)-
>next;
while ((*head) !=
NULL)
{
free(temp);
temp = *head;
(*head) =
(*head)- >next;
}
}
Output:
15. (a) To write a program to calculate the binomial co-efficient of nCr of two numbers using
recursive function.
Source Code:
#include<stdio.h>
int BC(int n, int k);
int main()
{
int n,k;
printf("Enter n and k
: ");

scanf("%d%d",&n,&k);
printf("Binomial coefficient \n",BC(n,k));
printf("%d\n",BC(n,k));
return 0;
}
int BC(int n, int k)
{
if(k==0 || k==n)
return 1;
return BC(n-1,k-1) + BC(n-1,k);
}
Output:

(b)To write a program to calculate the binomial co-efficient of nCr of two numbers using function in
non-recursive way.
Source Code:
#include<stdio.h>
void main() {
int i, j, n, k, min, c[20][20]={0};
printf("\n Enter the value of n: ");
scanf("%d", &n);
printf("\n Enter the value of k: ");
scanf("%d", &k);
if(n >= k) {
for(i=0; i<=n; i++) {
min = i<k? i:k;
for(j = 0; j <= min; j++) {
if(j==0 || j == i) {
c[i][j] = 1;
} else {
c[i][j] = c[i-1][j-1] + c[i-1][j];
}
}
}
printf("%d\t",c[n][k]);
printf("\n");
} else {
printf("\n Invalid input \n Enter value n>=k \n");
}
}
Output:

16. (a) To write a program to generate Fibonacci Series using recursive function.
Source Code:
#include <stdio.h>
int Fibonacci(int);
int main()
{
int n, i = 0, res;
printf("Enter the number of terms: \n");
scanf("%d", &n);
printf("Fibonacci Series
\n");
for(i = 0; i < n; i++)
{
res = Fibonacci(i);
printf("%d\t",res);
}
return 0;
}
int Fibonacci (int n)
{
if ( n == 0 )
return 0;
else if (n == 1)
return 1;
else
return (Fibonacci(n-1) + Fibonacci(n-2));
}
Output:
(b) To write a program to
generate Fibonacci
Series using function in
non-recursive way.
Source Code:
#include <stdio.h>
int main()
{
int n1 = 0, n2 = 1, n3, i, number;
printf("Enter the number of elements: \n");
scanf("%d", &number);
printf("\n %d%d \n \n", n1, n2); //printing 0 and n1
for(i = 2; i < number; ++i)
{
n3 = n1 + n2;
printf("%d \n", n3);
n1 = n2;
n2 = n3;
}
return 0;
}

Output:

17. To write a program to create a binary tree and transverse it in pre-order and post-order form.
Source Code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int item;
struct node* left;
struct node* right;
};
void preorderTraversal(struct node* root)
{
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
void postorderTraversal(struct node* root)
{
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}
struct node* createNode(value)
{
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node* insertLeft(struct node* root, int value)
{
root->left = createNode(value);
return root->left;
}
struct node* insertRight(struct node* root, int value)
{
root->right = createNode(value);
return root->right;
}
int main()
{
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->left, 6);
printf("\nPreorder traversal \n");
preorderTraversal(root);
printf("\nPostorder traversal \n");
postorderTraversal(root);
}
Output:

You might also like