Qx19. WAP to perform Quick Sort.

posted May 2, 2011, 11:46 AM by Neil Mathew   [ updated May 2, 2011, 11:50 AM ]


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<stdio.h>
 
void QuickSort(int *a, int first, int last)
{
     int low, high, pivot,temp,i;
     
     low=first;
     high=last;
     int pivot_pos= (int) (low+high)/2;
     
     pivot=a[pivot_pos];
     
printf("\n\n | First=%d | Last=%d | Pivot=%d | \n",first, last, pivot);
     
     while( low < high )
     {
      
       printf("\n Pass : ");
       for(i=first; i<=last; i++)
       printf(" %d",a[i]);
       
      
      while( a[low] < pivot )
      { low++; }
      
      while( a[high] > pivot )
      { high--;}
      
      if(low<high)
      { //swapping a[low] and a[high]
       temp=a[low];
       a[low]=a[high];
       a[high]=temp;
      }
         
     }
          
       if( first < high-1  )
       QuickSort(a,first,high-1); //last=high-1
     
       if( last > low+1 )
       QuickSort(a,low+1,last); //first=low+1
     
     
}  
 
 
int main()
{
    int i;
    int n;
    int a[20];
    
    printf("\n Enter the number of elements: ");
    scanf("%d",&n);
    
    printf("\n Enter the elements: ");
    for(i=0; i<n; i++)
    scanf("%d",&a[i]);
    
    printf("\n\n Steps in Sorting: ");
    QuickSort(a,0,n-1);
    
    printf("\n\n After Sorting:");
    for(i=0; i<n; i++)
    printf(" %d",a[i]);
    
}
 

OUTPUT:

  
 Enter the number of elements: 6

 Enter the elements: 6 0 8 2 4 10


 Steps in Sorting:

 | First=0 | Last=5 | Pivot=8 |

 Pass :  6 0 8 2 4 10
 Pass :  6 0 4 2 8 10

 | First=0 | Last=3 | Pivot=0 |

 Pass :  6 0 4 2
 Pass :  0 6 4 2

 | First=1 | Last=3 | Pivot=4 |

 Pass :  6 4 2
 Pass :  2 4 6

 After Sorting: 0 2 4 6 8 10






Qxx1. WAP to convert decimal to binary using recursion.

posted Apr 26, 2011, 8:59 AM by Neil Mathew   [ updated Apr 26, 2011, 9:07 AM ]

SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<stdio.h>
 
void Dec2Bin(int num)
{
    int z;
    
    if(num>=0)
    {
    // conditions to find the remainder 
    // then recall (if not at end) then print
         
    if(num<2 && num>=0)
    {
    z=num;
    }
    else
    {
    z=num%2;
    num/=2;
    Dec2Bin(num);
    }  
    printf("%d",z);
 
    }
}
 
 
int main()
{
    int num;
    scanf("%d",&num);
    
    printf("\n Binary: ");
    Dec2Bin(num);
    
}
 

OUTPUT:

9

 Binary: 1001



Qx18. WAP to add two polynomials using Header Linked Lists

posted Mar 19, 2011, 2:13 AM by Neil Mathew   [ updated Mar 19, 2011, 2:21 AM ]


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include<stdlib.h>
 
struct node
{
       int coeff; //coefficient
       int exp;  //exponent
       struct node * next;
 
}*ptr;
 
void INS(int exp,int coeff,struct node* start)
{
       if( coeff!=0 ) // If coeff is zero, node need not be created.
      {  
        
        struct node* newnode;
        newnode=(struct node*) malloc( sizeof(struct node) );
        
        newnode->coeff=coeff;
        newnode->exp=exp;
        newnode->next=NULL;
        
        ptr=start;
        while(ptr->next!=NULL) //points ptr to the last node
        ptr=ptr->next;
        
        ptr->next = newnode;
        
      }
}
 
void INPUT(struct node* start)
{
     int exp,coeff;
      
      printf("\n Enter the highest exponent: ");
      scanf("%d",&exp);
      printf("\n");
      
      while(exp>=0)
      {       
       printf(" Enter the Coefficient of X^%d : ",exp);
       scanf("%d",&coeff);
       
       INS(exp,coeff,start);
       exp--;
      }; 
 
}
 
void ADD(struct node*start1,struct node*start2,struct node*start3)
{
     struct node *ptr1;
     struct node *ptr2;
     
     ptr1=start1->next;
     ptr2=start2->next;
     
        // I based it on the same principle followed in Merge Sort
        
     while(ptr1!=NULL && ptr2!=NULL) 
     {
     
      if( ptr1->exp > ptr2->exp )
      {
          INS(ptr1->exp, ptr1->coeff, start3);
          ptr1=ptr1->next;
      }
      else if( ptr2->exp > ptr1->exp )
      {
          INS(ptr2->exp, ptr2->coeff, start3);
          ptr2=ptr2->next;         
      }
      else // IF ptr1->exp = ptr2->exp
      { 
          INS( ptr1->exp, (ptr1->coeff + ptr2->coeff), start3);   
          ptr1=ptr1->next;
          ptr2=ptr2->next;
      }
     }
     
     while( ptr1!=NULL )
     {
          INS(ptr1->exp, ptr1->coeff, start3);
          ptr1=ptr1->next;
     }
     
     while( ptr2!=NULL )
     {
          INS(ptr2->exp, ptr2->coeff, start3);
          ptr2=ptr2->next;
     }
}
 
void DISPLAY(struct node *start)
{
     if(start->next==NULL)
     printf(" No Nodes. ");
     else
     {
         ptr=start;
         ptr=ptr->next; //Header Node's values not considered.
                 
        do
        {
                   
          if(ptr->exp==0) //formatting for constant
          printf(" %d",ptr->coeff); 
          else
          printf(" %dX^%d",ptr->coeff, ptr->exp);
          
          ptr=ptr->next;      
          
          if(ptr!=NULL)
          printf(" +");
        
        }
        while(ptr!=NULL);
        
     }
 
 
}
 
main()
{      
      struct node * start1;
      struct node * start2;
      struct node * start3;
      
      //Declaring the Header Node of First Polynomial.
      struct node* newnode1;
      newnode1= (struct node *) malloc( sizeof(struct node) );
      
      newnode1->coeff=0;
      newnode1->exp=-1;
      newnode1->next=NULL;
      
      if(newnode1==NULL)
      {
      printf(" No Memory available. ");
      exit(0);
      }
      
      start1=newnode1;
      
      //Declaring the Header Node of Second Polynomial.
      struct node* newnode2;
      newnode2= (struct node *) malloc( sizeof(struct node) );
      
      newnode2->coeff=0;
      newnode2->exp=-1;
      newnode2->next=NULL;
      
      if(newnode2==NULL)
      {
      printf(" No Memory available. ");
      exit(0);
      }
      
      start2=newnode2;
      
      //Declaring the Header Node of Resultant Polynomial.
      struct node* newnode3;
      newnode3= (struct node *) malloc( sizeof(struct node) );
      
      newnode3->coeff=0;
      newnode3->exp=-1;
      newnode3->next=NULL;
      
      if(newnode3==NULL)
      {
      printf(" No Memory available. ");
      exit(0);
      }
      
      start3=newnode3;
      
      //Others
      printf("\n The polynomial in the format :");
      printf(" 5X^3 + 2X^2 + 7X^1 + 13 ");
      
      printf("\n\n First Polynomial: ");
      INPUT(start1);
      
      printf("\n Second Polynomial: ");
      INPUT(start2);    
      
      printf("\n Polynomial 1: ");
      DISPLAY(start1);
      
      printf("\n Polynomial 2: ");
      DISPLAY(start2);
      
      ADD(start1, start2, start3);
      
      printf("\n\n After Addition: ");
      printf("\n Resultant Polynomial: ");
      DISPLAY(start3);
      
      //getch();
      
}
     
 

OUTPUT:


 The polynomial in the format : 5X^3 + 2X^2 + 7X^1 + 13

 First Polynomial:
 Enter the highest exponent: 3

 Enter the Coefficient of X^3 : 12
 Enter the Coefficient of X^2 : 15
 Enter the Coefficient of X^1 : 0
 Enter the Coefficient of X^0 : 20

 Second Polynomial:
 Enter the highest exponent: 5

 Enter the Coefficient of X^5 : 20
 Enter the Coefficient of X^4 : 20
 Enter the Coefficient of X^3 : 8
 Enter the Coefficient of X^2 : 5
 Enter the Coefficient of X^1 : 20
 Enter the Coefficient of X^0 : 0

 Polynomial 1:  12X^3 + 15X^2 + 20
 Polynomial 2:  20X^5 + 20X^4 + 8X^3 + 5X^2 + 20X^1

 After Addition:
 Resultant Polynomial:  20X^5 + 20X^4 + 20X^3 + 20X^2 + 20X^1 + 20

Qx17. WAP to perform Merge Sort.

posted Mar 15, 2011, 4:14 AM by Neil Mathew   [ updated Mar 15, 2011, 10:04 AM ]


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<stdio.h>
 
void MergeSort(int *a, int m, int *b, int n, int *c)
{
     int i=0,j=0,k=0;
     
     while( i<m && j<n)
     {
            if( a[i] <= b[j] )
            {
                c[k++]=a[i++];
            }
            else
            {
                c[k++]=b[j++];
            }
     }
     
     if( i<m )
     {
         while( i<m )
         c[k++]=a[i++];
     }
     else if( j<n )
     {
          while(j<n)
          c[k++]=b[j++];
     }
     
}
 
 
 
int main()
{
    int a[50],b[50],c[100];
    int i,m,n;
    
    printf("\n Enter the Size of Array A: ");
    scanf("%d",&m);
    
    printf("\n Enter the Elements (In Ascending Order) : ");
    for(i=0; i<m; i++)
    scanf("%d",&a[i]);
    
    printf("\n Enter the Size of Array B: ");
    scanf("%d",&n);
    
    printf("\n Enter the Elements (In Ascending Order) : ");
    for(i=0; i<n; i++)
    scanf("%d",&b[i]);
    
    MergeSort(a,m,b,n,c);
    
    printf("\n The Merged & Sorted Array C: ");
    for(i=0; i<(m+n); i++)
    printf(" %d",c[i]);
    
}
    
    
    
 

OUTPUT:



 Enter the Size of Array A: 6

 Enter the Elements (In Ascending Order) : 0 2 4 6 8 10

 Enter the Size of Array B: 5

 Enter the Elements (In Ascending Order) : 1 3 5 7 9

 The Merged & Sorted Array C:  0 1 2 3 4 5 6 7 8 9 10


Qx16. WAP to perform searching, sorting, insertion and deletion (Sorted) in Array.

posted Mar 15, 2011, 1:41 AM by Neil Mathew   [ updated Mar 15, 2011, 3:23 AM ]

SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <stdio.h>
 
int n; //size of array
 
int BSearch(int *a,int ITEM) 
{
    int beg=0,end=n-1;
    int mid;
    
    while( beg<=end )
    {
     mid=(beg+end)/2;
     
     if(a[mid]==ITEM)
     return mid;
     else if( a[mid] < ITEM )
     beg=mid+1;
     else
     end=mid-1;
    }
    
    return -1;
}
 
void SSort(int *a)
{
    int i,j,temp;
    
    for(i=0; i<n-1; i++)
    {
     for(j=i+1; j<n; j++)
     {  
      if(a[i]>a[j])
      {
       temp=a[i];
       a[i]=a[j];
       a[j]=temp;
      }
     }
    }
    
}
 
void DEL(int *a, int ITEM)
{
     int i;
     int pos=BSearch(a, ITEM);
     
     if(pos!=-1)
     {
     for(i=pos; i<n-1; i++)
     a[i]=a[i+1];
     n--;
     printf("\n Item Deleted. ");
     }
     else
     printf("\n Item not found. ");
}
 
void INS(int *a, int ITEM)
{
     int pos,i;
     
     //Finding the proper position for Sorted Array
     if(ITEM>a[n-1])
     pos=n;
     else if(ITEM<a[0])
     pos=0;
     else
     {
         for(i=0; i<n-1; i++)
         if(a[i]<=ITEM && a[i+1]>=ITEM)
         {
          pos=i+1; 
          break;
         }
     }
     
     //Shifting of elements to Insert the element.
     for(i=n; i>pos; i--)
     a[i]=a[i-1];
     
     a[pos]=ITEM;
     n++;
     printf("\n Item Inserted. ");
}
    
void Display(int *a)
{
      int i;
      for(i=0; i<n; i++)
      printf(" %d",a[i]);
} 
  
int main()
{
    int a[100],ch,pos,ITEM,i;
    
    printf("\n Enter the number of Elements: ");
    scanf("%d",&n);
    
    printf("\n Enter the Elements: ");
    for(i=0; i<n; i++)
    scanf("%d",&a[i]);
    
    do
    {
     printf("\n ______________________________________ ");
     printf("\n MENU:\n");
     printf("\n 1. Insert an element. ");
     printf("\n 2. Delete an element. ");
     printf("\n 3. Search for an element. ");
     printf("\n 4. Display elements. ");
     printf("\n 0. Exit. ");
     printf("\n Your Choice: ");
     scanf("%d",&ch);
     
     switch(ch)
     {
                      
      case 1: //INSERT
      printf("\n Enter the Item to Insert: ");
      scanf("%d",&ITEM);
      
      SSort(a);
      INS(a,ITEM);
      Display(a);
      
      break;
      
      case 2: //DELETE
      printf("\n Enter the Item to Delete: ");
      scanf("%d",&ITEM);
      
      SSort(a);
      DEL(a,ITEM);    
      Display(a);  
      
      break;
      
      case 3: //SEARCH
      printf("\n Enter the Item to Search: ");
      scanf("%d",&ITEM);
      
      SSort(a);
      pos=BSearch(a,ITEM);
      
      if(pos==-1)
      printf("\n Item not Found. ");
      else
      printf("\n Item found at %d position. ",pos+1);
      
      break;
 
      case 4: //DISPLAY
      printf("\n The Elements are: ");
      
      SSort(a);
      Display(a);
      
      break;
      
      case 0:
      printf("\n Exiting... ");
      break;
      }
 
      }while(ch!=0);
 
}
 

OUTPUT:


 Enter the number of Elements: 5

 Enter the Elements: 10 2 8 4 6

 ______________________________________
 MENU:

 1. Insert an element.
 2. Delete an element.
 3. Search for an element.
 4. Display elements.
 0. Exit.
 Your Choice: 4

 The Elements are:  2 4 6 8 10
 ______________________________________
 MENU:

 1. Insert an element.
 2. Delete an element.
 3. Search for an element.
 4. Display elements.
 0. Exit.
 Your Choice: 1

 Enter the Item to Insert: 7

 Item Inserted.  2 4 6 7 8 10
 ______________________________________
 MENU:

 1. Insert an element.
 2. Delete an element.
 3. Search for an element.
 4. Display elements.
 0. Exit.
 Your Choice: 3

 Enter the Item to Search: 8

 Item found at 5 position.
 ______________________________________
 MENU:

 1. Insert an element.
 2. Delete an element.
 3. Search for an element.
 4. Display elements.
 0. Exit.
 Your Choice: 2

 Enter the Item to Delete: 4

 Item Deleted.  2 6 7 8 10
 ______________________________________
 MENU:

 1. Insert an element.
 2. Delete an element.
 3. Search for an element.
 4. Display elements.
 0. Exit.
 Your Choice: 0

 Exiting...

Qx15. WAP to Add, Subtract, Multiply two 2D Arrays.

posted Mar 15, 2011, 1:41 AM by Neil Mathew   [ updated Mar 15, 2011, 1:45 AM ]


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<stdio.h>
 
void Matrix_Display(int a[][20],int n)
{
     int i,j;
     for(i=0; i<n; i++)
     {        
      for(j=0; j<n; j++)
       {
        printf(" %d",a[i][j]);   
       }
      printf("\n");
     }
 
}
 
int main()
{
    int n,i,j,k;
    int a[20][20];
    int b[20][20];
    int c[20][20];
    
    printf("\n Enter the dimensions of the 2 Square matrices: ");
    scanf("%d",&n);
    
    printf("\n Enter elements of Matrix A: ");
    for(i=0; i<n; i++)
    for(j=0; j<n; j++)
    scanf("%d",&a[i][j]);
    
    printf("\n Enter elements of Matrix B: ");
    for(i=0; i<n; i++)
    for(j=0; j<n; j++)
    scanf("%d",&b[i][j]);
    
    printf("\n Matrix A: \n");
    Matrix_Display(a,n);
           
    printf("\n\n Matrix B: \n");
    Matrix_Display(b,n);
    
    //Addition
    for(i=0; i<n; i++)
    for(j=0; j<n; j++)
    c[i][j]=a[i][j]+b[i][j];
    
    printf("\n\n Addition of A and B gives: \n");
    Matrix_Display(c,n);
    
     //Subtraction
    for(i=0; i<n; i++)
    for(j=0; j<n; j++)
    c[i][j]=a[i][j]-b[i][j];
    
    printf("\n\n Subtraction of A and B gives: \n");
    Matrix_Display(c,n);
    
    //Multiplication
    for(i=0; i<n; i++)
    for(j=0; j<n; j++)
    {
     c[i][j]=0;
     for(k=0; k<n; k++)
     c[i][j]+=a[i][k]*b[k][j];
    }
    
    printf("\n\n Multiplication of A and B gives: \n");
    Matrix_Display(c,n);
       
}
 

OUTPUT:

 Enter the dimensions of the 2 Square matrices: 2

 Enter elements of Matrix A: 1 2 3 4

 Enter elements of Matrix B: 4 3 2 1

 Matrix A:
 1 2
 3 4


 Matrix B:
 4 3
 2 1


 Addition of A and B gives:
 5 5
 5 5


 Subtraction of A and B gives:
 -3 -1
 1 3


 Multiplication of A and B gives:
 8 5
 20 13


Qx14. WAP to implement Doubly LL.

posted Mar 13, 2011, 10:24 AM by Neil Mathew   [ updated Mar 13, 2011, 10:41 AM ]


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <stdlib.h>
 
int count;
 
struct node
{
       int info;
       struct node* next;
       struct node* prev;
}*start,*ptr;
 
int INS( int ITEM, int pos)
{
    struct node * newnode;
    newnode= (struct node*) malloc ( sizeof(struct node) );
    newnode->info=ITEM;
    newnode->next=NULL;
    newnode->prev=NULL;
    
    // ERROR HANDLING #1 - Overflow?
    if(newnode==NULL)
    {
     printf("\n No Memory Left to create a node. ");
     return 0;
    }
    
    //ERROR HANDLING #2 - Position Overlimits
    if( pos<=0 || pos>count+1 )
    {
     printf("\n Position Entered Over limits. ");
     return 0;
    }
 
    // Condition 1: Entering the first node of LL
    if(start==NULL)
    {
     start=newnode;
     count=1;
    }
    // Condition 2: Entering node to the first position
    else if(pos==1)
    {
     newnode->next=start;
     start->prev=newnode;
     start=newnode;
     count++;
    }
    // Condition 3: Enterting a node to last position
    else if(pos==count+1)
    {
     int i;
     ptr=start;
     for(i=2; i<=pos-1; i++)
     ptr=ptr->next;
     
     newnode->next=ptr->next;
     newnode->prev=ptr;
     ptr->next=newnode;
     count++;
    }
    // Condition 4: Entering a node to any other position
    else
    {
     int i;
     ptr=start;
     for(i=2; i<=pos-1; i++)
     ptr=ptr->next;
     
     newnode->next=ptr->next;
     newnode->prev=ptr;
     (ptr->next)->prev=newnode; // Note Line.
     ptr->next=newnode;
     count++;
    }
    
 return 1;
}
 
int DEL(int pos)
{
    struct node *temp;
    
    // ERROR HANDLING #1 - Underflow
    if(start==NULL)
    {
     printf("\n There are no Nodes to delete. ");
     return 0;
    }
    
    // ERROR HANDLING #2 - Position Overlimits
    if( pos<=0 || pos>count )
    {
     printf("\n Position Entered Over limits. ");
     return 0;
    }
    
    // Condition 1: Deleting Node at 1st position OR Last Node in LL.
    if(pos==1)
    {
     temp=start;
     start=start->next;
     free(temp);
     count--;
    }
    // Condition 2: Deleting node at last position
    else if(pos==count)
    {
     for(ptr=start; ptr->next!=NULL; ptr=ptr->next);
     
     (ptr->prev)->next=NULL;
     printf("\n Deleting Node with value = %d" , ptr->info);
     free(ptr);
     
     count--;
    }   
    // Condition 3; Deleting node at any other position
    else
    {
     int i;
     ptr=start;
     for(i=2; i<=pos; i++)
     ptr=ptr->next;
     
     (ptr->prev)->next=ptr->next;
     (ptr->next)->prev=ptr->prev;
     printf("\n Deleting Node with value = %d",ptr->info);
     free(ptr);
     count--;
    }
    return 1;
}
int F_Traverse()
{
    if(start==NULL)
    {
     printf("\n There are no elements in this Linked List.");
     return 0;
    }
    
    printf("\n [");
    for(ptr=start; ptr!=NULL; ptr=ptr->next)
    printf(" - %d",ptr->info);
    printf(" - ]\n");
 
    return 1;
}
 
int R_Traverse()
{
    if(start==NULL)
    {
     printf("\n There are no elements in this Linked List.");
     return 0;
    }
    
    for(ptr=start; ptr->next!=NULL; ptr=ptr->next);
    
    printf("\n [");
    for(; ptr!=NULL; ptr=ptr->prev)
    printf(" - %d",ptr->info);
    printf(" - ]\n");
 
    return 1;
}
 
int Search(int ITEM)
{
    int i;
    for(i=1,ptr=start; ptr!=NULL; ptr=ptr->next,i++)
    if(ptr->info==ITEM)
    return i;
    
    return -1;
}
 
int main()
{
    int ch,success;
    int ITEM; int pos=1;
    
    start=NULL;
    count=0;
    
    do
    {
    printf("\n ___________________________________________________ ");
    printf("\n MENU: ");
    printf("\n\n 1. Insert a Node. ");
    printf("\n 2. Delete a Node. ");
    printf("\n 3. Forward Traversing the Doubly LL. ");
    printf("\n 4. Reverse Traversing the Doubly LL. ");
    printf("\n 5. Search for a node. ");
    printf("\n 0. EXIT. ");
    printf("\n\n Your Choice: ");
    scanf("%d", &ch);
 
    switch(ch)
    {
              case 1: //INSERT
              
              printf("\n Enter the Item to Insert: ");
              scanf("%d",&ITEM);
              
              if(start!=NULL)
              {
              printf("\n Enter the position: ");
              scanf("%d", &pos);
              }
              
              success=INS(ITEM, pos);
              break;
              
              case 2: //DELETE
              
              if(start!=NULL)
              {
              printf("\n Enter the position: ");
              scanf("%d", &pos);
              }
              success=DEL(pos);
              break;
              
              case 3: //Forward Traversing
              printf("\n The Elements: ");
              success=F_Traverse();
              break;
              
              case 4: //Reverse Traversing
              printf("\n The Elements: \n");
              success=R_Traverse();
              break;
              
              case 5: //Searching
              
              printf("\n Enter the Item to Search for: ");
              scanf("%d",&ITEM);
              pos=Search(ITEM);
              
              if(pos!=-1)
              printf("\n The Item is found at %d Position",pos);
              else
              printf("\n Item not found. ");
              
              break;
              
              case 0: 
              printf("\n Exiting... ");
    
    }
    }while(ch);
    
     return 1;
}
 

OUTPUT:

 ___________________________________________________
MENU: 1. Insert a Node. 2. Delete a Node. 3. Forward Traversing the Doubly LL. 4. Reverse Traversing the Doubly LL. 5. Search for a node. 0. EXIT. Your Choice: 1 Enter the Item to Insert: 23 ___________________________________________________ MENU: 1. Insert a Node. 2. Delete a Node. 3. Forward Traversing the Doubly LL. 4. Reverse Traversing the Doubly LL. 5. Search for a node. 0. EXIT. Your Choice: 1 Enter the Item to Insert: 12 Enter the position: 1 ___________________________________________________ MENU: 1. Insert a Node. 2. Delete a Node. 3. Forward Traversing the Doubly LL. 4. Reverse Traversing the Doubly LL. 5. Search for a node. 0. EXIT. Your Choice: 1 Enter the Item to Insert: 90 Enter the position: 3 ___________________________________________________ MENU: 1. Insert a Node. 2. Delete a Node. 3. Forward Traversing the Doubly LL. 4. Reverse Traversing the Doubly LL. 5. Search for a node. 0. EXIT. Your Choice: 3 The Elements: [ - 12 - 23 - 90 - ] ___________________________________________________ MENU: 1. Insert a Node. 2. Delete a Node. 3. Forward Traversing the Doubly LL. 4. Reverse Traversing the Doubly LL. 5. Search for a node. 0. EXIT. Your Choice: 4 The Elements: [ - 90 - 23 - 12 - ] ___________________________________________________ MENU: 1. Insert a Node. 2. Delete a Node. 3. Forward Traversing the Doubly LL. 4. Reverse Traversing the Doubly LL. 5. Search for a node. 0. EXIT. Your Choice: 5 Enter the Item to Search for: 12 The Item is found at 1 Position ___________________________________________________ MENU: 1. Insert a Node. 2. Delete a Node. 3. Forward Traversing the Doubly LL. 4. Reverse Traversing the Doubly LL. 5. Search for a node. 0. EXIT. Your Choice: 2 Enter the position: 1 ___________________________________________________ MENU: 1. Insert a Node. 2. Delete a Node. 3. Forward Traversing the Doubly LL. 4. Reverse Traversing the Doubly LL. 5. Search for a node. 0. EXIT. Your Choice: 3 The Elements: [ - 23 - 90 - ] ___________________________________________________ MENU: 1. Insert a Node. 2. Delete a Node. 3. Forward Traversing the Doubly LL. 4. Reverse Traversing the Doubly LL. 5. Search for a node. 0. EXIT. Your Choice: 0
 
Exiting...


Qx13. WAP to implement Singly Linked Lists.

posted Mar 12, 2011, 7:32 AM by Neil Mathew   [ updated Mar 12, 2011, 7:37 AM ]


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include<stdio.h>
 
int count; // Total number of nodes in Singly Linked List.
 
struct node
{
       int info;
       struct node * next;
}*start,*ptr;
 
int INS(int ITEM, int pos)
{
    
     struct node * newnode;
     newnode= (struct node *) malloc ( sizeof(struct node) ) ;
     
     newnode->info=ITEM;
     newnode->next=NULL;
     
     // ERROR HANDLING #1 - No Memory Left (Overflow)
     if( newnode == NULL )
     {
      printf(" OVERFLOW: No Memory Left to create newnode. ");
      return 0;
     }
     
     // ERROR HANDLING #2 - Position Over Limits
     if( pos<=0 || pos>count+1 )
     {
      printf(" Position entered is over limits. ");
      return 0;
     }
     
     // Condition 1: First node of LL
     if( start==NULL )
     {
      start=newnode;
      count=1;
     }
     // Condition 2: Adding node to first position of LL
     else if( pos==1)
     {
      newnode->next=start;
      start=newnode;
      count++;   
     }
     // Condition 3: Adding node to any other position of LL
     else
     {    
     
      int i;
      ptr=start;
 
      for(i=2; i<=pos-1; i++)
      ptr=ptr->next;
        
      newnode->next = ptr->next;
      ptr->next = newnode;
      
      count++;
     }
        
     return 1; //successful 
}
 
int DEL(int pos)
{
    struct node *temp;
    
    //ERROR HANDLING #1 - Underflow
    if(start == NULL)
    {
     printf(" UNDERFLOW: No Node left to delete. ");
     return 0;
    }
    
    //ERROR HANDLING #2 - Position Over Limits
    if( pos<=0 || pos>count )
    {
     printf(" Position entered is over limits. ");
     return 0;
    }
    
 
    
    // Condition 1: Deleting First Node OR Last Remaining Node
    if( pos==1 )
    {
     temp=start;
     start=start->next;
     free(temp);
     count--;
    }
    // Condition 2: Deleting any other node
    else
    {
     int i;
     ptr=start;
     for(i=2; i<=pos-1; i++)
     ptr=ptr->next;
    
     temp=ptr->next;
     ptr->next=temp->next;
     free(temp);
     
     count--;
    }    
    
    return 1;
}
    
 
int TRAVERSE()
{  
 
   ptr=start;
   
   // ERROR HANDLING
   if(start==NULL)
   {
    printf(" Linked List Empty. No Nodes to display. ");
    return 0;
   }
 
   // TRAVERSING 
   int i;
   printf("\n [");
   for(i=1; i<=count; i++, ptr=ptr->next) 
   { 
    printf(" - %d",ptr->info); 
   }
   printf(" - ]");
   
   return 1;
} 
 
 
int main()
{
    int item,pos=1;
    int ch,success;
    start=NULL;
    count=0;
        
    do
    {
    printf("\n\n MENU:");
    printf("\n 1. Insert a node. ");
    printf("\n 2. Delete a node. ");
    printf("\n 3. Display the Singly Linked List. ");
    printf("\n 4. EXIT. \n Your Choice: ");
    scanf("%d",&ch);
    
    switch(ch)
    {
    case 1: //INSERT
        
    printf("\n ENTER ITEM: ");
    scanf("%d", &item);
    
    if(start!=NULL)
    { 
     printf("\n ENTER POSITION: ");
     scanf("%d", &pos);
    }
    
    success=INS(item,pos);
    break;
    
    case 2: //DELETE
    
    if(start!=NULL)
    { 
     printf("\n ENTER POSITION: ");
     scanf("%d", &pos);
    }
    
    success=DEL(pos);
    break;
    
    case 3: //DISPLAY
    printf("\n The Nodes: \n");
    success=TRAVERSE();
    break;
    
    case 4: 
    printf("\n Exiting.. ");
    break;
    
    default: printf(" INVALID CHOICE. ");
    }
    
    }
    while(ch!=4); //end of do while loop
    
    return 0;
}
 

OUTPUT:



 MENU:
 1. Insert a node.
 2. Delete a node.
 3. Display the Singly Linked List.
 4. EXIT.
 Your Choice: 1

 ENTER ITEM: 34


 MENU:
 1. Insert a node.
 2. Delete a node.
 3. Display the Singly Linked List.
 4. EXIT.
 Your Choice: 1

 ENTER ITEM: 56

 ENTER POSITION: 2


 MENU:
 1. Insert a node.
 2. Delete a node.
 3. Display the Singly Linked List.
 4. EXIT.
 Your Choice: 1

 ENTER ITEM: 67

 ENTER POSITION: 3


 MENU:
 1. Insert a node.
 2. Delete a node.
 3. Display the Singly Linked List.
 4. EXIT.
 Your Choice: 3

 The Nodes:

 [ - 34 - 56 - 67 - ]

 MENU:
 1. Insert a node.
 2. Delete a node.
 3. Display the Singly Linked List.
 4. EXIT.
 Your Choice: 1

 ENTER ITEM: 12

 ENTER POSITION: 1


 MENU:
 1. Insert a node.
 2. Delete a node.
 3. Display the Singly Linked List.
 4. EXIT.
 Your Choice: 3

 The Nodes:

 [ - 12 - 34 - 56 - 67 - ]

 MENU:
 1. Insert a node.
 2. Delete a node.
 3. Display the Singly Linked List.
 4. EXIT.
 Your Choice: 1

 ENTER ITEM: 0

 ENTER POSITION: 6

 Position entered is over limits.

 MENU:
 1. Insert a node.
 2. Delete a node.
 3. Display the Singly Linked List.
 4. EXIT.
 Your Choice: 2

 ENTER POSITION: 3


 MENU:
 1. Insert a node.
 2. Delete a node.
 3. Display the Singly Linked List.
 4. EXIT.
 Your Choice: 3

 The Nodes:

 [ - 12 - 34 - 67 - ]

 MENU:
 1. Insert a node.
 2. Delete a node.
 3. Display the Singly Linked List.
 4. EXIT.
 Your Choice: 4

 Exiting.. 


Qx12. WAP to implement Stacks using Linked Lists.

posted Feb 9, 2011, 9:55 AM by Neil Mathew   [ updated Feb 9, 2011, 9:57 AM ]

SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
 
struct node
{
       int info;
       struct node * next;
}*top,*temp;
 
 
void PUSH(int item)
{
struct node *newnode;
newnode = (struct node *) malloc ( sizeof(struct node) );
 
if(newnode==NULL)
printf(" NO SPACE LEFT ");
else
{
    
newnode->info=item;
 
if( top == NULL )
{ 
newnode->next=NULL;
top=newnode;
}
else 
{ 
newnode->next=top;
top=newnode;
}
 
} //end of outer else
}// end of func
 
 
void POP()
{
if(top==NULL)
printf("\n Stack Empty. \n");
else
{
printf("\n Node with Info=%d is deleted. \n", top->info);
temp=top;
top=top->next;
free(temp);
}
}
 
void DISPLAY()
{
if(top==NULL)
printf("\n Stack is empty. ");
else
{
temp=top;
 
printf("\n");
while(temp!=NULL)
{
printf("\n |%d| ", temp->info);
temp=temp->next;
}
printf("\n");
}//end of else
}     
 
 
int main()
{
    int item,ch=1;
    top=NULL;
 
    
   while(ch!=4)
    {
    printf("\n MENU: ");
    printf("\n 1. Insert a node. ");
    printf("\n 2. Delete a node. ");
    printf("\n 3. Display Stack. ");
    printf("\n 4. EXIT \n Your Choice: ");
    scanf("%d",&ch);
    
    switch(ch)
    {
    case 1: //INSERT
        
    printf("\n ENTER ITEM: ");
    scanf("%d", &item);
    
    PUSH(item);
    break;
    
    case 2: //DELETE
    POP();
    break;
    
    case 3: //DISPLAY
    DISPLAY();
    break;
    
    case 4: 
    printf("\n Exiting.. ");
    break;
    
    default: printf(" INVALID CHOICE. ");
    
    }
    
    }; //end of while loop
    return 0;
    
}
 

OUTPUT:

MENU: 
 1. Insert a node. 
 2. Delete a node. 
 3. Display Stack. 
 4. EXIT 
 Your Choice: 1

 ENTER ITEM: 64

 MENU: 
 1. Insert a node. 
 2. Delete a node. 
 3. Display Stack. 
 4. EXIT 
 Your Choice: 1

 ENTER ITEM: 89

 MENU: 
 1. Insert a node. 
 2. Delete a node. 
 3. Display Stack. 
 4. EXIT 
 Your Choice: 1

 ENTER ITEM: 90

 MENU: 
 1. Insert a node. 
 2. Delete a node. 
 3. Display Stack. 
 4. EXIT 
 Your Choice: 3


 |90| 
 |89| 
 |64| 

 MENU: 
 1. Insert a node. 
 2. Delete a node. 
 3. Display Stack. 
 4. EXIT 
 Your Choice: 2

 Node with Info=90 is deleted. 

 MENU: 
 1. Insert a node. 
 2. Delete a node. 
 3. Display Stack. 
 4. EXIT 
 Your Choice: 2


 |89| 
 |64| 

 MENU: 
 1. Insert a node. 
 2. Delete a node. 
 3. Display Stack. 
 4. EXIT 
 Your Choice: 4

 Exiting.. 

Qx11. WAP to implement Queues using Linked Lists.

posted Feb 9, 2011, 9:17 AM by Neil Mathew   [ updated May 5, 2011, 6:25 PM ]


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include<stdio.h>
 
struct node
{
       int info;
       struct node * next;
}*start, *rear, *temp;
 
 
void INS(int item)
{
 
struct node *newnode;
newnode = (struct node *) malloc ( sizeof(struct node) );
 
if(newnode==NULL)
printf(" NO SPACE LEFT ");
else
{
    
newnode->info=item;
newnode->next=NULL;
 
if( start == NULL )
{ 
start=newnode;
rear=newnode;
}
else // INS at END
{ 
rear->next = newnode;
rear=rear->next;
}
 
} //end of outer if
}// end of func
 
void DEL()
{
 
if(start==NULL)
printf("\n No item to delete. Queue is empty. \n");
else
{
 
printf("\n Node with Info = %d is Deleted. \n", start->info );
temp=start;
start=start->next;
free(temp);
 
}
 
}
 
void DISPLAY()
{
if(start==NULL)
printf("\n Queue is empty. No Nodes. ");
else
{
temp=start;
 
printf("\n |");
while(temp!=NULL)
{
printf(" - |%d|", temp->info);
temp=temp->next;
}
printf(" - | \n");
}//end of else
}     
 
int main()
{
    int item,ch=1;
    start=rear=NULL;
 
    
   while(ch!=4)
    {
    printf("\n MENU: \n");
    printf(" 1. Insert a node. ");
    printf("\n 2. Delete a node. ");
    printf("\n 3. Display Queue. ");
    printf("\n 4. EXIT \n Your Choice: ");
    scanf("%d",&ch);
    
    switch(ch)
    {
    case 1: //INSERT
        
    printf("\n ENTER ITEM: ");
    scanf("%d", &item);
    
    INS(item);
    break;
    
    case 2: //DELETE
    DEL();
    break;
    
    case 3: //DISPLAY
    DISPLAY();
    break;
    
    case 4: 
    printf("\n Exiting.. ");
    break;
    
    default: printf(" INVALID CHOICE. ");
    
    }
    
    }; //end of while loop
    
    return 0;
    
}
 

OUTPUT:


 MENU: 
 1. Insert a node. 
 2. Delete a node. 
 3. Display Queue. 
 4. EXIT 
 Your Choice: 1

 ENTER ITEM: 64

 MENU: 
 1. Insert a node. 
 2. Delete a node. 
 3. Display Queue. 
 4. EXIT 
 Your Choice: 1

 ENTER ITEM: 89

 MENU: 
 1. Insert a node. 
 2. Delete a node. 
 3. Display Queue. 
 4. EXIT 
 Your Choice: 1

 ENTER ITEM: 90

 MENU: 
 1. Insert a node. 
 2. Delete a node. 
 3. Display Queue. 
 4. EXIT 
 Your Choice: 3

 | - |64| - |89| - |90| - | 

 MENU: 
 1. Insert a node. 
 2. Delete a node. 
 3. Display Queue. 
 4. EXIT 
 Your Choice: 2

 Node with Info = 64 is Deleted. 

 MENU: 
 1. Insert a node. 
 2. Delete a node. 
 3. Display Queue. 
 4. EXIT 
 Your Choice: 3

 | - |89| - |90| - | 

 MENU: 
 1. Insert a node. 
 2. Delete a node. 
 3. Display Queue. 
 4. EXIT 
 Your Choice: 4

 Exiting.. 


1-10 of 20