Data Structure
Data Structure
on
Table of Contents
Program Program Name Page No
No.
1 Write a the C program to create an array by inserting N elements in it
then find second non repeating element from the array.
3 Write a C program Create a Dynamic array and then Reverse the array using
recursion and then finally print the array
5
Write a C Program to Convert Infix to Postfix Expression using Stack.
6 Write a C Program to create singly linked list by adding nodes in the right
hand side and delete alternate node from the list and then print the final list.
7 Write a C Program implement STACK using Link List in menu driven form.
12 . Write a C program to create TWO singly linked list L1 and L2 and sort both
the list and finally merge both the list such that L2 comes after L1.[ use double
pointer]
13 Write C program to create a doubly link list by adding the node right hand side
and then check list is in palindrome form or not.
14 Write a C program to create a circular link list by adding the nodes in right
hand side and then print the list.
DEPARTMENT OF CS & IT
B.Tech. (CS & IT)
STUDENT LAB REPORT SHEET
Photograph
Name of Student ……………………………………….…….Mob.No……………………………….………….. Passport Size
2 Practical-02
3 Practical-03
4 Practical-04
5 Practical-05
6 Practical-06
7 Practical-07
8 Practical-08
9 Practical-09
10 Practical-10
11 Practical-11
12 Practical-12
13 Practical-13
14 Practical-14
#include<stdio.h>
#include<limits.h>
int main()
{
int n;
printf("enter the size of array ");
scanf("%d",&n);
int a[n];
printf("enter the elements \n");
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int maximum=a[0];
for(int i=1;i<n;i++)
{
if(maximum<a[i])
maximum=a[i];
}
int hash[maximum+1],counter=0,ans;
for(int i=0;i<maximum+1;i++)
{
hash[i]=0;
}
for(int i=0;i<n;i++)
{
hash[a[i]]++;
int flag=0;
for(int i=0;i<n;i++)
{
if(hash[a[i]]==1)
{
counter++;
if(counter==2)
{
flag=1;
ans=a[i];
break;
}
}
}
if(flag)
printf("Second Non Repeating number is %d",ans);
else
printf("There is no Second Non Repeating ");
}
#include<stdio.h>
#include<limits.h>
int main()
{
int n;
printf("enter the size of array ");
scanf("%d",&n);
int a[n];
printf("enter the elements \n");
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int maximum=a[0];
for(int i=1;i<n;i++)
{
if(maximum<a[i])
maximum=a[i];
}
int hash[maximum+1],counter=0,ans;
for(int i=0;i<maximum+1;i++)
{
hash[i]=0;
}
for(int i=0;i<n;i++)
{
hash[a[i]]++;
}
int flag=0;
for(int i=0;i<maximum+1;i++)
{
if(hash[i]>1)
{
counter++;
if(counter==3)
{
flag=1;
ans=i;
break;
}
}
}
if(flag)
printf("Third Repeating Element in array is %d",ans);
else
printf("There is no Third Repeating Element ");
}
Q3. Write a C program Create a Dynamic array and then Reverse the
array using recursion and then finally print the array.
#include <stdio.h>
#include <stdlib.h>
int temp=a[n-1];
reverse(a,n-1,i+1);
a[i]=temp;
int main()
{
int n;
printf("Enter the size of array ");
scanf("%d",&n);
int *p=(int*)malloc(n*sizeof(int));
for(int i=0;i<n;i++)
*(p+i)=i+1;
reverse(p,n,0);
for(int i=0;i<n;i++)
printf("%d ",p[i]);
}
#include<stdio.h>
int x;
if(*top>=n-1)
printf(" \n\tSTACK is over flow ");
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
*top=*top+1;
stack[*top]=x;
}
if(*top<=-1)
printf("\n\t Stack is under flow ");
else
{
printf("\n\t The popped elements is % d ",stack[*top]);
*top=*top-1;
}
{
printf("\n The elements in STACK \n");
for(int i=*top; i>=0; i--)
printf("\n%d ",stack[i]);
int main ()
{
int stack[100], choice, n, top=-1;
switch(choice)
{
case 1:
{
push(stack,&top,n);
break;
}
case 2:
{
pop(stack,&top);
break;
}
case 3:
{
display(stack,&top);
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
printf ("\n\t Please Enter a Valid Choice (1 / 2 / 3 / 4) ");
}while(choice!=4);
return 0;
}
#include <stdio.h>
#include <ctype.h>
char ch = stack[*top];
*top=*top-1;
return ch;
int priority(char x)
{
if(x=='^')
return 3;
if(x=='*'||x=='/')
return 2;
if(x=='+'||x=='-')
return 1;
else
return 0;
int main()
{
char stack[10];
int top=-1;
char exp[20];
char *e;
printf("enter the infix expression ");
scanf("%s",exp);
e=exp;
while(*e!='\0')
{
if(isalpha(*e))
printf("%c",*e);
else if(*e=='(')
push(*e,stack,&top);
else if(*e==')')
{
while(stack[top]!='(')
{
printf("%c",pop(stack,&top));
}
pop(stack,&top);
}
else
{
while( priority(stack[top]) >= priority(*e) )
{
printf("%c",pop(stack,&top));
}
push(*e,stack,&top);
}
e++;
while(top!=-1)
{
printf("%c",pop(stack,&top));
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
};
if(head==NULL)
{
head=temp;
return head;
}
temp1->link=temp;
return head;
}
{
printf("%d ",temp1->data);
temp1=temp1->link;
}
}
if(temp1==NULL || temp2==NULL)
return ;
temp1->link=temp2->link;
temp1=temp2->link;
if(temp1!=NULL)
temp2=temp1->link;
}
int main()
{
int x,n;
printf("enter the no of element you want to insert in linked list ");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&x);
head=insert(head,x);
}
printf("\nPRINTIG LINKED LIST : ");
print(head);
print(head);
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* link;
};
if(head==NULL)
{
head=temp;
}
else
{
temp->link=head;
head=temp;
}
return head;
}
head=head->link;
free(temp);
}
return head;
}
}
}
int main()
{
int ch,x;
struct node* head=NULL;
while(1)
{
printf("\nEnter your choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the value ");
scanf("%d",&x);
head = push(x,head);
break;
case 2:
head = pop(head);
break;
case 3:
top_ele(head);
break;
case 4:
display(head);
break;
case 5:
exit(0);
break;
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* link;
};
}
else if(*head==*front)
{
printf("poped element is %d \n",(*front)->data);
*head=*front=NULL;
}
else
{
printf("poped element is %d \n",(*front)->data);
struct node *temp=*front;
*front=(*front)->link;
free(temp);
}
}
}
int main()
{
int ch,x;
struct node *head=NULL,*front=NULL;
while(1)
{
printf("\nEnter your choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the value ");
scanf("%d",&x);
push(x,&head,&front);
break;
case 2:
pop(&head,&front);
break;
case 3:
top_ele(&head,&front);
break;
case 4:
display(&head,&front);
break;
case 5:
exit(0);
break;
} } }
#include<stdio.h>
#define MAX 10
(*rear)++;
int max_index=0;
for(int i=1;i<=rear;i++)
{
if(p[max_index]<p[i])
max_index=i;
}
return max_index;
}
{
printf("Queue is empty.\n");
return;
}
int p_index=max_p(p,*rear);
while(p_index<(*rear))
{
a[p_index]=a[p_index+1];
p[p_index]=p[p_index+1];
p_index++;
}
(*rear)--;
for(int i=0;i<=rear;i++)
printf("%d ",a[i]);
printf("\n");
}
int main()
{
int a[MAX],p[MAX],rear=-1,choice;
printf("1.Insert\n2.Delete\n3.Display\n 0.Exit\n");
do
{
printf("Enter your choice: \n");
scanf("%d",&choice);
switch (choice)
{
case 1:
insert(a,p,&rear);
break;
case 2:
delete(a,p,&rear);
break;
case 3:
display(a,rear);
break;
case 0:
printf("Thanks for using the program");
break;
}
} while (choice!=0);
return 0;
#include <stdio.h>
#include<stdlib.h>
#include <stdbool.h>
#define N 5
a[rear]=x;
return rear;
}
else if(*rear==front)
*rear=front=-1;
else
front=(front+1)%N;
return front;
}
int main()
{
int a[N],rear=-1,front=-1;
int n,x;
while(1)
{
switch(n)
{
case 1:
printf("Enter a element ");
scanf("%d",&x);
rear=enqueue(a,rear,&front,x);
break;
case 2:
front=dequeue(a,&rear,front);
break;
case 3:
display(a,rear,front);
break;
case 4:
peek(a,front);
break;
case 5:
exit(0);
}
#include <stdio.h>
#include <ctype.h>
int x = stack[*top];
*top=*top-1;
return x;
}
return op1+op2;
if(x=='-')
return op1-op2;
int main()
{
char ch[20];
int stack[20];
printf("Enter the postfix expression : ");
scanf("%s",ch);
char *e=ch;
int top=-1;
while(*e!='\0')
{
if(isdigit(*e))
push(*e-'0',stack,&top);
else
{
int op2=pop(stack,&top);
int op1=pop(stack,&top);
int ans=operation(*e,op1,op2);
push(ans,stack,&top);
}
e++;
}
printf("ANS = %d ",stack[top]);
return 0;
}
#include<stdlib.h>
struct node
{
int value;
struct node *next;
};
if(ptr==NULL)
{
printf("Can't insert node.\n");
}
ptr->value=value;
ptr->next=NULL;
if(*head==NULL)
*head=ptr;
else
{
ptr->next=*head;
*head=ptr;
}
}
while(ptr!=NULL)
{
start=temp;
while(start->next!=NULL)
{
if(start->value>start->next->value)
{
swap(start,start->next);
}
start=start->next;
}
ptr=ptr->next;
}
printf("Sorting succesful.\n");
}
int main()
{
int n;
struct node *l1=NULL,*l2=NULL;
printf("How many nodes you want to enter in L1: ");
scanf("%d",&n);
for(int i=0;i<n;i++)
insert(&l1);
for(int i=0;i<n;i++)
insert(&l2);
display(l1);
display(l2);
sort(l1);
sort(l2);
merge(&l1,&l2);
printf("Sorted and merged list is: ") ;
display(l1);
return 0;
}
Q 13. Write C program to create a doubly link list by adding the node
right hand side and then check list is in palindrome form or not.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *pre;
struct node *next;
};
if(head==NULL)
{
head=temp;
}
else
{
struct node *temp1=head;
while(temp1->next!=NULL)
{
temp1=temp1->next;
}
temp1->next=temp;
temp->pre=temp1;
}
return head;
}
{
struct node *temp1=head;
if(head==NULL)
{
printf("\nlist is empty");
}
else
{
while(temp1!=NULL)
{
printf("%d ",temp1->data);
temp1=temp1->next;
}
printf("\n");
}
int flag=1;
while( (temp!=head) )
{
if(temp->data != head->data)
{
flag=0;
break;
}
temp=temp->pre;
head=head->next;
}
return flag;
}
int main()
{
int n;
for(int i=0;i<n;i++)
{
int x;
printf("\nEnter the value ");
scanf("%d",&x);
head=insert_at_tail(head,x);
}
display(head);
if( palindrome(head) )
printf("\nPALINDROME ");
else
printf("\nNON PALINDROME");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
if(head==NULL)
{
head=temp;
temp->next=head;
}
else
{
struct node *temp1=head;
while(temp1->next!=head)
{
temp1=temp1->next;
}
temp1->next=temp;
temp->next=head;
}
return head;
}
if(head==NULL)
{
printf("\nlist is empty");
}
else
{
while(temp1->next!=head)
{
printf("%d ",temp1->data);
temp1=temp1->next;
}
printf("%d ",temp1->data);
printf("\n");
}
}
int main()
{
int n;
printf("Enter the number of values you want to insert in circular linked list ");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
int x;
scanf("%d",&x);
head=insert_at_tail(head,x);
display(head);
return 0;
}