├── BST.c ├── Btree.c ├── README.md ├── bsearch.c ├── cqueue.c ├── dll.c ├── evaluation.c ├── infix.c ├── pqueue.c ├── priqueue.c ├── queue.c ├── sll.c ├── stack.c └── toh.c /BST.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct bst 4 | { 5 | int data; 6 | struct bst *rchild,*lchild; 7 | }node; 8 | node *root = NULL; 9 | void main() 10 | { 11 | int x,ch,i,n; 12 | void create(int); 13 | void display(node *); 14 | void inorder(node *); 15 | void preorder(node *); 16 | void postorder(node *); 17 | 18 | while(1) 19 | { 20 | printf("Enter your choice:\n1:create\n2:display\n3:Inorder\n4:preorder\n5:postorder\n6:exit\n"); 21 | scanf("%d",&ch); 22 | switch(ch) 23 | { 24 | case 1:printf("Enter the number of nodes to be created\n"); 25 | scanf("%d",&n); 26 | for(i=0;idata=x; 66 | new->lchild = new ->rchild = NULL; 67 | 68 | if(root == NULL)root = new; 69 | else 70 | { 71 | temp = root; 72 | while(1) 73 | { 74 | if(xdata) 75 | if(temp->lchild==NULL) 76 | { 77 | temp->rchild=new; 78 | break; 79 | } 80 | else 81 | temp = temp->lchild; 82 | 83 | else if(x>temp->data) 84 | if(temp->rchild==NULL) 85 | { 86 | temp->rchild = new; 87 | break; 88 | 89 | } 90 | else 91 | temp = temp->rchild; 92 | else 93 | { 94 | printf("Node with element %d already exists\n enter a new data\n"); 95 | scanf("%d",&x); 96 | create(x); 97 | break; 98 | 99 | } 100 | 101 | 102 | } 103 | } 104 | } 105 | void display(node *root) 106 | { 107 | if(root != NULL) 108 | { 109 | display(root->lchild); 110 | printf("%d\t",root->data); 111 | display(root->rchild); 112 | } 113 | } 114 | 115 | void inorder(node *root) 116 | { 117 | if(root != NULL) 118 | { 119 | inorder(root->lchild); 120 | printf("%d\t",root->data); 121 | inorder(root->rchild); 122 | } 123 | } 124 | 125 | void preorder(node *root) 126 | { 127 | if(root != NULL) 128 | { 129 | printf("%d\t",root->data); 130 | preorder(root->lchild); 131 | 132 | preorder(root->rchild); 133 | } 134 | } 135 | 136 | void postorder(node *root) 137 | { 138 | if(root != NULL) 139 | { 140 | 141 | postorder(root->lchild); 142 | 143 | postorder(root->rchild); 144 | printf("%d\t",root->data); 145 | } 146 | } -------------------------------------------------------------------------------- /Btree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct bst 4 | { 5 | int data; 6 | struct bst *lchild,*rchild; 7 | }node; 8 | node *root=NULL; 9 | void main() 10 | { 11 | int n,i,x,ch; 12 | void create(int); 13 | void display(node*); 14 | void inorder(node*); 15 | void preorder(node*); 16 | void postorder(node*); 17 | while(1) 18 | { 19 | printf("enter the choice\n"); 20 | printf("\n1.create\n2.display\n3.inorder traversal\n4.preorder traversal\n5.postorder traversal\n6.exit\n"); 21 | 22 | scanf("%d",&ch); 23 | switch(ch) 24 | { 25 | case 1:printf("enter the number of nodes to be created in a bst\n"); 26 | scanf("%d",&n); 27 | for(i=0;idata=x; 80 | new->lchild=new->rchild=NULL; 81 | if(root==NULL) 82 | root=new; 83 | else 84 | { 85 | temp=root; 86 | while(1) 87 | { 88 | if(xdata) 89 | if(temp->lchild==NULL) 90 | { 91 | temp->lchild=new; 92 | break; 93 | } 94 | else 95 | temp=temp->lchild; 96 | else if(x>temp->data) 97 | if(temp->rchild==NULL) 98 | { 99 | temp->rchild=new; 100 | break; 101 | } 102 | else 103 | temp=temp->rchild; 104 | else 105 | { 106 | printf("\n node with data %d already exists, enter the new data \n",x); 107 | scanf("%d",&x); 108 | create(x); 109 | break; 110 | } 111 | } 112 | } 113 | } 114 | void display(node *root) 115 | { 116 | if(root!=NULL) 117 | { 118 | display(root->lchild); 119 | printf("%d\t",root->data); 120 | display(root->rchild); 121 | } 122 | } 123 | void inorder(node *root) 124 | { 125 | if(root!=NULL) 126 | { 127 | inorder(root->lchild); 128 | printf("%d\t",root->data); 129 | inorder(root->rchild); 130 | } 131 | } 132 | void preorder(node *root) 133 | { 134 | if(root!=NULL) 135 | { 136 | 137 | printf("%d\t",root->data); 138 | preorder(root->lchild); 139 | preorder(root->rchild); 140 | } 141 | } 142 | void postorder(node *root) 143 | { 144 | if(root!=NULL) 145 | { 146 | postorder(root->lchild); 147 | postorder(root->rchild); 148 | printf("%d\t",root->data); 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data-Structures-C-language- 2 | This repository contains basic Data Structures code starting from Stack, Queue all the way up to Linked list, Singly linked list and Doubly linked list 3 | This repository contains 4 | * Stack 5 | * Evaluation of Infix expression to postfix using Stack 6 | * Evaluation of Postfix to infix 7 | * Queue 8 | * Circular Queue 9 | * Priority Queue 10 | * Tower of Hanoi problem 11 | * Binary Search 12 | * Binary Search Trees 13 | * Singly Linked list 14 | * Doubly Linked list 15 | -------------------------------------------------------------------------------- /bsearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { 5 | int a[10],n,i,low,high,pos,key; 6 | int binsearch(int a[10],int key,int low, int high); 7 | printf("Enter the number of elements to be inserted\n"); 8 | scanf("%d",&n); 9 | printf("Enter the %d elements in sorted form\n",n); 10 | for(i=0;ihigh) 25 | return -1; 26 | else 27 | { 28 | mid = (low+high)/2; 29 | return (key==a[mid]?mid:key 2 | #include 3 | #define MS 5 4 | int cq[MS],cf=-1,cr=-1; 5 | void main() 6 | { 7 | void insert(int); 8 | void delete(); 9 | void display(); 10 | int x,ch; 11 | while(1) 12 | { 13 | printf("Enter your choice\n1:insert\n2:delete\n3:display\n4:exit\n"); 14 | scanf("%d",&ch); 15 | switch(ch) 16 | { 17 | case 1:printf("Enter the element to be inserted into the queue\n"); 18 | scanf("%d",&x); 19 | insert(x); 20 | break; 21 | 22 | case 2:delete(); 23 | break; 24 | 25 | case 3:display(); 26 | break; 27 | 28 | case 4: printf("Thank you\n"); 29 | exit(0); 30 | 31 | default: printf("Invalid choice\n"); 32 | 33 | } 34 | } 35 | } 36 | void insert(int x) 37 | { 38 | int temp; 39 | if(cr == MS-1){ 40 | cr=0; 41 | temp=cr; 42 | } 43 | else cr++; 44 | if(cf==cr) 45 | { 46 | printf("queue is full\n"); 47 | return; 48 | } 49 | else 50 | cq[cr]=x; 51 | printf("Element %d is successfully inserted into the queue\n",x); 52 | if(cf==-1)cf=0; 53 | 54 | } 55 | void delete() 56 | { 57 | int t; 58 | if(cf==-1) 59 | { 60 | printf("queue is empty\n"); 61 | return; 62 | } 63 | else 64 | t=cq[cf]; 65 | if(cf==cr)cf=cr=-1; 66 | else if(cf==MS-1)cf=0; 67 | cf++; 68 | printf("element %d is deleted from the queue\n",t); 69 | 70 | } 71 | void display() 72 | { 73 | 74 | } 75 | -------------------------------------------------------------------------------- /dll.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct dll 4 | { 5 | int data; 6 | struct dll *llink,*rlink; 7 | }node; 8 | node *first = NULL; 9 | void main() 10 | { 11 | int x,ch,i,key,n; 12 | void ins_front(int); 13 | void ins_key(int,int); 14 | void del(int); 15 | void display(); 16 | while(1) 17 | { 18 | printf("Enter your choice:\n1:create\n2:insertion at given key value\n 3:delete \n 4:display\n 4:exit\n"); 19 | scanf("%d",&ch); 20 | switch(ch) 21 | { 22 | case 1:printf("Enter the number of nodes to be initially created\n"); 23 | scanf("%d",&n); 24 | for(i=0;idata = x; 58 | new ->llink = new->rlink = NULL; 59 | if(first == NULL) 60 | { 61 | first = new; 62 | } 63 | else{ 64 | new->rlink = first; 65 | first->llink= new; 66 | first = new; 67 | 68 | } 69 | } 70 | void del(int key) 71 | { 72 | 73 | } 74 | void ins_key(int key, int x) 75 | { 76 | node *new,*next,*temp; 77 | new = (node*)malloc(sizeof(node)); 78 | new->data = x; 79 | new->llink=new->rlink= NULL; 80 | if(first == NULL) 81 | { 82 | printf("List is empty\n"); 83 | return; 84 | } 85 | else 86 | { 87 | temp = first; 88 | while((temp!=NULL)&&(temp->data != key)) 89 | { 90 | temp = temp->rlink; 91 | next = temp->rlink; 92 | } 93 | if(temp == NULL) 94 | { 95 | printf("Element with key value %d is not found\n",key); 96 | return; 97 | } 98 | else if(temp == first) 99 | { 100 | new->rlink = first; 101 | first ->llink = new; 102 | first = new; 103 | } 104 | else{ 105 | temp->rlink = new; 106 | new->llink = temp; 107 | new->rlink = next; 108 | next->llink = new; 109 | } 110 | } 111 | } 112 | void display() 113 | { 114 | node *temp; 115 | if(first == NULL) 116 | { 117 | printf("List is emty"); 118 | return; 119 | 120 | } 121 | else{ 122 | temp = first; 123 | while(temp!=NULL) 124 | { 125 | printf("%d<-->",temp->data); 126 | temp=temp->rlink; 127 | } 128 | 129 | } 130 | } -------------------------------------------------------------------------------- /evaluation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define MS 25 5 | int s[MS],top= -1; 6 | char postfix[23]; 7 | void main() 8 | { 9 | void push(int); 10 | int pop(); 11 | int x,i=0,op1,op2; 12 | printf("Enter a valid postfix expression\n"); 13 | gets(postfix); 14 | while(postfix[i]!='\0') 15 | { 16 | if(isdigit(postfix[i])) 17 | push(postfix[i]); 18 | else{ 19 | op2 = pop(); 20 | op1 = pop(); 21 | switch(postfix[i]) 22 | { 23 | case '+': x = op1 + op2; 24 | break; 25 | case '-': x = op1 - op2; 26 | break; 27 | case '/': x = op1 / op2; 28 | break; 29 | case '*': x = op1 * op2; 30 | break; 31 | 32 | default:printf("Invalid postfix expression\n"); 33 | } 34 | push(x); 35 | } 36 | i++; 37 | } 38 | if(top>0) 39 | { 40 | printf("Invalid postfix expression\n"); 41 | exit(0); 42 | } 43 | else 44 | { 45 | printf("The result of the postfix expression %s is %d",postfix,s[top]); 46 | } 47 | } 48 | void push(int x) 49 | { 50 | if(top == MS -1) 51 | { 52 | printf("Stack overflow\n"); 53 | exit(0); 54 | } 55 | else 56 | s[++top]=x; 57 | } 58 | int pop() 59 | { 60 | if(top == -1) 61 | { 62 | printf("Stack underflow\n"); 63 | exit(0); 64 | } 65 | else 66 | return s[top--]; 67 | } -------------------------------------------------------------------------------- /infix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define MS 25 5 | char s[MS]; 6 | int top = -1; 7 | void main() 8 | { 9 | void push(char); 10 | int pop(); 11 | int priority(char); 12 | int i=0,j=0; 13 | char infix[25],postfix[25]; 14 | push('#'); 15 | printf("Enter a valid infix expression\n"); 16 | gets(infix); 17 | puts(infix); 18 | while(infix[i]!='\0') 19 | { 20 | if(isalnum(infix[i])) 21 | postfix[j++]= infix[i]; 22 | else if(infix[i] == '(') 23 | push(infix[i]); 24 | else if(infix[i] == ')') 25 | { 26 | while(s[top]!='(') 27 | { 28 | postfix[j++]=pop(); 29 | pop(); 30 | } 31 | } 32 | else if(infix[i] == '+' ||infix[i] == '-' ||infix[i] == '*' ||infix[i] == '/') 33 | { 34 | while(priority(s[top])>=priority(infix[i])) 35 | { 36 | postfix[j++]= pop(); 37 | push(infix[i]); 38 | } 39 | } 40 | i++; 41 | } 42 | while(s[top]!='#') 43 | postfix[j++]=pop(); 44 | postfix[j] = '\0'; 45 | printf("Infix expression: %s\npostfix expression %s\n",infix,postfix); 46 | } 47 | void push(char ch) 48 | { 49 | if(top == MS -1) 50 | { 51 | printf("Stack overflow\n"); 52 | exit(0); 53 | } 54 | else 55 | s[top++] = ch; 56 | } 57 | int pop() 58 | { 59 | if(top == -1) 60 | { 61 | printf("Stack underflow"); 62 | exit(0); 63 | } 64 | else 65 | return s[top--]; 66 | } -------------------------------------------------------------------------------- /pqueue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MS 2 4 | int pq[3][MS],pf[3]={-1,-1,-1},pr[3]={-1,-1,-1}; 5 | void main() 6 | { 7 | int x,ch,p; 8 | void insert(int,int); 9 | void delete(); 10 | void display(); 11 | while(1) 12 | { 13 | printf("\nEnter your choice\n1:insert\n2:delete\n3:display\n4:exit\n"); 14 | scanf("%d",&ch); 15 | switch(ch) 16 | { 17 | case 1:printf("Enter the element to be inserted and its priority\n"); 18 | scanf("%d%d",&x,&p); 19 | insert(x,p-1); 20 | break; 21 | 22 | case 2:delete(); 23 | break; 24 | 25 | case 3:display(); 26 | break; 27 | 28 | case 4: printf("Thank you\n"); 29 | exit(0); 30 | 31 | default: printf("Invalid choice\n"); 32 | 33 | } 34 | } 35 | } 36 | void insert(int x, int i) 37 | { 38 | if(pr[i]==MS-1) 39 | { 40 | printf("\nQueue with priority %d is full\n",i+1); 41 | return; 42 | } 43 | else{ 44 | pr[i]++; 45 | pq[i][pr[i]]=x; 46 | printf("\nelement %d with priority %d is successfully inserted into the queue\n",x,i+1); 47 | if(pf[i]==-1)pf[i]=0; 48 | } 49 | } 50 | void delete() 51 | { 52 | int t,i; 53 | for(i=0;i<3;i++) 54 | { 55 | if(pf[i]==-1)printf("\nqueue with priority %d is empty\n",i+1); 56 | else 57 | { 58 | t=pq[i][pf[i]]; 59 | if(pf[i]==pr[i])pf[i]=pr[i]=-1; 60 | else 61 | pf[i]++; 62 | printf("\nelement %d with priority %d is successfully deleted \n",t,i+1); 63 | break; 64 | 65 | } 66 | } 67 | 68 | 69 | } 70 | void display() 71 | { 72 | int i,j; 73 | for(i=0;i<3;i++) 74 | { 75 | if(pf[i]==-1) 76 | printf("queue with priority %d is empty",i+1); 77 | else 78 | { 79 | printf("\nElements in the queue with priority %d:\n",i+1); 80 | for(j=pf[i];j<=pr[i];j++) 81 | printf("%d\t",pq[i][j]); 82 | } 83 | } 84 | 85 | 86 | 87 | } -------------------------------------------------------------------------------- /priqueue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX_SIZE 2 4 | int pq[3][MAX_SIZE],pf[3]={-1,-1,-1},pr[3]={-1,-1,-1}; 5 | void main() 6 | { 7 | int ch,x,p; 8 | void pqinsert(int,int); 9 | void pqdelete(); 10 | void pqdisplay(); 11 | while(1) 12 | { 13 | printf("\n enter your choice:\n 1.insert\n 2.delete\n 3.display\n 4.exit\n"); 14 | scanf("%d",&ch); 15 | switch(ch) 16 | { 17 | case 1: printf("enter the element to be inserted and priority\n"); 18 | scanf("%d%d",&x,&p); 19 | pqinsert(x,p-1); 20 | break; 21 | case 2:pqdelete(); 22 | break; 23 | case 3:pqdisplay(); 24 | break; 25 | case 4:printf("\n thank you\n"); 26 | getchar(); 27 | exit(0); 28 | default :printf("\n invalid choice\n"); 29 | 30 | } 31 | 32 | } 33 | } 34 | void pqinsert(int x,int i) 35 | { 36 | if(pr[i]==MAX_SIZE-1) 37 | { 38 | printf("queue with priority %d is full, insertion is not possiblr\n",i+1); 39 | return; 40 | } 41 | pr[i]++; 42 | pq[i][pr[i]]=x; 43 | printf("element %d is inserted into queue with priority %d \n",x,i+1); 44 | if(pf[i]==-1) 45 | pf[i]=0; 46 | } 47 | 48 | void pqdelete() 49 | { 50 | int temp,i; 51 | for(i=0;i<3;i++) 52 | { 53 | if(pf[i]==-1) 54 | printf("\n queue %d is empty\n",i+1); 55 | else 56 | { 57 | temp=pq[i][pf[i]]; 58 | if(pf[i]==pr[i]) 59 | pf[i]=pr[i]=-1; 60 | else 61 | pf[i]++; 62 | printf("\nelement %d is deleted from queue with the priority %d\n",temp,i+1); 63 | break; 64 | } 65 | } 66 | } 67 | void pqdisplay() 68 | { 69 | int i,j; 70 | for(i=0;i<3;i++) 71 | { 72 | if(pf[i]==-1) 73 | printf("\n queue with priority %d is empty\n",i+1); 74 | else 75 | { 76 | printf("\n elements in queue with a priority %d :\n",i+1); 77 | for(j=pf[i];j<=pr[i];j++) 78 | printf("%d\t",pq[i][j]); 79 | } 80 | } 81 | } 82 | 83 | 84 | -------------------------------------------------------------------------------- /queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MS 5 4 | int q[MS],f=-1,r=-1; 5 | void main() 6 | { 7 | void qinsert(int); 8 | void delete(); 9 | void display(); 10 | int x,ch; 11 | while(1) 12 | { 13 | printf("Enter your choice\n1:insert\n2:delete\n3:display\n4:exit\n"); 14 | scanf("%d",&ch); 15 | switch(ch) 16 | { 17 | case 1:printf("Enter the element to be inserted into the queue\n"); 18 | scanf("%d",&x); 19 | qinsert(x); 20 | break; 21 | 22 | case 2:delete(); 23 | break; 24 | 25 | case 3:display(); 26 | break; 27 | 28 | case 4: printf("Thank you\n"); 29 | exit(0); 30 | 31 | default: printf("Invalid choice\n"); 32 | 33 | } 34 | } 35 | 36 | } 37 | void qinsert(int x) 38 | { 39 | if(r==MS-1) 40 | { 41 | printf("queue is full\n"); 42 | return; 43 | } 44 | r++; 45 | q[r]=x; 46 | if(f==-1)f=0; 47 | printf("Element %d is successfully inserted\n",x); 48 | } 49 | 50 | void delete() 51 | { 52 | int t; 53 | if(f==-1) 54 | { 55 | printf("queue is empty\n"); 56 | return; 57 | } 58 | t=q[f]; 59 | if(f==r) 60 | f=r=-1; 61 | else 62 | f++; 63 | printf("element %d is successfully deleted from the queue\n",t); 64 | } 65 | void display() 66 | { 67 | int t; 68 | if(f==-1) 69 | { 70 | printf("queue is empty\n"); 71 | return; 72 | } 73 | else 74 | { 75 | printf("Elements in the queue are:\n"); 76 | for(t=f;t<=r;t++)printf("%d\t",q[t]); 77 | } 78 | 79 | 80 | } 81 | -------------------------------------------------------------------------------- /sll.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef struct sll 5 | { 6 | int id; 7 | char name[25]; 8 | int sem; 9 | struct sll *link; 10 | }node; 11 | node *first = NULL; 12 | void main() 13 | { 14 | int id,sem,n,i,ch,pos; 15 | char name[25]; 16 | void insfront(int,char name[],int); 17 | void insend(int,char name[], int); 18 | void inspos(int, char name[], int , int); 19 | void delid(int); 20 | void search_up(int); 21 | void display(); 22 | while(1) 23 | { 24 | printf("Enter your choice:\n1:Create\n2:Insertion at front\n3:Insertion at the end\n4:insertion at given pos\n5:delete using id\n6:search and update\n7:display\n8:exit\n"); 25 | scanf("%d",&ch); 26 | switch(ch) 27 | { 28 | case 1:printf("\nEnter the number nodes to be initially created\n"); 29 | scanf("%d",&n); 30 | for(i=0;iid=id; 82 | strcpy(new->name,name); 83 | new->sem=sem; 84 | new->link=NULL; 85 | if(first == NULL)first=new; 86 | else 87 | { 88 | new->link = first; 89 | first = new; 90 | } 91 | 92 | 93 | } 94 | void insend(int id,char name[25], int sem) 95 | { 96 | node *new,*temp; 97 | new=(node*)malloc(sizeof(node)); 98 | new->id=id; 99 | strcpy(new->name,name); 100 | new->sem=sem; 101 | new->link=NULL; 102 | if(first = NULL) 103 | { 104 | first = new; 105 | } 106 | else 107 | { 108 | temp = first; 109 | while(temp->link!=NULL) 110 | temp=temp->link; 111 | temp->link = new; 112 | } 113 | 114 | } 115 | void inspos(int id, char name[25], int sem , int pos) 116 | { 117 | int i; 118 | node *new,*temp,*prev; 119 | new=(node*)malloc(sizeof(node)); 120 | new->id=id; 121 | strcpy(new->name,name); 122 | new->sem=sem; 123 | new->link=NULL; 124 | temp=first; 125 | for(i=0;i<=pos;i++) 126 | { 127 | prev = temp; 128 | temp = temp->link; 129 | } 130 | if(pos == 1) 131 | { 132 | new->link = first; 133 | first = new; 134 | } 135 | else{ 136 | prev->link = new; 137 | new->link = temp; 138 | } 139 | 140 | } 141 | void delid(int id) 142 | { 143 | node *temp,*prev; 144 | if(first == NULL) 145 | { 146 | printf("list is empty\n"); 147 | return; 148 | } 149 | else{ 150 | temp = first; 151 | while((temp!=NULL)&&(temp->id!=id)) 152 | { 153 | prev = temp; 154 | temp=temp->link; 155 | } 156 | if(temp == NULL) 157 | { 158 | printf("Student with id %d isnot found",id); 159 | return; 160 | } 161 | else if(temp == first) 162 | { 163 | first = first->link; 164 | free(first); 165 | } 166 | else{ 167 | prev->link=temp->link; 168 | free(temp); 169 | } 170 | } 171 | 172 | } 173 | void search_up(int id) 174 | { 175 | node *temp; 176 | temp = first; 177 | while((temp!=NULL)&&(temp->id!=id)) 178 | temp = temp->link; 179 | if(temp == NULL) 180 | { 181 | printf("Student with id %d not found",id); 182 | } 183 | else 184 | { 185 | 186 | printf("Enter the id, name and sem of the student to update details\n"); 187 | scanf("%d%c%d",&temp->id,temp->name,&temp->sem); 188 | } 189 | 190 | 191 | } 192 | void display() 193 | { 194 | node *temp; 195 | if(first == NULL) 196 | { 197 | printf("list is empty\n"); 198 | return; 199 | } 200 | else 201 | { 202 | temp = first; 203 | printf("Contents of list are:\n"); 204 | while(temp!=NULL) 205 | { 206 | printf("%d%s%d",temp->id,temp->name,temp->sem); 207 | temp=temp->link; 208 | } 209 | } 210 | 211 | } 212 | -------------------------------------------------------------------------------- /stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MS 5 4 | int s[MS],top=-1; 5 | void main() 6 | { 7 | void push(int); 8 | void pop(); 9 | void display(); 10 | int x,ch; 11 | while(1) 12 | { 13 | printf("Enter your choice\n1:push\n2:pop\n3:display\n4:exit\n"); 14 | scanf("%d",&ch); 15 | switch(ch) 16 | { 17 | case 1:printf("Enter the element to be inserted into the stack\n"); 18 | scanf("%d",&x); 19 | push(x); 20 | break; 21 | 22 | case 2:pop(); 23 | break; 24 | 25 | case 3: display(); 26 | break; 27 | 28 | case 4:printf("thank you\n"); 29 | exit(0); 30 | 31 | default: printf("Invalid choice\n"); 32 | } 33 | } 34 | 35 | } 36 | void push(int x) 37 | { 38 | if(top == MS-1)printf("Stack is full\n"); 39 | else 40 | { 41 | s[++top]=x; 42 | printf("Element %d is successfully inserted into the stack\n",x); 43 | 44 | } 45 | } 46 | void pop() 47 | { 48 | int temp; 49 | if(top == -1)printf("Stack is empty\n"); 50 | else{ 51 | temp = s[top]; 52 | top--; 53 | printf("Element %d is successfully deleted from the stack",temp); 54 | 55 | } 56 | } 57 | void display() 58 | { 59 | int i; 60 | if(top == -1)printf("Stack is empty\n"); 61 | else 62 | { 63 | printf("Elements in the stack are:\n"); 64 | for(i=0;i<=top;i++)printf("%d\t",s[i]); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /toh.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int c; 4 | void main() 5 | { 6 | int n; 7 | void toh(int,char,char,char); 8 | printf("Enter the number of disks\n"); 9 | scanf("%d",&n); 10 | toh(n,'A','c','B'); 11 | printf("Total moves = %d",c); 12 | } 13 | void toh(int n,char s,char d,char a) 14 | { 15 | if(n==1) 16 | { 17 | printf("\nMove disk %d from peg %c to peg %c\n",n,s,d); 18 | c++; 19 | return; 20 | } 21 | toh(n-1,s,a,d); 22 | printf("\nMove disk %d from peg %c to peg %c\n",n,s,d); 23 | c++; 24 | toh(n-1,a,s,d); 25 | } --------------------------------------------------------------------------------