0% ont trouvé ce document utile (0 vote)
35 vues26 pages

Ds 2

Le document contient plusieurs programmes en C, chacun illustrant une structure de données ou un algorithme différent, tels que des piles, des files d'attente, des polynômes, des listes doublement chaînées, des recherches binaires, des tris, des arbres binaires et des parcours de graphes. Chaque programme est accompagné d'une sortie d'exemple qui montre son fonctionnement. Les programmes démontrent des concepts fondamentaux de la programmation et des structures de données.

Transféré par

abithalakshmisuresh14
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
35 vues26 pages

Ds 2

Le document contient plusieurs programmes en C, chacun illustrant une structure de données ou un algorithme différent, tels que des piles, des files d'attente, des polynômes, des listes doublement chaînées, des recherches binaires, des tris, des arbres binaires et des parcours de graphes. Chaque programme est accompagné d'une sortie d'exemple qui montre son fonctionnement. Les programmes démontrent des concepts fondamentaux de la programmation et des structures de données.

Transféré par

abithalakshmisuresh14
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF, TXT ou lisez en ligne sur Scribd

Program

#include<stdio.h>
#include<conio.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--];
}
int priority(char x){
if(x==’(‘)
return 0;
if(x==’+’ || x==’-‘)
return 1;
if(x== ‘x’ || x==’/’)
return 2;
return 0;
}
int main() {
char exp[100];
char *e,x;
printf(“Enter the expression:”);
scanf(“%s”,exp);
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);
} else {
while(priority(stack[top])>=priority(*e))
printf(“%c”,pop());
push(*e);
}
e++;
}
while(top!= -1)
printf(“%c”,pop());
getch();
return 0;
}
OUTPUT

Enter the expression: a+b*c-d


abc*+d-
PROGRAM

#include<stdio.h>
#include<conio.h>
#define MAX 5
int queue[MAX];
int front = -1 , rear = -1;
void enqueue(int value){
clrscr();
if(rear = = MAX-1){
printf(“Queue is full\n”);
} else if (front == -1){
front = 0;
rear=0;
queue[rear]=value;
} else {
rear = (rear + 1)% MAX;
queue[rear]=value;
}
}
void dequeue() {
if (front == -1) {
printf(“Queue is empty\n”);
} else {
printf(“Dequeued : %d\n”, queue[front]);
front = (front + 1) % Max;
}
}
Void printQueue() {
If(front = = -1) {
printf(“Queue is empty\n”);
} else {
int temp=front;
while(temp!=rear) {
printf(“%d”, queue[temp]);
temp=(temp+1) % MAX;
}
printf(“d\n”,queue[rear]);
}
}
int main() {
enqueue(1);
enqueue(2);
enqueue(3);
printQueue();
dequeue();
printQueue();
getch();
return 0;
}
OUTPUT:

123
Dequeued: 1
23
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
struct Node{
int coeff,exp;
Node* next;
Node* nullptr;
Node(int c, int e): coeff(c), exp(e),next(nullptr){}
};
void insert(Node* & head, int c, int e){
clrscr();
Node* newNode=new Node(c,e);
if(!head || head -> exp<e) {
newNode->next=head;
head=newNodee;
}else {
Node* temp = head;
while(temp ->next && temp->next-> exp>e)temp=temp->next;
if(temp->exp==e) temp->coeff+=c;
else {
newNode->next = temp->next;
temp->next = newNode;
}
}
}
Node* nullptr;
Node* next;
Node* addPoly(Node* p1, Node*p2) {
Node*result = nullptr;
while(p1 || p2) {
if(!p2 || (p1 && p1->exp>p2->exp)){
insert(result, p1->coeff, p1->exp), p1=p1=next;
}else if(!p1 || (p2 && p2 ->exp>p1->exp)){
insert(result , p2->coeff, p2->exp), p2=p2->next;
} else {
insert(result, p1->coeff + p2->coeff, p1->exp);
p1 = p1->next, p2=p2->next;
}
} return result;
}
void printPoly(Node* head) {
while(head) {
cout<<head->coeff<<”x”<<head->exp;
if(head->next)cout<<”+”;
head=head->next;
}
cout<<endl;
}
int main()
{
Node* poly1=nullptr,*poly2=nullptr;
insert(poly1, 3,2), insert(poly1, 5,1), insert(poly1, 4,3);
insert(poly2, 2,2), insert(poly2, 3,1), insert(poly2, 3,3);
Node*result=addPoly(poly1,poly2);
cout<<”Resultant Polynomial:”);
printPoly(result);
getch();
return 0; }
OUTPUT:

Resultant Polynomial : 7x3 + 5x2 + 8x1


PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct Node {
int data;
struct Node *prev, * next;
} Node;
Node* createNode(int data) {
Node* newNode= (Node*)malloc(sizeof(Node));
clrscr();
newNode->data = data;
newNode->prev = newNode->next = NULL;
return newNode;
}
void insertFront(Node** head, int data) {
Node* newNode = createNode(data);
newNode->next=*head;
if(*head)(*head)->prev=newNode;
*head=newNode;
}
Void insertEnd(Node** head, int data){
Node* newNode = createNode(data);
Node* temp;
if(!* head) {
* head =newNode;
return ;
}
temp=*head;
while(temp->next) temp= temp->next;
temp->next = newNode;
newNode->prev= temp;
}
void deleteNode(Node**head, int key) {
Node* temp= *head;
while(temp && temp->data!=key) temp = temp->next;
if(! temp) return;
if(temp->prev) temp->prev->next = temp->next;
else *head= temp->next;
if(temp->next) temp->next->prev = temp-> prev;
free(temp);
}
void display(Node* head) {
while(head) {
printf(“%d “,head->data);
head= head->next;
} printf(“\n”);
}
int main() {
Node* head = NULL;
insertFront(&head, 30);
insertFront(&head, 20);
insertFront(&head, 10);
insertEnd(&head, 40);
Display(head);
deleteNode(&head, 20);
display(head);
getch();
return 0; }
OUTPUT:

10 20 30 40
10 30 40
PROGRAM:

#include<stdio.h>
#include<conio.h>
void main() {
int n, i, search, f=0, low, high, mid, a[20];
clrscr();
printf(“Enter the n value:”);
scanf(“%d”, &n);
printf(“Enter the numbers in ascending order:\n”);
for(i=0; i<n; i++) {
printf(“a[%d]: “,i);
scanf(“%d”, &a[i]);
}
printf(“Enter the search element:”);
scanf(“%d”, &search);
low=0;
high=n-1;
while(low<=high) {
mid=(low+high)/2;
if(search<a[mid]) {
high=mid-1;
} else if(search>a[mid]) {
low=mid+1;
} else {
f=1;
printf(“Element found at position:%d\n”, mid+1);
break;
}
}
if(f==0) {
printf(“Element not present in the array.\n”);
}
getch();
}
OUTPUT:

Enter the n value: 5


Enter the numbers in ascending order:
A[0]: 2
A[1]: 4
A[2]: 6
A[3]:9
A[4]: 12
Enter the search element : 4
Element found at position : 2
PROGRAM:

#include<stdio.h>
#include<conio.h>
void bubblesort(int arrr[] , int n)
{
int i, j, temp;
clrscr();
for(i=0;i<n-1;i++){
for(j=0; j<n-i-1; j++) {
if(arr[j]>arr[j+1]) {
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
void selectionsort(int arr[], int n) {
int i, j, temp;
clrscr();
for(i=0; i<n-1; i++) {
int minIndex = i;
for(j=i+1; j<n; j++) {
if(arr[j]< arr[minIndex]) {
minIndex=j;
temp=arr[minIndex];
arr[minIndex]=arr[i];
}
}
}
}
void insertionsort(int arr[], int n) {
int i, j, key;
clrscr();
for(i=1; i<n; i++) {
key=arr[i];
j=i-1;
while(j>=0 && arr[j]> key) {
arr[j+1]=arr[j];
j--;
}
arr[j+1]=key;
}
}
void printarray(int arr[], int n) {
int i;
for(ii=0; i<n; i++)
printf(“\n %d\n”, arr[i]);
printf(“\n”);
}
int main() {
int n, i, choice;
int arr[100];
printf(“Enter the number of elements:”);
scanf(“%d”, &n);
printf(“Enter the elements: \n”);
for(i=0; i<n; i++)
scanf(“%d”, & arr[i]);
printf(“choose sorting algorithm 1. bubble 2. insertion 3. selection”);
scanf(“%d”, &choice);
if(choice ==1)
bubblesort(arr, n);
else if(choice==2)
insertionsort(arr,n);
else if(choice==3)
selectionsort(arr, n);
else {
printf(“Invalid choice! \n”);
return 1;
}
printf(“Sorted array:”);
printarray(arr, n);
getch();
return 0;
}
OUTPUT:

Enter the number of elements: 4


5
6
3
1
choose sorting algorithm 1. bubble 2. insertion 3. selection
3

Sorted array:
1
3
5
6
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node *left, *right;
};
struct Node* newNode(int data) {
struct Node* temp=(struct Node*)malloc(sizeof(struct Node));
clrscr();
temp->data=data;
temp->left=temp->right = NULL;
return temp;
}
struct Node* findMin(struct Node* root) {
while(root->left)root=root->left;
return root;
}
struct Node* deleteNode(struct Node* root, int key) {
if(!root)return NULL;
if(key<root->data)
root->left = deleteNode((root->left, key);
else if(key>root->data)
root->right=deleteNode(root->right, key);
else {
struct Node* temp;
if(!root->left)return root->right;
If(!root->right)return root->left;
temp=findMin(root->right);
root->data=temp->data;
root->right=deleteNode(root->right,temp->data);
}
return root;
}
void inorder(struct Node* root) {
if(root) {
inorder(root->left);
printf(“%d “,root->data);
inorder(root->right);
}
}
struct Node* insert(struct Node* root, int data) {
if(root)return newNode(data);
if(data<root->data)
root->left=insert(root->left,data);
else
root->right=insert(root->right,data);
return root;
}
int main() {
struct Node* root=NULL;
root=insert(root, 50);
root=insert(root, 30);
root=insert(root, 70);
printf(“Before Deletion:”);
inorder(root);
printf(“\n”);
root=deleteNode(root, 50);
printf(“After Deletion:”);
inorder(root);
printf(“\n”);
getch();
return 0;
}
OUTPUT:

Before Deletion: 30 50 70
After Deletion: 30 70
PROGRAM:

#include<stdio.h>
#include<conio.h>
#define MAX 10
int graph[MAX][MAX], visited[MAX],n;
int i;
void dfs(int v)
{
printf(“%d ”,v);
visited[v]=1;
For(i=0;i<n;i++)
if(graph[v][i] && !visited[i])
dfs(i);
}
void bfs(int start) {
int front =0; rear=0;
int i;
visited[start]=1;
queue[rear++]=start;
while(front<rear){
int v==queue[front++];
printf(“%d ”,v);
for(i=0; i<n; i++)
if(graph[v][i] && !visited[i]) {
visited[i]=1;
queue[rear++]=i;
}
}
}
int main() {
int edges, v1, v2, start;
int i;
scanf(“%d %d”, &n, &edges);
for(I=0; I<edges;I++) {
scanf(“%d %d”, &v1, &v2);
graph[v1][v2]=graph[v2][v1]=1;
}
scanf((“%d ”, &start);
printf(“DFS:”);
dfs(start);
for(i=0; i<n; i++) visited[I]=0;
printf(“\nBFS: “);
bfs(start);
getch();
return 0;
}
OUTPUT:

56
01
02
13
14
23
34
0
DFS: 0 1 3 2
BFS: 0 1 2 3 4

Vous aimerez peut-être aussi