Uploaded by negiaditya1234

5 6197482519290446935

advertisement
Term work
on
Data Structure with C
(PCS 302)
2021-22
Submitted to:
Submitted by:
Mr. Rakesh Patra
Aditya Negi
Assistant Professor
GEHU, Dehradun
University Roll No. :2018105
Class Roll No./Section: 07 /B
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
GRAPHIC ERA HILL UNIVERSITY, DEHRADUN
2
ACKNOWLEDGMENT
I would like to particularly thank my Data Structure with C Lab Faculty
Mr. Rakesh Patra for his patience, support and encouragement throughout the
completion of this Term work.
At last but not the least I greatly indebted to all other persons who directly
or indirectly helped me during this course.
Aditya Negi
University. Roll No.- 2018105
B.Tech CSE-B-III Sem
Session: 2021-22
GEHU, Dehradun
Name- Shikhar dhyani
Roll Number:50
Section-A
3
Table of Contents
Program
No.
1.
Program Name
Page No
4.
Write a the C program to create an array by inserting N elements in it
then find second non repeating element from the array.
Write a the C program to create an array by inserting N elements in it
then find third repeating element from the array.
Write a C program Create a Dynamic array and then Reverse the array
using recursion and then finally print the array.
Write a C Program implement STACK using array in menu driven form
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.
Write a C Program implement STACK using Link List in menu driven
form.
Write a C Program implement QUEUE using Link List in menu driven
form.
Write a C Program implement priority QUEUE using array in menu
driven form.
Write a C Program implement QUEUE using array in menu driven
form.
Write a C program to Evaluate Postfix Expression using Stack
2.
3.
7.
8.
9.
10.
11.
12.
13.
14.
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]
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.
Write a C program to create a circular link list by adding the nodes in
right hand side and then print the list.
Name- Shikhar dhyani
Roll Number:50
Section-A
4
1. Write a C program to create an array by inserting N elements in it then find
second non repeating element from the array.
Source Code :
#include<stdio.h>
#include<limits.h>
int
main ()
{
int n, c = 0, flag = INT_MAX;
printf ("Enter No. of elements : ");
scanf ("%d", &n);
int arr[n];
printf ("Enter elements : \n");
for (int i = 0; i < n; i++)
scanf ("%d", &arr[i]);
for (int i = 0; i < n; i++)
{
int flagc = 0;
for (int j = i; j < n; j++)
{
if (arr[i] == arr[j] && i != j)
{
arr[j] = flag;
flagc++;
}
}
if (flagc != 0)
arr[i] = flag;
}
int ind = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] != flag)
Name- Shikhar dhyani
Roll Number:50
Section-A
5
ind++;
if (ind == 2)
{
ind = i;
break;
}
else if (i == n - 1)
ind = flag;
}
if (ind != flag)
printf ("%d is 2nd non repeating element ", arr[ind]);
else
printf ("non repeating not available");
}
Output
Name- Shikhar dhyani
Roll Number:50
Section-A
6
2. Write a the C program to create an array by inserting N elements in it then
find third repeating element from the array.
Source Code :
#include<stdio.h>
int thirdrepeat (int a[], int n, int k)
{
int c = 0;
int in;
int arr[n];
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] == a[j])
{
arr[i] = -1;
a[j] = 0;
c = 1;
}
if (c == 0)
arr[i] = a[i];
}
}
for (int i = 0; i < n; i++)
{
if (arr[i] == -1)
{
in = i;
k--;
if (k == 0)
return in;
}
}
}
int main ()
Name- Shikhar dhyani
Roll Number:50
Section-A
7
{
int n;
printf ("Enter No. of elements : ");
scanf ("%d", &n);
int a[n];
printf ("Enter elements : \n");
for (int i = 0; i < n; i++)
scanf ("%d", &a[i]);
int ans = thirdrepeat (a, n, 3);
printf ("third repeat element in array==>%d", a[ans]);
}
Output
Name- Shikhar dhyani
Roll Number:50
Section-A
8
3.Write a C program create a Dynamic array and then Reverse the array using
recursion and then finally print the array.
Source Code :
#include<stdio.h>
#include<stdlib.h>
void swap (int *array, int leftIndex, int rightIndex)
{
int temp;
temp = array[leftIndex];
array[leftIndex] = array[rightIndex];
array[rightIndex] = temp;
}
void reverse (int *array, int leftIndex, int rightIndex){
if (NULL == array){
printf ("Invalid Input");
return;
}
if (leftIndex < rightIndex){
swap (array, leftIndex, rightIndex);
reverse (array, leftIndex + 1, rightIndex - 1);
}
int main (){
int n;
printf ("Enter Size of the array : ");
scanf ("%d", &n);
int *arr = malloc (n * sizeof (int));
printf ("Enter elements in the array : \n");
for (int i = 0; i < n; i++)
scanf ("%d", &arr[i]);
reverse (arr, 0, n - 1);
printf ("\nReverse Array is : \n");
for (int i = 0; i < n; i++)
printf ("%d ", arr[i]);
free (arr);
return 0;
}
Name- Shikhar dhyani
Roll Number:50
Section-A
9
Output
Name- Shikhar dhyani
Roll Number:50
Section-A
10
4. Write a C program implement STACK using array in menu driven form.
Source Code :
#include<stdbool.h>
#include<stdio.h>
#define n 5
int arr[n];
int ind=-1;
int top(){
return arr[ind];
}
void pop(){
if(ind!=-1)
ind--;
}
void push(int num){
if(ind<n)
arr[++ind]=num;
}
int size(){
return ind+1;
}
bool isempty(){
if(size())
return false;
return true;
}
void functions(){
Name- Shikhar dhyani
Roll Number:50
Section-A
11
int choice,num;
printf("1.push 2.pop 3.top 4.size 5.isempty 6.exit\nEnter your choice : ");
scanf("%d",&choice);
bool emp;
switch(choice){
case 1 :
scanf("%d",&num);
push(num);
functions();
break;
case 2: pop();
functions();
break;
case 3: num=top();
printf("%d\n",num);
functions();
break;
case 4: num=size();
printf("%d\n",num);
functions();
break;
case 5: emp=isempty();
printf("%d\n",emp);
functions();
break;
case 6: return ;
}
}
int main(){
functions();
return 0;
}
Name- Shikhar dhyani
Roll Number:50
Section-A
12
Output
5. Write a C program to convert Infix to Postfix Expression using Stack.
Source Code :
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
Name- Shikhar dhyani
Roll Number:50
Section-A
13
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}
int main()
{
char exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++; }
while(top != -1)
printf("%c ",pop());
return 0;
}
Name- Shikhar dhyani
Roll Number:50
Section-A
14
Output
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.
Source Code :
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void deleteAlt(struct Node *head)
{
if (head == NULL)
return;
Name- Shikhar dhyani
Roll Number:50
Section-A
15
struct Node *prev = head;
struct Node *node = head->next;
while (prev != NULL && node != NULL)
{
prev->next = node->next;
free(node);
prev = prev->next;
if (prev != NULL)
node = prev->next;
}
}
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;
}
void printList(struct Node *node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
int main()
{
struct Node* head = NULL;
push(&head, 10);
push(&head, 9);
push(&head, 8);
push(&head, 7);
push(&head, 6);
Name- Shikhar dhyani
Roll Number:50
Section-A
16
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
printf("List before deleting Alternate Nodes : \n");
printList(head);
deleteAlt(head);
printf("\n\nList after deleting Alternate Node : \n");
printList(head);
return 0;
}
Output
Name- Shikhar dhyani
Roll Number:50
Section-A
17
Name- Shikhar dhyani
Roll Number:50
Section-A
18
7. Write a C Program implement STACK using Linked List in menu driven
form.
Source Code :
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;
void main ()
{
int choice=0;
while(choice != 4)
{
printf("\n\nChose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
Name- Shikhar dhyani
Roll Number:50
Section-A
19
}
case 4:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid choice : ");
}
};
}
}
void push ()
{
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("Not able to push the element.");
}
else
{
printf("Enter the value : ");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;
}
printf("Item pushed.");
}
}
void pop()
Name- Shikhar dhyani
Roll Number:50
Section-A
20
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements :\n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}
Name- Shikhar dhyani
Roll Number:50
Section-A
21
Output
Name- Shikhar dhyani
Roll Number:50
Section-A
22
8. Write a C Program implement QUEUE using Linked List in menu driven
form.
Source Code :
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("Enter your choice : ");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
printf("Exiting the program!\n");
break;
default:
Name- Shikhar dhyani
Roll Number:50
Section-A
23
printf("\nEnter valid choice : ");
}
}
}
void insert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value : ");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
Name- Shikhar dhyani
Roll Number:50
Section-A
24
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nQueue is Empty!\n");
}
else
{ printf("\nPrinting values ...\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}
Name- Shikhar dhyani
Roll Number:50
Section-A
25
Output
9. Write a C Program implement priority QUEUE using array in menu driven
form.
Source Code :
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
void insert_by_priority(int);
void delete_by_priority(int);
void create();
void check(int);
void display_pqueue();
int pri_que[MAX];
int front, rear;
Name- Shikhar dhyani
Roll Number:50
Section-A
26
void main()
{
int n, ch;
printf("\n1 - Insert an element into queue");
printf("\n2 - Delete an element from queue");
printf("\n3 - Display queue elements");
printf("\n4 - Exit");
create();
while (1)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter value to be inserted : ");
scanf("%d",&n);
insert_by_priority(n);
break;
case 2:
printf("\nEnter value to delete : ");
scanf("%d",&n);
delete_by_priority(n);
break;
case 3:
display_pqueue();
break;
case 4:
printf("Exiting the program !\n");
exit(0);
default:
printf("\nChoice is incorrect, Enter a correct choice");
}
}
}
void create()
{
front = rear = -1;
}
void insert_by_priority(int data)
{
Name- Shikhar dhyani
Roll Number:50
Section-A
27
if (rear >= MAX - 1)
{
printf("\nQueue overflow no more elements can be inserted");
return;
}
if ((front == -1) && (rear == -1))
{
front++;
rear++;
pri_que[rear] = data;
return;
}
else
check(data);
rear++;
}
void check(int data)
{
int i,j;
for (i = 0; i <= rear; i++)
{
if (data >= pri_que[i])
{
for (j = rear + 1; j > i; j--)
{
pri_que[j] = pri_que[j - 1];
}
pri_que[i] = data;
return;
}
}
pri_que[i] = data;
}
void delete_by_priority(int data)
{
int i;
if ((front==-1) && (rear==-1))
{
printf("\nQueue is empty no elements to delete");
return;
Name- Shikhar dhyani
Roll Number:50
Section-A
28
}
for (i = 0; i <= rear; i++)
{
if (data == pri_que[i])
{
for (; i < rear; i++)
{
pri_que[i] = pri_que[i + 1];
}
pri_que[i] = -99;
rear--;
if (rear == -1)
front = -1;
return;
}
}
printf("\n%d not found in queue to delete", data);
}
void display_pqueue()
{
if ((front == -1) && (rear == -1))
{
printf("\nQueue is empty");
return;
}
for (; front <= rear; front++)
{
printf(" %d ", pri_que[front]);
}
front = 0;
}
Name- Shikhar dhyani
Roll Number:50
Section-A
29
Output
10. Write a C program implement QUEUE using array in menu driven form.
Source Code :
#include<stdio.h>
#include<stdlib.h>
#define Max 5
int rear = -1;
int front = -1;
int queue[Max];
void Enqueue(int data)
{
if(rear == Max-1)
{
printf("Queue is Full");
return;
}
if(front == -1 )
front = 0;
rear++;
Name- Shikhar dhyani
Roll Number:50
Section-A
30
queue[rear] = data;
}
int Dequeue()
{
int item;
if(front == -1 || front == rear + 1)
{
printf("Queue is Empty");
exit(1);
}
else
{
item = queue[0];
front = front + 1;
return item;
}
}
void peek()
{
printf("Top Element is %d ",queue[front]);
}
void display()
{
for( int i = front ; i<=rear; i++)
{
printf("data -> %d \n",queue[i]);
}
}
int main()
{
int data;
int choice, del;
while(1)
{
printf("\n1. Enqueue");
printf("\n2. Dequeue");
printf("\n3. Peek");
printf("\n4. Display");
Name- Shikhar dhyani
Roll Number:50
Section-A
31
printf("\n5. Exit ");
printf("\nEnter Your Choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\nEnter Data : ");
scanf("%d", &data);
Enqueue(data);
break;
case 2:
del = Dequeue();
printf("\nDeleted data is %d ",del);
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
printf("Exiting the program!\n");
return 0;
}
}
return 0;
}
Output
Name- Shikhar dhyani
Roll Number:50
Section-A
32
11. Write a C program to evaluate Postfix Expression using Stack.
Source Code :
#include<stdio.h>
#include<ctype.h>
int stack[20];
int top = -1;
Name- Shikhar dhyani
Roll Number:50
Section-A
33
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression : ");
scanf("%s", exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
Name- Shikhar dhyani
Roll Number:50
Section-A
34
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of postfix expression %s = %d\n\n", exp, pop());
return 0;
}
Output
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]
Name- Shikhar dhyani
Roll Number:50
Section-A
35
Source Code :
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node * next;
};
void create(struct Node **head)
{
struct Node *newNode,*temp;
int choice =1;
while(choice)
{
newNode = (struct Node*)malloc (sizeof( struct Node));
printf("\nEnter the data");
scanf("%d",&newNode->data);
newNode->next=0;
if(*head ==NULL)
*head =temp = newNode;
else
{
temp->next = newNode;
temp = newNode;
}
printf("\nEnter 1 to continue or 0 to finish data entry :");
scanf("%d",&choice);
}
}
void Display(struct Node *head)
{
struct Node *temp;
temp =head;
while(temp)
{
printf("%d ",temp->data);
temp= temp->next;
}
Name- Shikhar dhyani
Roll Number:50
Section-A
36
}
void sortList(struct Node **head)
{
if (*head == NULL)
return;
sortList(&((*head)->next));
struct Node *temp = *head, *headreturn = *head;
while (temp->next != NULL && (*head)->data > temp->next->data)
{
temp = temp->next;
headreturn = (*head)->next;
if (temp->next == NULL || (*head)->data <= temp->next->data)
{
(*head)->next = temp->next;
temp->next = *head;
break;
}
}
*head = headreturn;
}
void mergeList(struct Node **head1,struct Node **head2)
{
struct Node *temp1 = *head1,*temp2 = *head2;
while(temp1->next)
temp1 = temp1->next;
temp1->next = temp2;
}
int main()
{
struct Node *head1=NULL, *head2=NULL;
printf("Enter the data for list L1\n");
create(&head1);
printf("\nEnter the data for list L2\n");
create(&head2);
sortList(&head1);
sortList(&head2);
mergeList(&head1,&head2);
Display(head1);
return 0;
}
Output
Name- Shikhar dhyani
Roll Number:50
Section-A
37
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.
Source Code :
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;};
void create(struct Node**head,struct Node**temp)
Name- Shikhar dhyani
Roll Number:50
Section-A
38
{
struct Node*ptr;
int choice=1;
while(choice)
{
ptr=(struct Node*)malloc(sizeof(struct Node));
printf("Enter the data ");
scanf("%d",&ptr->data);
ptr->next=NULL;
ptr->prev=NULL;
if(*head==NULL && *temp==NULL)
*head=*temp=ptr;
else
{
(*temp)->next=ptr;
ptr->prev=*temp;
*temp=ptr;
}
printf("\n Enter 1 to continue or 0 to finish entry");
scanf("%d",&choice);
}}
void check_palindrome(struct Node**head)
{
struct Node *temp=(*head);
while(temp->next!=NULL)
temp=temp->next;
while(*head!=temp && (*head)->prev != temp->next)
{
if((*head)->data!=temp->data)
{
*head=(*head)->next;
temp=temp->prev;
}
else
{
printf("List is not palindrome");
return;
}
}
printf("List is a palindrome");
}
void Display(struct Node *head)
{
while (head != NULL)
{
printf("%d ", head->data);
head = head->next;
Name- Shikhar dhyani
Roll Number:50
Section-A
39
}
}
int main()
{
struct Node *head=NULL,*temp=NULL;
create(&head,&temp);
Display(head);
printf("\n");
check_palindrome(&head);
}
Output:
14. Write a C program to create a circular linked list by adding the nodes in
right hand side and then print the list.
Source Code :
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *next;
};
Name- Shikhar dhyani
Roll Number:50
Section-A
40
void push(struct node **head, int val)
{
struct node *newNode = malloc(sizeof(struct node));
newNode->info = val;
newNode->next = NULL;
if (*head == NULL)
{
*head = newNode;
newNode->next = *head;
}
else
{
struct node *lastNode = *head;
while (lastNode->next != *head)
{
lastNode = lastNode->next;
}
lastNode->next = newNode;
newNode->next = *head;
}
}
void print(struct node *ptr)
{
struct node *temp = ptr;
do
{
printf("%d ", temp->info);
temp = temp->next;
} while (temp != ptr);
}
int main()
{
struct node *start = NULL;
int t=1;
while(t){
printf("Enter the data ");
scanf("%d",&t);
push(&start, t);
printf("\n Enter 1 to continue or 0 to finish entry");
scanf("%d",&t);
}
printf("Circular linked list AFTER adding the nodes in right hand side...\n\n");
print(start);
}
Name- Shikhar dhyani
Roll Number:50
Section-A
41
Output
Name- Shikhar dhyani
Roll Number:50
Section-A
Related documents
Download