├── programList.pdf ├── IV Sem Core DS & RDBMS.pdf ├── APPEND.C ├── SEARCH2D.C ├── LINSEARC.C ├── INSSORT.C ├── SELSORT.C ├── BUBBSORT.C ├── PATTERNMATCHING.C ├── EXCHANGESORT.C ├── Reverse using pointer.c ├── SORTSTR.C ├── BINSEARC.C ├── SPARSE.C ├── POSTFIX.C ├── BSERREC.C ├── QUIKSORT.C ├── POLYLIN1.C ├── STACKARR.C ├── CIRQUEUE.C ├── QUEUEARR.C ├── STACKLIN.C ├── QUEUELIN.C ├── POLYARR1.C ├── LISTSORT.C ├── BSTSEARC.C ├── SPARSE1.C ├── TREEREC.C ├── LINKEDLI.C ├── DHEADLIS.C ├── HEADLIST.C ├── DOUBLLIS.C └── TREENREC.C /programList.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohammedmishalk/Data-Structure-Lab/HEAD/programList.pdf -------------------------------------------------------------------------------- /IV Sem Core DS & RDBMS.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohammedmishalk/Data-Structure-Lab/HEAD/IV Sem Core DS & RDBMS.pdf -------------------------------------------------------------------------------- /APPEND.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { 5 | int a[10],b[10],i,j,m,n; 6 | clrscr(); 7 | printf("Enter the size of first array: "); 8 | scanf("%d",&m); 9 | printf("\nEnter %d elements into first array:\n",m); 10 | for(i=0;i 2 | #include 3 | void main() 4 | { 5 | int a[10][10],i,j,m,n,x,count=0; 6 | clrscr(); 7 | printf("\nEnter the Row and column : "); 8 | scanf("%d%d",&m,&n); 9 | printf("\nEnter %d elements : ",m*n); 10 | for(i=0;i 2 | #include 3 | 4 | int search(int [],int,int); 5 | 6 | void main() 7 | { 8 | int a[10],i,n,x,loc; 9 | clrscr(); 10 | printf("Enter the size : "); 11 | scanf("%d",&n); 12 | printf("\nEnter %d elements into the array:\n",n); 13 | for(i=0;i 2 | #include 3 | 4 | void sort(int*,int); 5 | 6 | void main() 7 | { 8 | int a[10],n,i; 9 | clrscr(); 10 | printf("Enter the size : "); 11 | scanf("%d",&n); 12 | printf("\nEnter %d elements into the array :\n",n); 13 | for(i=0;i=0;j--) 32 | a[j+1]=a[j]; 33 | a[j+1]=x; 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /SELSORT.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void sort(int*,int); 5 | 6 | void main() 7 | { 8 | int a[10],n,i; 9 | clrscr(); 10 | printf("Enter the size : "); 11 | scanf("%d",&n); 12 | printf("\nEnter %d elements into the array :\n",n); 13 | for(i=0;i 2 | #include 3 | 4 | void sort(int*,int); 5 | 6 | void main() 7 | { 8 | int a[10],n,i; 9 | clrscr(); 10 | printf("Enter the size : "); 11 | scanf("%d",&n); 12 | printf("\nEnter %d elements into the array :\n",n); 13 | for(i=0;ia[j+1]) 33 | { 34 | temp=a[j]; 35 | a[j]=a[j+1]; 36 | a[j+1]=temp; 37 | swapped=1; 38 | } 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /PATTERNMATCHING.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | void main() 5 | { 6 | char text[20],pat[20]; 7 | int a,b,flag=0; 8 | printf("Enter the string : "); 9 | gets(text); 10 | printf("Enter the pattern to find : "); 11 | gets(pat); 12 | clrscr(); 13 | 14 | a = strlen(pat); 15 | b = strlen(text); 16 | 17 | for (int i = 0; i <= b - a; i++) { 18 | int j; 19 | 20 | for (j = 0; j < a; j++) 21 | if (text[i + j] != pat[j]) 22 | break; 23 | 24 | if (j == a) 25 | { 26 | printf("Pattern found at position %d \n", i+1); 27 | flag++; 28 | } 29 | } 30 | if (flag==0) 31 | printf("Not found"); 32 | 33 | getch(); 34 | } 35 | -------------------------------------------------------------------------------- /EXCHANGESORT.C: -------------------------------------------------------------------------------- 1 | #include 2 | ​#​include​<​conio.h​> 3 | void main(void) 4 | { 5 | int array[5], i, j, temp,n; 6 | ​   ​clrscr​(); 7 | printf("Enter the number of elements : "); 8 | scanf("%d",&n); 9 | printf("Enter %d numbers : ",n); 10 | for (i = 0; i < n; i++) 11 | { 12 | scanf("%d",&array[i]); 13 | } 14 | for(i = 0; i < (n -1); i++) 15 | { 16 | for (j=(i + 1); j < n; j++) 17 | { 18 | if (array[i] > array[j]) 19 | { 20 | temp = array[i]; 21 | array[i] = array[j]; 22 | array[j] = temp; 23 | } 24 | } 25 | } 26 | printf("Sorted array is : "); 27 | for (i = 0; i < n; i++) 28 | { 29 | printf(" %d ",array[i]); 30 | } 31 | getch(); 32 | } 33 | -------------------------------------------------------------------------------- /Reverse using pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | void str(char *str); 6 | void str(char *str) 7 | 8 | { 9 | 10 | int l, i; 11 | 12 | char *beg, *end, ch; 13 | 14 | l = strlen(str); 15 | 16 | beg= str; 17 | 18 | end= str; 19 | 20 | 21 | for (i = 0; i < l - 1; i++) 22 | 23 | end++; 24 | 25 | 26 | for (i = 0; i < l / 2; i++) { 27 | 28 | ch = *end; 29 | 30 | *end = *beg; 31 | 32 | *beg= ch; 33 | 34 | 35 | beg++; 36 | 37 | end--; 38 | 39 | } 40 | 41 | } 42 | 43 | 44 | void main() 45 | 46 | { 47 | 48 | char str1[100] ; 49 | 50 | printf("Enter a string:"); 51 | gets(str1); 52 | str(str1); 53 | printf("Reverse of the string: %s\n", str1); 54 | getch(); 55 | } 56 | -------------------------------------------------------------------------------- /SORTSTR.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void sort(char [][15],int); 6 | 7 | void main() 8 | { 9 | char list[10][15]; 10 | int i,n; 11 | clrscr(); 12 | printf("\nEnter number of strings : "); 13 | scanf("%d",&n); 14 | printf("\nEnter %d strings:\n",n); 15 | flushall(); 16 | for(i=0;i0) 33 | { 34 | strcpy(temp,str[j]); 35 | strcpy(str[j],str[j+1]); 36 | strcpy(str[j+1],temp); 37 | swapped=1; 38 | } 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /BINSEARC.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int search(int [],int,int); 5 | 6 | void main() 7 | { 8 | int a[10],i,n,x,loc; 9 | clrscr(); 10 | printf("Enter the size : "); 11 | scanf("%d",&n); 12 | printf("\nEnter %d elements into the array in ascending order:\n",n); 13 | for(i=0;i 2 | #include 3 | 4 | void main() 5 | { 6 | struct {int r,c,v;} terms[10]; 7 | int rows,cols,n,i,j,k; 8 | clrscr(); 9 | printf("Enter the order of the sparse matrix : "); 10 | scanf("%d%d",&rows,&cols); 11 | printf("\nEnter the number of non zero elements : "); 12 | scanf("%d",&n); 13 | printf("\nEnter the details of %d non zero elements rowwise:\n",n); 14 | for(i=0;i 2 | #include 3 | 4 | int evaluate(char*); 5 | 6 | void main() 7 | { 8 | char exp[15]; 9 | clrscr(); 10 | printf("Enter an expression in postfix form : "); 11 | gets(exp); 12 | printf("\nResult = %d",evaluate(exp)); 13 | getch(); 14 | } 15 | 16 | int evaluate(char *str) 17 | { 18 | int stack[15],top=0,op1,op2,i; 19 | for(i=0;str[i]!='\0';i++) 20 | { 21 | if(str[i]>='0' && str[i]<='9') 22 | stack[top++]=str[i]-'0'; 23 | else 24 | { 25 | op2=stack[--top]; 26 | op1=stack[--top]; 27 | switch(str[i]) 28 | { 29 | case '+': 30 | stack[top++]=op1+op2; 31 | break; 32 | case '-': 33 | stack[top++]=op1-op2; 34 | break; 35 | case '*': 36 | stack[top++]=op1*op2; 37 | break; 38 | case '/': 39 | stack[top++]=op1/op2; 40 | break; 41 | default:top+=2; 42 | } 43 | } 44 | } 45 | return stack[--top]; 46 | } 47 | -------------------------------------------------------------------------------- /BSERREC.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int search(int [],int,int); 5 | int binsearch(int [],int,int,int); 6 | 7 | void main() 8 | { 9 | int a[10],i,n,x,loc; 10 | clrscr(); 11 | printf("Enter the size : "); 12 | scanf("%d",&n); 13 | printf("\nEnter %d elements into the array in ascending order:\n",n); 14 | for(i=0;iub) return -1; 32 | else if(a[middle]==v) return middle; 33 | else 34 | { 35 | if(v 2 | #include 3 | 4 | void sort(int*,int); 5 | void qsort(int*,int,int); 6 | 7 | void main() 8 | { 9 | int a[10],n,i; 10 | clrscr(); 11 | printf("Enter the size : "); 12 | scanf("%d",&n); 13 | printf("\nEnter %d elements into the array :\n",n); 14 | for(i=0;i=ub) return; 35 | c=a[lb]; 36 | up=lb; 37 | down=ub; 38 | while(1) 39 | { 40 | while(a[up]<=c && upc) down--; 42 | if(up>=down) break; 43 | temp=a[up]; 44 | a[up]=a[down]; 45 | a[down]=temp; 46 | } 47 | a[lb]=a[down]; 48 | a[down]=c; 49 | qsort(a,lb,down-1); 50 | qsort(a,down+1,ub); 51 | } 52 | 53 | -------------------------------------------------------------------------------- /POLYLIN1.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct node* polynomial; 6 | 7 | struct node 8 | { 9 | int coef,exp; 10 | polynomial next; 11 | }; 12 | 13 | void main() 14 | { 15 | polynomial p,t; 16 | int i,n; 17 | clrscr(); 18 | printf("Number of terms in the polynomial : "); 19 | scanf("%d",&n); 20 | printf("\nEnter %d terms for the polynomial:\n",n); 21 | p=(polynomial)malloc(sizeof(*p)); 22 | printf("\nCoefficient : "); 23 | scanf("%d",&p->coef); 24 | printf("Exponent : "); 25 | scanf("%d",&p->exp); 26 | p->next=NULL; 27 | t=p; 28 | for(i=0;inext=(polynomial)malloc(sizeof(*p)); 31 | t=t->next; 32 | printf("\nCoefficient : "); 33 | scanf("%d",&t->coef); 34 | printf("Exponent : "); 35 | scanf("%d",&t->exp); 36 | t->next=NULL; 37 | } 38 | printf("\nThe polynomial : "); 39 | if(p->exp==0) printf("%d",p->coef); 40 | else if(p->exp==1) printf("%dx",p->coef); 41 | else printf("%dx^%d",p->coef,p->exp); 42 | for(t=p->next;t!=NULL;t=t->next) 43 | { 44 | if(t->coef>0) printf("+"); 45 | if(t->exp==0) printf("%d",t->coef); 46 | else if(t->exp==1) printf("%dx",t->coef); 47 | else printf("%dx^%d",t->coef,t->exp); 48 | } 49 | getch(); 50 | } 51 | -------------------------------------------------------------------------------- /STACKARR.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define LENGTH 5 5 | 6 | int stack[LENGTH]; 7 | int top=0; 8 | 9 | void push(int); 10 | void pop(); 11 | void list(); 12 | 13 | void main() 14 | { 15 | int ch,n; 16 | do 17 | { 18 | clrscr(); 19 | printf("\n\tSTACK OPERATIONS\n"); 20 | printf("\n\t\t1.PUSH"); 21 | printf("\n\t\t2.POP"); 22 | printf("\n\t\t3.LIST"); 23 | printf("\n\t\t4.EXIT"); 24 | printf("\n\n\tEnter your choice(1-4) : "); 25 | scanf("%d",&ch); 26 | switch(ch) 27 | { 28 | case 1: 29 | printf("\nEnter the element : "); 30 | scanf("%d",&n); 31 | push(n); 32 | break; 33 | case 2: 34 | pop(); 35 | break; 36 | case 3: 37 | list(); 38 | break; 39 | default:continue; 40 | } 41 | getch(); 42 | }while(ch!=4); 43 | } 44 | 45 | void push(int x) 46 | { 47 | if(top==LENGTH) 48 | { 49 | printf("\nSorry, stack is full..."); 50 | return; 51 | } 52 | stack[top++]=x; 53 | printf("\n%d is added...",x); 54 | } 55 | void pop() 56 | { 57 | if(top==0) 58 | { 59 | printf("\nSorry, stack is empty..."); 60 | return; 61 | } 62 | printf("\n%d is removed...",stack[--top]); 63 | } 64 | void list() 65 | { 66 | int i; 67 | if(top==0) 68 | { 69 | printf("\nSorry, stack is empty..."); 70 | return; 71 | } 72 | printf("\nThe stack :\n"); 73 | for(i=top-1;i>=0;i--) 74 | printf("\n%d",stack[i]); 75 | } 76 | 77 | -------------------------------------------------------------------------------- /CIRQUEUE.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define LENGTH 5 5 | 6 | int queue[LENGTH]; 7 | int front=0,rear=0; 8 | 9 | void add(int); 10 | void del(); 11 | void list(); 12 | 13 | void main() 14 | { 15 | int ch,n; 16 | do 17 | { 18 | clrscr(); 19 | printf("\n\tQUEUE OPERATIONS\n"); 20 | printf("\n\t\t1.ADD"); 21 | printf("\n\t\t2.DELETE"); 22 | printf("\n\t\t3.LIST"); 23 | printf("\n\t\t4.EXIT"); 24 | printf("\n\n\tEnter your choice(1-4) : "); 25 | scanf("%d",&ch); 26 | switch(ch) 27 | { 28 | case 1: 29 | printf("\nEnter the element : "); 30 | scanf("%d",&n); 31 | add(n); 32 | break; 33 | case 2: 34 | del(); 35 | break; 36 | case 3: 37 | list(); 38 | break; 39 | default:continue; 40 | } 41 | getch(); 42 | }while(ch!=4); 43 | } 44 | 45 | void add(int x) 46 | { 47 | if((rear+1)%LENGTH==front) 48 | { 49 | printf("\nSorry, queue is full..."); 50 | return; 51 | } 52 | rear=(rear+1)%LENGTH; 53 | queue[rear]=x; 54 | printf("\n%d is added...",x); 55 | } 56 | void del() 57 | { 58 | if(front==rear) 59 | { 60 | printf("\nSorry, queue is empty..."); 61 | return; 62 | } 63 | front=(front+1)%LENGTH; 64 | printf("\n%d is removed...",queue[front]); 65 | } 66 | void list() 67 | { 68 | int i; 69 | if(front==rear) 70 | { 71 | printf("\nSorry, queue is empty..."); 72 | return; 73 | } 74 | printf("\nThe queue :\n\n"); 75 | i=front; 76 | do 77 | { 78 | i=(i+1)%LENGTH; 79 | printf("%d\t",queue[i]); 80 | } 81 | while(i!=rear); 82 | } 83 | 84 | -------------------------------------------------------------------------------- /QUEUEARR.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define LENGTH 5 5 | 6 | int queue[LENGTH]; 7 | int front=0,rear=0; 8 | 9 | void add(int); 10 | void del(); 11 | void list(); 12 | 13 | void main() 14 | { 15 | int ch,n; 16 | do 17 | { 18 | clrscr(); 19 | printf("\n\tQUEUE OPERATIONS\n"); 20 | printf("\n\t\t1.ADD"); 21 | printf("\n\t\t2.DELETE"); 22 | printf("\n\t\t3.LIST"); 23 | printf("\n\t\t4.EXIT"); 24 | printf("\n\n\tEnter your choice(1-4) : "); 25 | scanf("%d",&ch); 26 | switch(ch) 27 | { 28 | case 1: 29 | printf("\nEnter the element : "); 30 | scanf("%d",&n); 31 | add(n); 32 | break; 33 | case 2: 34 | del(); 35 | break; 36 | case 3: 37 | list(); 38 | break; 39 | default:continue; 40 | } 41 | getch(); 42 | }while(ch!=4); 43 | } 44 | 45 | void add(int x) 46 | { 47 | int i; 48 | if(rear==LENGTH && front==0) 49 | { 50 | printf("\nSorry, queue is full..."); 51 | return; 52 | } 53 | if(rear==LENGTH) 54 | { 55 | for(i=front;i 2 | #include 3 | #include 4 | 5 | typedef struct stacknode* stackpointer; 6 | 7 | struct stacknode 8 | { 9 | int data; 10 | stackpointer next; 11 | }; 12 | 13 | stackpointer top=NULL,node; 14 | 15 | void push(int); 16 | void pop(); 17 | void list(); 18 | 19 | void main() 20 | { 21 | int ch,n; 22 | do 23 | { 24 | clrscr(); 25 | printf("\n\tSTACK OPERATIONS\n"); 26 | printf("\n\t\t1.PUSH"); 27 | printf("\n\t\t2.POP"); 28 | printf("\n\t\t3.LIST"); 29 | printf("\n\t\t4.EXIT"); 30 | printf("\n\n\tEnter your choice(1-4) : "); 31 | scanf("%d",&ch); 32 | switch(ch) 33 | { 34 | case 1: 35 | printf("\nEnter the element : "); 36 | scanf("%d",&n); 37 | push(n); 38 | break; 39 | case 2: 40 | pop(); 41 | break; 42 | case 3: 43 | list(); 44 | break; 45 | default:continue; 46 | } 47 | getch(); 48 | }while(ch!=4); 49 | } 50 | 51 | void push(int x) 52 | { 53 | node=(stackpointer)malloc(sizeof(*node)); 54 | if(node==NULL) 55 | { 56 | printf("\nSorry, insufficient memory..."); 57 | return; 58 | } 59 | node->data=x; 60 | node->next=top; 61 | top=node; 62 | printf("\n%d is added...",x); 63 | } 64 | void pop() 65 | { 66 | if(top==NULL) 67 | { 68 | printf("\nSorry, stack is empty..."); 69 | return; 70 | } 71 | printf("\n%d is removed...",top->data); 72 | node=top; 73 | top=top->next; 74 | free(node); 75 | } 76 | void list() 77 | { 78 | if(top==NULL) 79 | { 80 | printf("\nSorry, stack is empty..."); 81 | return; 82 | } 83 | printf("\nThe stack :\n"); 84 | for(node=top;node!=NULL;node=node->next) 85 | printf("\n%d",node->data); 86 | } 87 | -------------------------------------------------------------------------------- /QUEUELIN.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct queuenode* queuepointer; 6 | 7 | struct queuenode 8 | { 9 | int data; 10 | queuepointer next; 11 | }; 12 | 13 | queuepointer front=NULL,rear=NULL,node; 14 | 15 | void add(int); 16 | void del(); 17 | void list(); 18 | 19 | void main() 20 | { 21 | int ch,n; 22 | do 23 | { 24 | clrscr(); 25 | printf("\n\tQUEUE OPERATIONS\n"); 26 | printf("\n\t\t1.ADD"); 27 | printf("\n\t\t2.DELETE"); 28 | printf("\n\t\t3.LIST"); 29 | printf("\n\t\t4.EXIT"); 30 | printf("\n\n\tEnter your choice(1-4) : "); 31 | scanf("%d",&ch); 32 | switch(ch) 33 | { 34 | case 1: 35 | printf("\nEnter the element : "); 36 | scanf("%d",&n); 37 | add(n); 38 | break; 39 | case 2: 40 | del(); 41 | break; 42 | case 3: 43 | list(); 44 | break; 45 | default:continue; 46 | } 47 | getch(); 48 | }while(ch!=4); 49 | } 50 | 51 | void add(int x) 52 | { 53 | node=(queuepointer)malloc(sizeof(*node)); 54 | if(node==NULL) 55 | { 56 | printf("\nSorry, insufficient memory..."); 57 | return; 58 | } 59 | node->data=x; 60 | node->next=NULL; 61 | if(front==NULL) front=rear=node; 62 | else 63 | { 64 | rear->next=node; 65 | rear=rear->next; 66 | } 67 | printf("\n%d is added...",x); 68 | } 69 | void del() 70 | { 71 | if(front==NULL) 72 | { 73 | printf("\nSorry, queue is empty..."); 74 | return; 75 | } 76 | printf("\n%d is removed...",front->data); 77 | node=front; 78 | front=front->next; 79 | free(node); 80 | } 81 | void list() 82 | { 83 | if(front==NULL) 84 | { 85 | printf("\nSorry, queue is empty..."); 86 | return; 87 | } 88 | printf("\nThe queue :\n\n"); 89 | for(node=front;node!=NULL;node=node->next) 90 | printf("%d\t",node->data); 91 | } 92 | 93 | -------------------------------------------------------------------------------- /POLYARR1.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct {int coef,exp;} polynomial; 5 | 6 | void read(polynomial*,int); 7 | void print(polynomial*,int); 8 | 9 | void main() 10 | { 11 | polynomial p1[10],p2[10],p3[20],p4[50],t; 12 | int n1,n2,n3,n4,i,j,k; 13 | clrscr(); 14 | printf("Number of terms in first polynomial : "); 15 | scanf("%d",&n1); 16 | printf("\nEnter %d terms for first polynomial:\n",n1); 17 | read(p1,n1); 18 | printf("\nNumber of terms in second polynomial : "); 19 | scanf("%d",&n2); 20 | printf("\nEnter %d terms for second polynomial:\n",n2); 21 | read(p2,n2); 22 | i=j=n3=0; 23 | while(ip2[j].exp) 26 | p3[n3++]=p1[i++]; 27 | else if(p1[i].exp0) printf("+"); 81 | if(p[i].coef==-1) printf("-"); 82 | else if(p[i].coef!=1) printf("%d",p[i].coef); 83 | if(p[i].exp==1) printf("x"); 84 | else if(p[i].exp!=0) printf("x^%d",p[i].exp); 85 | else if(p[i].coef==-1 || p[i].coef==1) printf("1"); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /LISTSORT.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct listnode* listpointer; 6 | 7 | struct listnode 8 | { 9 | int data; 10 | listpointer next; 11 | }; 12 | 13 | listpointer first=NULL,node; 14 | 15 | void create(); 16 | void list(); 17 | void sort(); 18 | 19 | void main() 20 | { 21 | int choice; 22 | do 23 | { 24 | clrscr(); 25 | printf("\n\tLINKED LIST OPERATIONS\n"); 26 | printf("\n\t\t1. CREATE"); 27 | printf("\n\t\t2. LIST"); 28 | printf("\n\t\t3. SORT"); 29 | printf("\n\t\t4. EXIT"); 30 | printf("\n\n\tEnter your choice(1-4) : "); 31 | scanf("%d",&choice); 32 | switch(choice) 33 | { 34 | case 1:create(); 35 | break; 36 | case 2:list(); 37 | break; 38 | case 3:sort(); 39 | break; 40 | default:continue; 41 | } 42 | getch(); 43 | }while(choice!=4); 44 | } 45 | 46 | void create() 47 | { 48 | int i,n; 49 | if(first!=NULL) 50 | { 51 | printf("\nList already created..."); 52 | return; 53 | } 54 | printf("\nEnter the number of elements : "); 55 | scanf("%d",&n); 56 | if(n<1) return; 57 | printf("\nEnter %d elements into list :\n",n); 58 | first=(listpointer)malloc(sizeof(*first)); 59 | scanf("%d",&first->data); 60 | node=first; 61 | for(i=0;inext=(listpointer)malloc(sizeof(*node)); 64 | node=node->next; 65 | scanf("%d",&node->data); 66 | } 67 | node->next=NULL; 68 | printf("\nList created with %d elements.",n); 69 | } 70 | void list() 71 | { 72 | if(first==NULL) 73 | { 74 | printf("\nList is empty..."); 75 | return; 76 | } 77 | printf("\nElements in the list :\n\n"); 78 | for(node=first;node!=NULL;node=node->next) 79 | printf(" %d",node->data); 80 | } 81 | void sort() 82 | { 83 | int temp; 84 | listpointer node1,node2; 85 | if(first==NULL) 86 | { 87 | printf("\nList is empty..."); 88 | return; 89 | } 90 | for(node1=first;node1->next!=NULL;node1=node1->next) 91 | { 92 | node=node1; 93 | for(node2=node1->next;node2!=NULL;node2=node2->next) 94 | if(node2->datadata) node=node2; 95 | if(node==node1) continue; 96 | temp=node1->data; 97 | node1->data=node->data; 98 | node->data=temp; 99 | } 100 | printf("\nList is sorted..."); 101 | } 102 | -------------------------------------------------------------------------------- /BSTSEARC.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct treeNode* treePointer; 6 | 7 | struct treeNode 8 | { 9 | int data; 10 | treePointer leftChild,rightChild; 11 | }; 12 | 13 | treePointer root=NULL; 14 | 15 | void create(); 16 | void search(int); 17 | 18 | void main() 19 | { 20 | int choice,n; 21 | do 22 | { 23 | clrscr(); 24 | printf("\n\tBINARY SEARCH TREE OPERATIONS\n"); 25 | printf("\n\t\t1. Create"); 26 | printf("\n\t\t2. Search"); 27 | printf("\n\t\t3. Exit"); 28 | printf("\n\n\tEnter your choice(1-3) : "); 29 | scanf("%d",&choice); 30 | switch(choice) 31 | { 32 | case 1: 33 | create(); 34 | break; 35 | case 2: 36 | if(root==NULL) printf("\nTree is empty..."); 37 | else 38 | { 39 | printf("\nEnter the element : "); 40 | scanf("%d",&n); 41 | search(n); 42 | } 43 | break; 44 | default:continue; 45 | } 46 | getch(); 47 | }while(choice!=3); 48 | } 49 | 50 | void create() 51 | { 52 | int i,n=0,left; 53 | treePointer node,temp,p; 54 | if(root!=NULL) 55 | { 56 | printf("\nTree already exists..."); 57 | return; 58 | } 59 | printf("\nEnter the number of nodes : "); 60 | scanf("%d",&n); 61 | printf("\nEnter %d nodes for the binary search tree:\n",n); 62 | root=(treePointer)malloc(sizeof(*root)); 63 | scanf("%d",&root->data); 64 | root->leftChild=root->rightChild=NULL; 65 | for(i=0;idata); 69 | temp->leftChild=temp->rightChild=NULL; 70 | node=root; 71 | while(node!=NULL) 72 | { 73 | left=0; 74 | p=node; 75 | if(temp->datadata) 76 | { 77 | left=1; 78 | node=node->leftChild; 79 | } 80 | else node=node->rightChild; 81 | } 82 | if(left) p->leftChild=temp; 83 | else p->rightChild=temp; 84 | } 85 | printf("\n\nBinary search tree created with %d nodes...",n); 86 | } 87 | void search(int x) 88 | { 89 | treePointer node; 90 | node=root; 91 | while(node!=NULL) 92 | { 93 | if(node->data==x) 94 | { 95 | printf("\nElement is found"); 96 | return; 97 | } 98 | if(xdata) node=node->leftChild; 99 | else node=node->rightChild; 100 | } 101 | printf("\nElement is not found"); 102 | } 103 | -------------------------------------------------------------------------------- /SPARSE1.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct {int r,c,v;} term; 6 | typedef struct 7 | { 8 | int rows,cols,n; 9 | term terms[100]; 10 | }sparse; 11 | 12 | void read(sparse*); 13 | void print(sparse); 14 | sparse add(sparse,sparse); 15 | sparse mul(sparse,sparse); 16 | 17 | void main() 18 | { 19 | sparse s1,s2,s3,s4; 20 | clrscr(); 21 | printf("Enter the first sparse matrix:"); 22 | read(&s1); 23 | printf("\nEnter the second sparse matrix:"); 24 | read(&s2); 25 | printf("\nFirst sparse matrix:"); 26 | print(s1); 27 | printf("\nSecond sparse matrix:"); 28 | print(s2); 29 | printf("\nMatrix sum:"); 30 | s3=add(s1,s2); 31 | print(s3); 32 | printf("\nMatrix product:"); 33 | s4=mul(s1,s2); 34 | print(s4); 35 | getch(); 36 | } 37 | 38 | void read(sparse* s) 39 | { 40 | int i; 41 | printf("\nEnter the order: "); 42 | scanf("%d%d",&s->rows,&s->cols); 43 | printf("\nEnter the number of non zero elements : "); 44 | scanf("%d",&s->n); 45 | printf("\nEnter the details of %d non zero elements rowwise:\n",s->n); 46 | for(i=0;in;i++) 47 | { 48 | printf("\nRow : "); 49 | scanf("%d",&s->terms[i].r); 50 | printf("Column : "); 51 | scanf("%d",&s->terms[i].c); 52 | printf("Value : "); 53 | scanf("%d",&s->terms[i].v); 54 | } 55 | } 56 | void print(sparse s) 57 | { 58 | int i,j,k; 59 | for(i=1,k=0;i<=s.rows;i++) 60 | { 61 | printf("\n"); 62 | for(j=1;j<=s.cols;j++) 63 | if(ks2.terms[j].r || s1.terms[i].r==s2.terms[j].r && s1.terms[i].c>s2.terms[j].c) 87 | s3.terms[k++]=s2.terms[j++]; 88 | else if(s1.terms[i].v+s2.terms[j].v!=0) 89 | { 90 | s3.terms[k]=s1.terms[i++]; 91 | s3.terms[k++].v+=s2.terms[j++].v; 92 | } 93 | else i++,j++; 94 | } 95 | while(i=0 && (t.r 2 | #include 3 | #include 4 | 5 | typedef struct treeNode* treePointer; 6 | 7 | struct treeNode 8 | { 9 | int data; 10 | treePointer leftChild,rightChild; 11 | }; 12 | 13 | treePointer root=NULL; 14 | 15 | void create(); 16 | void preOrder(treePointer); 17 | void inOrder(treePointer); 18 | void postOrder(treePointer); 19 | 20 | void main() 21 | { 22 | int choice; 23 | do 24 | { 25 | clrscr(); 26 | printf("\n\tBINARY TREE OPERATIONS\n"); 27 | printf("\n\t\t1. Create"); 28 | printf("\n\t\t2. Traverse preorder"); 29 | printf("\n\t\t3. Traverse inorder"); 30 | printf("\n\t\t4. Traverse postorder"); 31 | printf("\n\t\t5. Exit"); 32 | printf("\n\n\tEnter your choice(1-5) : "); 33 | scanf("%d",&choice); 34 | switch(choice) 35 | { 36 | case 1: 37 | create(); 38 | break; 39 | case 2: 40 | if(root==NULL) printf("\nTree is empty..."); 41 | else 42 | { 43 | printf("\nPreorder traversal is "); 44 | preOrder(root); 45 | } 46 | break; 47 | case 3: 48 | if(root==NULL) printf("\nTree is empty..."); 49 | else 50 | { 51 | printf("\nInorder traversal is "); 52 | inOrder(root); 53 | } 54 | break; 55 | case 4: 56 | if(root==NULL) printf("\nTree is empty..."); 57 | else 58 | { 59 | printf("\nPostorder traversal is "); 60 | postOrder(root); 61 | } 62 | break; 63 | default:continue; 64 | } 65 | getch(); 66 | }while(choice!=5); 67 | } 68 | 69 | void create() 70 | { 71 | int n=0; 72 | char choice; 73 | treePointer node,temp; 74 | treePointer queue[50]; 75 | int front=0,rear=0; 76 | if(root!=NULL) 77 | { 78 | printf("\nTree already exists..."); 79 | return; 80 | } 81 | printf("\nEnter the nodes level wise:\n"); 82 | root=(treePointer)malloc(sizeof(*root)); 83 | printf("\nEnter data for the root node : "); 84 | scanf("%d",&root->data); 85 | root->leftChild=root->rightChild=NULL; 86 | queue[rear++]=root; 87 | n++; 88 | while(front!=rear) 89 | { 90 | node=queue[front++]; 91 | printf("\n\nEnter child nodes for node with data %d",node->data); 92 | printf("\n\nAdd left child(y/n) : "); 93 | choice=getche(); 94 | if(choice=='y' || choice=='Y') 95 | { 96 | temp=(treePointer)malloc(sizeof(*temp)); 97 | printf("\nEnter data for the left child node : "); 98 | scanf("%d",&temp->data); 99 | temp->leftChild=temp->rightChild=NULL; 100 | node->leftChild=temp; 101 | queue[rear++]=temp; 102 | n++; 103 | } 104 | printf("\nAdd right child(y/n) : "); 105 | choice=getche(); 106 | if(choice=='y' || choice=='Y') 107 | { 108 | temp=(treePointer)malloc(sizeof(*temp)); 109 | printf("\nEnter data for the right child node : "); 110 | scanf("%d",&temp->data); 111 | temp->leftChild=temp->rightChild=NULL; 112 | node->rightChild=temp; 113 | queue[rear++]=temp; 114 | n++; 115 | } 116 | } 117 | printf("\n\nBinary tree created with %d nodes...",n); 118 | } 119 | void preOrder(treePointer node) 120 | { 121 | if(node==NULL) return; 122 | printf(" %d",node->data); 123 | preOrder(node->leftChild); 124 | preOrder(node->rightChild); 125 | } 126 | void inOrder(treePointer node) 127 | { 128 | if(node==NULL) return; 129 | inOrder(node->leftChild); 130 | printf(" %d",node->data); 131 | inOrder(node->rightChild); 132 | } 133 | void postOrder(treePointer node) 134 | { 135 | if(node==NULL) return; 136 | postOrder(node->leftChild); 137 | postOrder(node->rightChild); 138 | printf(" %d",node->data); 139 | } 140 | -------------------------------------------------------------------------------- /LINKEDLI.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct listnode* listpointer; 6 | 7 | struct listnode 8 | { 9 | int data; 10 | listpointer next; 11 | }; 12 | 13 | listpointer fnode=NULL,p; 14 | int listSize=0; 15 | 16 | void create(); 17 | void list(); 18 | void insert(int,int); 19 | void delv(int); 20 | void delp(int); 21 | 22 | void main() 23 | { 24 | int choice; 25 | int n,pos; 26 | do 27 | { 28 | clrscr(); 29 | printf("\n\tLINKED LIST OPERATIONS\n"); 30 | printf("\n\t\t1. CREATE"); 31 | printf("\n\t\t2. LIST"); 32 | printf("\n\t\t3. INSERT"); 33 | printf("\n\t\t4. DELETE BY VALUE"); 34 | printf("\n\t\t5. DELETE BY POSITION"); 35 | printf("\n\t\t6. EXIT"); 36 | printf("\n\n\tEnter your choice(1-6) : "); 37 | scanf("%d",&choice); 38 | switch(choice) 39 | { 40 | case 1:create(); 41 | break; 42 | case 2:list(); 43 | break; 44 | case 3:printf("\nEnter the data : "); 45 | scanf("%d",&n); 46 | printf("Enter the position(1-%d) : ",listSize+1); 47 | scanf("%d",&pos); 48 | insert(n,pos); 49 | break; 50 | case 4:printf("\nEnter the data : "); 51 | scanf("%d",&n); 52 | delv(n); 53 | break; 54 | case 5:printf("\nEnter the position(1-%d) : ",listSize); 55 | scanf("%d",&pos); 56 | delp(pos); 57 | break; 58 | default:continue; 59 | } 60 | getch(); 61 | }while(choice!=6); 62 | } 63 | 64 | void create() 65 | { 66 | int i; 67 | if(fnode!=NULL) 68 | { 69 | printf("\nList already created..."); 70 | return; 71 | } 72 | printf("\nEnter the number of elements : "); 73 | scanf("%d",&listSize); 74 | if(listSize<1) return; 75 | printf("\nEnter %d elements into list :\n",listSize); 76 | fnode=(listpointer)malloc(sizeof(*fnode)); 77 | scanf("%d",&fnode->data); 78 | fnode->next=NULL; 79 | p=fnode; 80 | for(i=0;inext=(listpointer)malloc(sizeof(*p)); 83 | p=p->next; 84 | scanf("%d",&p->data); 85 | } 86 | p->next=NULL; 87 | printf("\nList created and %d elements inserted into the list",listSize); 88 | } 89 | void list() 90 | { 91 | if(fnode==NULL) 92 | { 93 | printf("\nList is empty..."); 94 | return; 95 | } 96 | printf("\nElements in the list :\n\n"); 97 | for(p=fnode;p!=NULL;p=p->next) 98 | printf(" %d",p->data); 99 | } 100 | void insert(int x,int loc) 101 | { 102 | int i; 103 | listpointer node; 104 | if(loc<1 || loc>listSize+1) 105 | { 106 | printf("\nInvalid location..."); 107 | return; 108 | } 109 | node=(listpointer)malloc(sizeof(*node)); 110 | node->data=x; 111 | if(loc==1) 112 | { 113 | node->next=fnode; 114 | fnode=node; 115 | } 116 | else 117 | { 118 | for(i=1,p=fnode;i!=loc-1;i++,p=p->next) 119 | ; 120 | node->next=p->next; 121 | p->next=node; 122 | } 123 | listSize++; 124 | printf("\nOne element inserted..."); 125 | } 126 | void delv(int x) 127 | { 128 | listpointer d; 129 | if(fnode==NULL) 130 | { 131 | printf("\nList is empty..."); 132 | return; 133 | } 134 | if(fnode->data==x) 135 | { 136 | d=fnode; 137 | fnode=fnode->next; 138 | } 139 | else 140 | { 141 | for(p=fnode;p->next!=NULL && p->next->data!=x;p=p->next) 142 | ; 143 | if(p->next==NULL) 144 | { 145 | printf("\nElement not found..."); 146 | return; 147 | } 148 | d=p->next; 149 | p->next=d->next; 150 | } 151 | free(d); 152 | listSize--; 153 | printf("\nOne element deleted..."); 154 | } 155 | void delp(int loc) 156 | { 157 | int i; 158 | listpointer d; 159 | if(fnode==NULL) 160 | { 161 | printf("\nList is empty..."); 162 | return; 163 | } 164 | if(loc<1 || loc>listSize) 165 | { 166 | printf("\nInvalid location..."); 167 | return; 168 | } 169 | if(loc==1) 170 | { 171 | d=fnode; 172 | fnode=fnode->next; 173 | } 174 | else 175 | { 176 | for(i=1,p=fnode;i!=loc-1;i++,p=p->next) 177 | ; 178 | d=p->next; 179 | p->next=d->next; 180 | } 181 | free(d); 182 | listSize--; 183 | printf("\nOne element deleted..."); 184 | } 185 | -------------------------------------------------------------------------------- /DHEADLIS.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct listnode* listpointer; 6 | 7 | struct listnode 8 | { 9 | int data; 10 | listpointer next,prev; 11 | }; 12 | 13 | listpointer head,node,p; 14 | int listSize=0; 15 | 16 | void create(); 17 | void list(); 18 | void insert(int,int); 19 | void delv(int); 20 | void delp(int); 21 | 22 | void main() 23 | { 24 | int choice; 25 | int n,pos; 26 | listpointer head=(listpointer)malloc(sizeof(*head)); 27 | head->next=head->prev=head; 28 | do 29 | { 30 | clrscr(); 31 | printf("\n\tLINKED LIST OPERATIONS\n"); 32 | printf("\n\t\t1. CREATE"); 33 | printf("\n\t\t2. LIST"); 34 | printf("\n\t\t3. INSERT"); 35 | printf("\n\t\t4. DELETE BY VALUE"); 36 | printf("\n\t\t5. DELETE BY POSITION"); 37 | printf("\n\t\t6. EXIT"); 38 | printf("\n\n\tEnter your choice(1-6) : "); 39 | scanf("%d",&choice); 40 | switch(choice) 41 | { 42 | case 1:create(); 43 | break; 44 | case 2:list(); 45 | break; 46 | case 3:printf("\nEnter the data : "); 47 | scanf("%d",&n); 48 | printf("Enter the position(1-%d) : ",listSize+1); 49 | scanf("%d",&pos); 50 | insert(n,pos); 51 | break; 52 | case 4:printf("\nEnter the data : "); 53 | scanf("%d",&n); 54 | delv(n); 55 | break; 56 | case 5:printf("\nEnter the position(1-%d) : ",listSize); 57 | scanf("%d",&pos); 58 | delp(pos); 59 | break; 60 | default:continue; 61 | } 62 | getch(); 63 | }while(choice!=6); 64 | } 65 | 66 | void create() 67 | { 68 | int i; 69 | if(head->next!=head) 70 | { 71 | printf("\nList already created..."); 72 | return; 73 | } 74 | printf("\nEnter the number of elements : "); 75 | scanf("%d",&listSize); 76 | if(listSize<1) return; 77 | printf("\nEnter %d elements into list :\n",listSize); 78 | node=head; 79 | for(i=0;inext=(listpointer)malloc(sizeof(*node)); 82 | node->next->prev=node; 83 | node=node->next; 84 | scanf("%d",&node->data); 85 | } 86 | node->next=head; 87 | head->prev=node; 88 | printf("\nList created with %d elements.",listSize); 89 | } 90 | void list() 91 | { 92 | if(head->next==head) 93 | { 94 | printf("\nList is empty..."); 95 | return; 96 | } 97 | printf("\nElements in the list :\n\n"); 98 | for(node=head->next;node!=head;node=node->next) 99 | printf(" %d",node->data); 100 | } 101 | void insert(int x,int loc) 102 | { 103 | int i; 104 | if(loc<1 || loc>listSize+1) 105 | { 106 | printf("\nInvalid location..."); 107 | return; 108 | } 109 | node=(listpointer)malloc(sizeof(*node)); 110 | node->data=x; 111 | p=head; 112 | if(loc<=listSize/2) 113 | for(i=0;i!=loc-1;i++,p=p->next) 114 | ; 115 | else 116 | for(i=listSize+1;i!=loc-1;i--,p=p->prev) 117 | ; 118 | node->next=p->next; 119 | node->prev=p; 120 | p->next=node; 121 | node->next->prev=node; 122 | listSize++; 123 | printf("\nOne element inserted..."); 124 | } 125 | void delv(int x) 126 | { 127 | if(head->next==head) 128 | { 129 | printf("\nList is empty..."); 130 | return; 131 | } 132 | for(node=head->next;node!=head && node->data!=x;node=node->next) 133 | ; 134 | if(node==head) 135 | { 136 | printf("\nElement not found..."); 137 | return; 138 | } 139 | node->prev->next=node->next; 140 | node->next->prev=node->prev; 141 | free(node); 142 | listSize--; 143 | printf("\nOne element deleted..."); 144 | } 145 | void delp(int loc) 146 | { 147 | int i; 148 | if(head->next==head) 149 | { 150 | printf("\nList is empty..."); 151 | return; 152 | } 153 | if(loc<1 || loc>listSize) 154 | { 155 | printf("\nInvalid location..."); 156 | return; 157 | } 158 | if(loc<=listSize/2) 159 | for(i=1,node=head->next;i!=loc;i++,node=node->next) 160 | ; 161 | else 162 | for(i=listSize,node=head->prev;i!=loc;i--,node=node->prev) 163 | ; 164 | node->prev->next=node->next; 165 | node->next->prev=node->prev; 166 | free(node); 167 | listSize--; 168 | printf("\nOne element deleted..."); 169 | } 170 | -------------------------------------------------------------------------------- /HEADLIST.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct listnode* listpointer; 6 | 7 | struct listnode 8 | { 9 | int data; 10 | listpointer next; 11 | }; 12 | 13 | listpointer head,node,p; 14 | int listSize=0; 15 | 16 | void create(); 17 | void list(); 18 | void sort(); 19 | void insert(int,int); 20 | void delv(int); 21 | void delp(int); 22 | 23 | void main() 24 | { 25 | int choice; 26 | int n,pos; 27 | head=(listpointer)malloc(sizeof(*head)); 28 | head->next=NULL; 29 | do 30 | { 31 | clrscr(); 32 | printf("\n\tLINKED LIST OPERATIONS\n"); 33 | printf("\n\t\t1. CREATE"); 34 | printf("\n\t\t2. LIST"); 35 | printf("\n\t\t3. SORT"); 36 | printf("\n\t\t4. INSERT"); 37 | printf("\n\t\t5. DELETE BY VALUE"); 38 | printf("\n\t\t6. DELETE BY POSITION"); 39 | printf("\n\t\t7. EXIT"); 40 | printf("\n\n\tEnter your choice(1-7) : "); 41 | scanf("%d",&choice); 42 | switch(choice) 43 | { 44 | case 1:create(); 45 | break; 46 | case 2:list(); 47 | break; 48 | case 3:sort(); 49 | break; 50 | case 4:printf("\nEnter the data : "); 51 | scanf("%d",&n); 52 | printf("Enter the position(1-%d) : ",listSize+1); 53 | scanf("%d",&pos); 54 | insert(n,pos); 55 | break; 56 | case 5:printf("\nEnter the data : "); 57 | scanf("%d",&n); 58 | delv(n); 59 | break; 60 | case 6:printf("\nEnter the position(1-%d) : ",listSize); 61 | scanf("%d",&pos); 62 | delp(pos); 63 | break; 64 | default:continue; 65 | } 66 | getch(); 67 | }while(choice!=7); 68 | } 69 | 70 | void create() 71 | { 72 | int i; 73 | if(head->next!=NULL) 74 | { 75 | printf("\nList already created..."); 76 | return; 77 | } 78 | printf("\nEnter the number of elements : "); 79 | scanf("%d",&listSize); 80 | if(listSize<1) return; 81 | printf("\nEnter %d elements into list :\n",listSize); 82 | node=head; 83 | for(i=0;inext=(listpointer)malloc(sizeof(*node)); 86 | node=node->next; 87 | scanf("%d",&node->data); 88 | } 89 | node->next=NULL; 90 | printf("\nList created with %d elements.",listSize); 91 | } 92 | void list() 93 | { 94 | if(head->next==NULL) 95 | { 96 | printf("\nList is empty..."); 97 | return; 98 | } 99 | printf("\nElements in the list :\n\n"); 100 | for(node=head->next;node!=NULL;node=node->next) 101 | printf(" %d",node->data); 102 | } 103 | void sort() 104 | { 105 | int temp; 106 | listpointer node1; 107 | if(head->next==NULL) 108 | { 109 | printf("\nList is empty..."); 110 | return; 111 | } 112 | for(node1=head->next;node1->next!=NULL;node1=node1->next) 113 | { 114 | node=node1; 115 | for(p=node1->next;p!=NULL;p=p->next) 116 | if(p->datadata) node=p; 117 | if(node==node1) continue; 118 | temp=node1->data; 119 | node1->data=node->data; 120 | node->data=temp; 121 | } 122 | printf("\nList is sorted..."); 123 | } 124 | void insert(int x,int loc) 125 | { 126 | int i; 127 | if(loc<1 || loc>listSize+1) 128 | { 129 | printf("\nInvalid location..."); 130 | return; 131 | } 132 | node=(listpointer)malloc(sizeof(*node)); 133 | node->data=x; 134 | for(i=0,p=head;i!=loc-1;i++,p=p->next) 135 | ; 136 | node->next=p->next; 137 | p->next=node; 138 | listSize++; 139 | printf("\nOne element inserted..."); 140 | } 141 | void delv(int x) 142 | { 143 | if(head->next==NULL) 144 | { 145 | printf("\nList is empty..."); 146 | return; 147 | } 148 | for(p=head;p->next!=NULL && p->next->data!=x;p=p->next) 149 | ; 150 | if(p->next==NULL) 151 | { 152 | printf("\nElement not found..."); 153 | return; 154 | } 155 | node=p->next; 156 | p->next=node->next; 157 | free(node); 158 | listSize--; 159 | printf("\nOne element deleted..."); 160 | } 161 | void delp(int loc) 162 | { 163 | int i; 164 | if(head->next==NULL) 165 | { 166 | printf("\nList is empty..."); 167 | return; 168 | } 169 | if(loc<1 || loc>listSize) 170 | { 171 | printf("\nInvalid location..."); 172 | return; 173 | } 174 | for(i=0,p=head;i!=loc-1;i++,p=p->next) 175 | ; 176 | node=p->next; 177 | p->next=node->next; 178 | free(node); 179 | listSize--; 180 | printf("\nOne element deleted..."); 181 | } 182 | -------------------------------------------------------------------------------- /DOUBLLIS.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct listnode* listpointer; 6 | 7 | struct listnode 8 | { 9 | int data; 10 | listpointer next,prev; 11 | }; 12 | 13 | listpointer fnode=NULL,lnode=NULL,p; 14 | int listSize=0; 15 | 16 | void create(); 17 | void list(); 18 | void insert(int,int); 19 | void delv(int); 20 | void delp(int); 21 | 22 | void main() 23 | { 24 | int choice; 25 | int n,pos; 26 | do 27 | { 28 | clrscr(); 29 | printf("\n\tLINKED LIST OPERATIONS\n"); 30 | printf("\n\t\t1. CREATE"); 31 | printf("\n\t\t2. LIST"); 32 | printf("\n\t\t3. INSERT"); 33 | printf("\n\t\t4. DELETE BY VALUE"); 34 | printf("\n\t\t5. DELETE BY POSITION"); 35 | printf("\n\t\t6. EXIT"); 36 | printf("\n\n\tEnter your choice(1-6) : "); 37 | scanf("%d",&choice); 38 | switch(choice) 39 | { 40 | case 1:create(); 41 | break; 42 | case 2:list(); 43 | break; 44 | case 3:printf("\nEnter the data : "); 45 | scanf("%d",&n); 46 | printf("Enter the position(1-%d) : ",listSize+1); 47 | scanf("%d",&pos); 48 | insert(n,pos); 49 | break; 50 | case 4:printf("\nEnter the data : "); 51 | scanf("%d",&n); 52 | delv(n); 53 | break; 54 | case 5:printf("\nEnter the position(1-%d) : ",listSize); 55 | scanf("%d",&pos); 56 | delp(pos); 57 | break; 58 | default:continue; 59 | } 60 | getch(); 61 | }while(choice!=6); 62 | } 63 | 64 | void create() 65 | { 66 | int i; 67 | if(fnode!=NULL) 68 | { 69 | printf("\nList already created..."); 70 | return; 71 | } 72 | printf("\nEnter the number of elements : "); 73 | scanf("%d",&listSize); 74 | if(listSize<1) return; 75 | printf("\nEnter %d elements into list :\n",listSize); 76 | fnode=(listpointer)malloc(sizeof(*fnode)); 77 | scanf("%d",&fnode->data); 78 | fnode->next=fnode->prev=NULL; 79 | p=fnode; 80 | for(i=0;inext=(listpointer)malloc(sizeof(*p)); 83 | p->next->prev=p; 84 | p=p->next; 85 | scanf("%d",&p->data); 86 | } 87 | p->next=NULL; 88 | lnode=p; 89 | printf("\nList created and %d elements inserted into the list",listSize); 90 | } 91 | void list() 92 | { 93 | if(fnode==NULL) 94 | { 95 | printf("\nList is empty..."); 96 | return; 97 | } 98 | printf("\nElements in the list :\n\n"); 99 | for(p=fnode;p!=NULL;p=p->next) 100 | printf(" %d",p->data); 101 | } 102 | void insert(int x,int loc) 103 | { 104 | int i; 105 | listpointer node; 106 | if(loc<1 || loc>listSize+1) 107 | { 108 | printf("\nInvalid location..."); 109 | return; 110 | } 111 | node=(listpointer)malloc(sizeof(*node)); 112 | node->data=x; 113 | node->next=node->prev=NULL; 114 | if(fnode==NULL) fnode=lnode=node; 115 | else if(loc==1) 116 | { 117 | node->next=fnode; 118 | fnode->prev=node; 119 | fnode=node; 120 | } 121 | else if(loc==listSize+1) 122 | { 123 | node->prev=lnode; 124 | lnode->next=node; 125 | lnode=node; 126 | } 127 | else 128 | { 129 | if(loc<=listSize/2) 130 | for(i=1,p=fnode;i!=loc-1;i++,p=p->next) 131 | ; 132 | else 133 | for(i=listSize-1,p=lnode->prev;i!=loc-1;i--,p=p->prev) 134 | ; 135 | node->next=p->next; 136 | node->prev=p; 137 | p->next=node; 138 | node->next->prev=node; 139 | } 140 | listSize++; 141 | printf("\nOne element inserted..."); 142 | } 143 | void delv(int x) 144 | { 145 | if(fnode==NULL) 146 | { 147 | printf("\nList is empty..."); 148 | return; 149 | } 150 | if(fnode->data==x) 151 | { 152 | p=fnode; 153 | fnode=fnode->next; 154 | fnode->prev=NULL; 155 | } 156 | else if(lnode->data==x) 157 | { 158 | p=lnode; 159 | lnode=lnode->prev; 160 | lnode->next=NULL; 161 | } 162 | else 163 | { 164 | for(p=fnode->next;p!=NULL && p->data!=x;p=p->next) 165 | ; 166 | if(p==NULL) 167 | { 168 | printf("\nElement not found..."); 169 | return; 170 | } 171 | p->prev->next=p->next; 172 | p->next->prev=p->prev; 173 | } 174 | free(p); 175 | listSize--; 176 | printf("\nOne element deleted..."); 177 | } 178 | void delp(int loc) 179 | { 180 | int i; 181 | if(fnode==NULL) 182 | { 183 | printf("\nList is empty..."); 184 | return; 185 | } 186 | if(loc<1 || loc>listSize) 187 | { 188 | printf("\nInvalid location..."); 189 | return; 190 | } 191 | if(loc==1) 192 | { 193 | p=fnode; 194 | fnode=fnode->next; 195 | fnode->prev=NULL; 196 | } 197 | else if(loc==listSize) 198 | { 199 | p=lnode; 200 | lnode=lnode->prev; 201 | lnode->next=NULL; 202 | } 203 | else 204 | { 205 | if(loc<=listSize/2) 206 | for(i=2,p=fnode->next;i!=loc;i++,p=p->next) 207 | ; 208 | else 209 | for(i=listSize-1,p=lnode->prev;i!=loc;i--,p=p->prev) 210 | ; 211 | p->prev->next=p->next; 212 | p->next->prev=p->prev; 213 | } 214 | free(p); 215 | listSize--; 216 | printf("\nOne element deleted..."); 217 | } 218 | -------------------------------------------------------------------------------- /TREENREC.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct treeNode* treePointer; 6 | struct treeNode 7 | { 8 | int data; 9 | treePointer leftChild,rightChild; 10 | }; 11 | 12 | treePointer root=NULL; 13 | 14 | void create(); 15 | void preOrder(treePointer); 16 | void inOrder(treePointer); 17 | void postOrder(treePointer); 18 | void levelOrder(treePointer); 19 | 20 | treePointer queue[100]; 21 | int front,rear; 22 | #define initq() front=rear=-1 23 | #define addq(node) queue[++rear]=node 24 | #define delq() (front==rear)?NULL:queue[++front] 25 | 26 | treePointer stack[100]; 27 | int top=-1; 28 | #define push(node) stack[++top]=node 29 | #define pop() (top==-1)?NULL:stack[top--] 30 | 31 | void main() 32 | { 33 | int choice; 34 | do 35 | { 36 | clrscr(); 37 | printf("\n\tBINARY TREE OPERATIONS\n"); 38 | printf("\n\t\t1. Create"); 39 | printf("\n\t\t2. Traverse preorder"); 40 | printf("\n\t\t3. Traverse inorder"); 41 | printf("\n\t\t4. Traverse postorder"); 42 | printf("\n\t\t5. Traverse levelorder"); 43 | printf("\n\t\t6. Exit"); 44 | printf("\n\n\tEnter your choice(1-6) : "); 45 | scanf("%d",&choice); 46 | switch(choice) 47 | { 48 | case 1: 49 | create(); 50 | break; 51 | case 2: 52 | preOrder(root); 53 | break; 54 | case 3: 55 | inOrder(root); 56 | break; 57 | case 4: 58 | postOrder(root); 59 | break; 60 | case 5: 61 | levelOrder(root); 62 | break; 63 | default:continue; 64 | } 65 | getch(); 66 | }while(choice!=6); 67 | } 68 | 69 | void create() 70 | { 71 | int n=0; 72 | char choice; 73 | treePointer node,temp; 74 | if(root!=NULL) 75 | { 76 | printf("\nTree already exists..."); 77 | return; 78 | } 79 | initq(); 80 | printf("\nEnter the nodes level wise:\n"); 81 | root=(treePointer)malloc(sizeof(*root)); 82 | printf("\nEnter data for the root node : "); 83 | scanf("%d",&root->data); 84 | root->leftChild=root->rightChild=NULL; 85 | n++; 86 | node=root; 87 | do 88 | { 89 | printf("\n\nEnter child nodes for node with data %d",node->data); 90 | printf("\n\nAdd left child(y/n) : "); 91 | choice=getche(); 92 | if(choice=='y' || choice=='Y') 93 | { 94 | temp=(treePointer)malloc(sizeof(*temp)); 95 | printf("\nEnter data for the left child node : "); 96 | scanf("%d",&temp->data); 97 | temp->leftChild=temp->rightChild=NULL; 98 | node->leftChild=temp; 99 | addq(temp); 100 | n++; 101 | } 102 | printf("\nAdd right child(y/n) : "); 103 | choice=getche(); 104 | if(choice=='y' || choice=='Y') 105 | { 106 | temp=(treePointer)malloc(sizeof(*temp)); 107 | printf("\nEnter data for the right child node : "); 108 | scanf("%d",&temp->data); 109 | temp->leftChild=temp->rightChild=NULL; 110 | node->rightChild=temp; 111 | addq(temp); 112 | n++; 113 | } 114 | node=delq(); 115 | } 116 | while(node!=NULL); 117 | printf("\n\nBinary tree created with %d nodes...",n); 118 | } 119 | void preOrder(treePointer node) 120 | { 121 | if(node==NULL) 122 | { 123 | printf("\nTree is empty..."); 124 | return; 125 | } 126 | printf("\nPre order traversal :"); 127 | while(1) 128 | { 129 | while(node!=NULL) 130 | { 131 | printf(" %d",node->data); 132 | push(node); 133 | node=node->leftChild; 134 | } 135 | node=pop(); 136 | if(!node) break; 137 | node=node->rightChild; 138 | } 139 | } 140 | void inOrder(treePointer node) 141 | { 142 | if(node==NULL) 143 | { 144 | printf("\nTree is empty..."); 145 | return; 146 | } 147 | printf("\nIn order traversal :"); 148 | while(1) 149 | { 150 | while(node!=NULL) 151 | { 152 | push(node); 153 | node=node->leftChild; 154 | } 155 | node=pop(); 156 | if(!node) break; 157 | printf(" %d",node->data); 158 | node=node->rightChild; 159 | } 160 | } 161 | void postOrder(treePointer node) 162 | { 163 | treePointer p; 164 | if(node==NULL) 165 | { 166 | printf("\nTree is empty..."); 167 | return; 168 | } 169 | printf("\nPost order traversal :"); 170 | while(1) 171 | { 172 | while(node!=NULL) 173 | { 174 | push(node); 175 | node=node->leftChild; 176 | } 177 | node=pop(); 178 | if(node->rightChild==NULL) 179 | { 180 | printf(" %d",node->data); 181 | for(p=pop();p!=NULL && p->rightChild==node;p=pop()) 182 | { 183 | printf(" %d",p->data); 184 | node=p; 185 | } 186 | if(!p) break; 187 | node=p; 188 | } 189 | push(node); 190 | node=node->rightChild; 191 | } 192 | } 193 | void levelOrder(treePointer node) 194 | { 195 | if(node==NULL) 196 | { 197 | printf("\nTree is empty..."); 198 | return; 199 | } 200 | initq(); 201 | printf("\nLevel order traversal :"); 202 | do 203 | { 204 | printf(" %d",node->data); 205 | if(node->leftChild!=NULL) addq(node->leftChild); 206 | if(node->rightChild!=NULL) addq(node->rightChild); 207 | node=delq(); 208 | } 209 | while(node!=NULL); 210 | } 211 | --------------------------------------------------------------------------------