KEMBAR78
Program 2 and 10 | PDF | C++ | Computing
0% found this document useful (0 votes)
15 views2 pages

Program 2 and 10

Cs programs

Uploaded by

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

Program 2 and 10

Cs programs

Uploaded by

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

#include <stdio.h>#include <stdlib.

h>struct node{ int info;


struct node *llink; struct node *rlink;};typedef struct node *NODE;NODE
getnode(){ NODE x; x = (NODE)malloc(sizeof(struct node)); if(x
== NULL) { printf("Not enough memory\n");
exit(0); } return x;}int search(int item, NODE root){ NODE
cur; if (root == NULL) return 0; cur = root; while (cur !=
NULL) { if (item == cur->info) return 1; if
(item < cur->info) cur = cur->llink; else
cur = cur->rlink; } return 0;}NODE insert(int item, NODE root){
NODE temp, cur, prev; temp = getnode(); temp->info = item;
temp->llink = temp->rlink = NULL; if (root == NULL) return temp; cur
= root; while (cur != NULL) { prev = cur;
if (item < cur->info) cur = cur->llink; else
cur = cur->rlink; } if (item < prev->info) prev->llink
= temp; else prev->rlink = temp; return root;}void
preorder(NODE root){ if (root == NULL) return; printf("%d ",root-
>info); preorder(root->llink); preorder(root->rlink);}void
inorder(NODE root){ if (root == NULL) return; inorder(root->llink);
printf("%d ",root->info); inorder(root->rlink);}void postorder(NODE root){
if (root == NULL) return; postorder(root->llink); postorder(root-
>rlink); printf("%d ",root->info);}int main(){ int item, choice,
flag; NODE root; root = NULL; for(;;) {
printf("1:Insert 2:Preorder\n"); printf("3:Inorder 4:Postorder\
n"); printf("5:Search 6:Exit : "); scanf("%d",
&choice); switch(choice) {
case 1: printf("Enter the item : ");
scanf("%d", &item); root = insert(item, root);
break; case 2: if (root ==
NULL) {
printf("Empty"); break;
} printf("Preorder : ");
preorder(root); printf("\n");
break; case 3: if (root ==
NULL) {
printf("Empty"); break;
} printf("inorder : ");
inorder(root); printf("\n");
break; case 4: if (root ==
NULL) {
printf("Empty"); break;
} printf("postorder : ");
postorder(root); printf("\n");
break; case 5: printf("Enter
the item to be searched : "); scanf("%d", &item);
flag = search(item, root); if (flag == 0)
printf("Item not found\n"); else
printf("Item found\n"); break;
default: exit(0); } }}#include
<stdlib.h>int my_strlen(char s[]){ int i; i = 0; while (s[i] !
= '\0') i++; return i;}int search(char p[], char t[]){ int n, m, i,
j; n = my_strlen(t); m = my_strlen(p); for (i = 0; i <= n - m;
i++) { j = 0; while (j < m && p[j] == t[i +
j]) j++; if (j == m) return i; }
return -1;}void replace(char p[], char t[], char r[], int pos){ int i, k;
char d[30]; for (k = 0; k < pos; k++) d[k] = t[k]; for (i = 0; i <
my_strlen(r); i++) { d[k] = r[i]; k++;
} pos += my_strlen(p); for (i = pos; i <= my_strlen(t); i++)
{ d[k] = t[i]; k++; } for (i = 0; i <=
my_strlen(d); i++) t[i] = d[i];}int main(){ char t[30],
p[30], r[30]; int pos; printf("STR : "); scanf("%[^\n]", t);
printf("PTR : "); scanf("%s", p); printf("REP : "); scanf("%s",
r); pos = search(p, t); if (pos == -1) {
printf("Pattern not found\n"); exit(0); } for (;;)
{ replace(p, t, r, pos); pos = search(p, t);
if (pos == -1) break; } printf("FINAL : %s\n", t);}

You might also like