0% found this document useful (0 votes)
40 views28 pages

B003-Data Structures.

DATA STRUCTURES BASIC

Uploaded by

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

B003-Data Structures.

DATA STRUCTURES BASIC

Uploaded by

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

1st Question

#include <stdio.h>

int main()

int array[100], position, c, n, value;

printf("Enter number of elements in array\n");

scanf("%d", &n);

printf("Enter %d elements\n", n);

for (c = 0; c < n; c++)

scanf("%d", &array[c]);

printf("Enter the location where you wish to insert an element\n");

scanf("%d", &position);

printf("Enter the value to insert\n");

scanf("%d", &value);

for (c = n - 1; c >= position - 1; c--)

array[c+1] = array[c];

array[position-1] = value;
printf("Resultant array is\n");

for (c = 0; c <= n; c++)

printf("%d\n", array[c]);

return 0;

Output:
2nd Question

1. #include<stdio.h>  
2. #include<stdlib.h>  
3. struct node  
4. {  
5.     struct node *prev;  
6.     struct node *next;  
7.     int data;  
8. };  
9. struct node *head;  
10. void insertion_beginning();  
11. void insertion_last();  
12. void insertion_specified();  
13. void deletion_beginning();  
14. void deletion_last();  
15. void deletion_specified();  
16. void display();  
17. void search();  
18. void main ()  
19. {  
20. int choice =0;  
21.     while(choice != 9)  
22.     {  
23.         printf("\n*********Main Menu*********\n");  
24.         printf("\nChoose one option from the following list ...\n");  
25.         printf("\n===============================================\n");  
26.         printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete from B
eginning\n  
27.         5.Delete from last\n6.Delete the node after the given data\n7.Search\n8.Show\n9.Exit\n");  
28.         printf("\nEnter your choice?\n");  
29.         scanf("\n%d",&choice);  
30.         switch(choice)  
31.         {  
32.             case 1:  
33.             insertion_beginning();  
34.             break;  
35.             case 2:  
36.                     insertion_last();  
37.             break;  
38.             case 3:  
39.             insertion_specified();  
40.             break;  
41.             case 4:  
42.             deletion_beginning();  
43.             break;  
44.             case 5:  
45.             deletion_last();  
46.             break;  
47.             case 6:  
48.             deletion_specified();  
49.             break;  
50.             case 7:  
51.             search();  
52.             break;  
53.             case 8:  
54.             display();  
55.             break;  
56.             case 9:  
57.             exit(0);  
58.             break;  
59.             default:  
60.             printf("Please enter valid choice..");  
61.         }  
62.     }  
63. }  
64. void insertion_beginning()  
65. {  
66.    struct node *ptr;   
67.    int item;  
68.    ptr = (struct node *)malloc(sizeof(struct node));  
69.    if(ptr == NULL)  
70.    {  
71.        printf("\nOVERFLOW");  
72.    }  
73.    else  
74.    {  
75.     printf("\nEnter Item value");  
76.     scanf("%d",&item);  
77.       
78.    if(head==NULL)  
79.    {  
80.        ptr->next = NULL;  
81.        ptr->prev=NULL;  
82.        ptr->data=item;  
83.        head=ptr;  
84.    }  
85.    else   
86.    {  
87.        ptr->data=item;  
88.        ptr->prev=NULL;  
89.        ptr->next = head;  
90.        head->prev=ptr;  
91.        head=ptr;  
92.    }  
93.    printf("\nNode inserted\n");  
94. }  
95.      
96. }  
97. void insertion_last()  
98. {  
99.    struct node *ptr,*temp;  
100.    int item;  
101.    ptr = (struct node *) malloc(sizeof(struct node));  
102.    if(ptr == NULL)  
103.    {  
104.        printf("\nOVERFLOW");  
105.    }  
106.    else  
107.    {  
108.        printf("\nEnter value");  
109.        scanf("%d",&item);  
110.         ptr->data=item;  
111.        if(head == NULL)  
112.        {  
113.            ptr->next = NULL;  
114.            ptr->prev = NULL;  
115.            head = ptr;  
116.        }  
117.        else  
118.        {  
119.           temp = head;  
120.           while(temp->next!=NULL)  
121.           {  
122.               temp = temp->next;  
123.           }  
124.           temp->next = ptr;  
125.           ptr ->prev=temp;  
126.           ptr->next = NULL;  
127.           }  
128.              
129.        }  
130.      printf("\nnode inserted\n");  
131.     }  
132. void insertion_specified()  
133. {  
134.    struct node *ptr,*temp;  
135.    int item,loc,i;  
136.    ptr = (struct node *)malloc(sizeof(struct node));  
137.    if(ptr == NULL)  
138.    {  
139.        printf("\n OVERFLOW");  
140.    }  
141.    else  
142.    {  
143.        temp=head;  
144.        printf("Enter the location");  
145.        scanf("%d",&loc);  
146.        for(i=0;i<loc;i++)  
147.        {  
148.            temp = temp->next;  
149.            if(temp == NULL)  
150.            {  
151.                printf("\n There are less than %d elements", loc);  
152.                return;  
153.            }  
154.        }  
155.        printf("Enter value");  
156.        scanf("%d",&item);  
157.        ptr->data = item;  
158.        ptr->next = temp->next;  
159.        ptr -> prev = temp;  
160.        temp->next = ptr;  
161.        temp->next->prev=ptr;  
162.        printf("\nnode inserted\n");  
163.    }  
164. }  
165. void deletion_beginning()  
166. {  
167.     struct node *ptr;  
168.     if(head == NULL)  
169.     {  
170.         printf("\n UNDERFLOW");  
171.     }  
172.     else if(head->next == NULL)  
173.     {  
174.         head = NULL;   
175.         free(head);  
176.         printf("\nnode deleted\n");  
177.     }  
178.     else  
179.     {  
180.         ptr = head;  
181.         head = head -> next;  
182.         head -> prev = NULL;  
183.         free(ptr);  
184.         printf("\nnode deleted\n");  
185.     }  
186.   
187. }  
188. void deletion_last()  
189. {  
190.     struct node *ptr;  
191.     if(head == NULL)  
192.     {  
193.         printf("\n UNDERFLOW");  
194.     }  
195.     else if(head->next == NULL)  
196.     {  
197.         head = NULL;   
198.         free(head);   
199.         printf("\nnode deleted\n");  
200.     }  
201.     else   
202.     {  
203.         ptr = head;   
204.         if(ptr->next != NULL)  
205.         {  
206.             ptr = ptr -> next;   
207.         }  
208.         ptr -> prev -> next = NULL;   
209.         free(ptr);  
210.         printf("\nnode deleted\n");  
211.     }  
212. }  
213. void deletion_specified()  
214. {  
215.     struct node *ptr, *temp;  
216.     int val;  
217.     printf("\n Enter the data after which the node is to be deleted : ");  
218.     scanf("%d", &val);  
219.     ptr = head;  
220.     while(ptr -> data != val)  
221.     ptr = ptr -> next;  
222.     if(ptr -> next == NULL)  
223.     {  
224.         printf("\nCan't delete\n");  
225.     }  
226.     else if(ptr -> next -> next == NULL)  
227.     {  
228.         ptr ->next = NULL;  
229.     }  
230.     else  
231.     {   
232.         temp = ptr -> next;  
233.         ptr -> next = temp -> next;  
234.         temp -> next -> prev = ptr;  
235.         free(temp);  
236.         printf("\nnode deleted\n");  
237.     }     
238. }  
239. void display()  
240. {  
241.     struct node *ptr;  
242.     printf("\n printing values...\n");  
243.     ptr = head;  
244.     while(ptr != NULL)  
245.     {  
246.         printf("%d\n",ptr->data);  
247.         ptr=ptr->next;  
248.     }  
249. }   
250. void search()  
251. {  
252.     struct node *ptr;  
253.     int item,i=0,flag;  
254.     ptr = head;   
255.     if(ptr == NULL)  
256.     {  
257.         printf("\nEmpty List\n");  
258.     }  
259.     else  
260.     {   
261.         printf("\nEnter item which you want to search?\n");   
262.         scanf("%d",&item);  
263.         while (ptr!=NULL)  
264.         {  
265.             if(ptr->data == item)  
266.             {  
267.                 printf("\nitem found at location %d ",i+1);  
268.                 flag=0;  
269.                 break;  
270.             }   
271.             else  
272.             {  
273.                 flag=1;  
274.             }  
275.             i++;  
276.             ptr = ptr -> next;  
277.         }  
278.         if(flag==1)  
279.         {  
280.             printf("\nItem not found\n");  
281.         }  
282.     }     
283.           
284. } 

Output:
3Rd question:

#include<stdio.h>

int stack[100],choice,n,top,x,i;

void push(void);

void pop(void);

void display(void);

int main()

//clrscr();

top=-1;

printf("\n Enter the size of STACK[MAX=100]:");

scanf("%d",&n);

printf("\n\t STACK OPERATIONS USING ARRAY");

printf("\n\t--------------------------------");

printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");

do

printf("\n Enter the Choice:");

scanf("%d",&choice);

switch(choice)

case 1:

push();
break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

printf("\n\t EXIT POINT ");

break;

default:

printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");

while(choice!=4);
return 0;

void push()

if(top>=n-1)

printf("\n\tSTACK is over flow");

else

printf(" Enter a value to be pushed:");

scanf("%d",&x);

top++;

stack[top]=x;

void pop()

if(top<=-1)

printf("\n\t Stack is under flow");

else

{
printf("\n\t The popped elements is %d",stack[top]);

top--;

void display()

if(top>=0)

printf("\n The elements in STACK \n");

for(i=top; i>=0; i--)

printf("\n%d",stack[i]);

printf("\n Press Next Choice");

else

printf("\n The STACK is empty");

Output:
4th Question:

// A Linked List Node


class Node
{
    int data;       // integer data
    Node next;      // pointer to the next node
 
    public Node(int data)
    {
        // set data in the allocated node and return it
        this.data = data;
        this.next = null;
    }
}
 
class Queue
{
    private static Node rear = null, front = null;
    private static int count = 0;
 
    // Utility function to dequeue the front element
    public static int dequeue()     // delete at the beginning
    {
        if (front == null)
        {
            System.out.println("\nQueue Underflow");
            System.exit(-1);
        }
 
        Node temp = front;
        System.out.printf("Removing %d\n", temp.data);
 
        // advance front to the next node
        front = front.next;
 
        // if the list becomes empty
        if (front == null) {
            rear = null;
        }
 
        // decrease the node's count by 1
        count -= 1;
 
        // return the removed item
        return temp.data;
    }
 
    // Utility function to add an item to the queue
    public static void enqueue(int item)     // insertion at the end
    {
        // allocate a new node in a heap
        Node node = new Node(item);
        System.out.printf("Inserting %d\n", item);
 
        // special case: queue was empty
        if (front == null)
        {
            // initialize both front and rear
            front = node;
            rear = node;
        }
        else {
            // update rear
            rear.next = node;
            rear = node;
        }
 
        // increase the node's count by 1
        count += 1;
    }
 
    // Utility function to return the top element in a queue
    public static int peek()
    {
        // check for an empty queue
        if (front == null) {
            System.exit(-1);
        }
 
        return front.data;
    }
 
    // Utility function to check if the queue is empty or not
    public static boolean isEmpty() {
        return rear == null && front == null;
    }
 
    // Function to return the size of the queue
    private static int size() {
        return count;
    }
}
 
class Main
{
    public static void main(String[] args)
    {
        Queue q = new Queue();
        q.enqueue(1);
        q.enqueue(2);
        q.enqueue(3);
        q.enqueue(4);
 
        System.out.printf("The front element is %d\n", q.peek());
 
        q.dequeue();
        q.dequeue();
        q.dequeue();
        q.dequeue();
 
        if (q.isEmpty()) {
            System.out.println("The queue is empty");
        }
        else {
            System.out.println("The queue is not empty");
        }
    }
}

Output:

5th Question

// Java program for implementation of Bubble Sort


class BubbleSort
{
    void bubbleSort(int arr[])
    {
        int n = arr.length;
        for (int i = 0; i < n-1; i++)
            for (int j = 0; j < n-i-1; j++)
                if (arr[j] > arr[j+1])
                {
                    // swap arr[j+1] and arr[j]
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
    }
 
    /* Prints the array */
    void printArray(int arr[])
    {
        int n = arr.length;
        for (int i=0; i<n; ++i)
            System.out.print(arr[i] + " ");
        System.out.println();
    }
 
    // Driver method to test above
    public static void main(String args[])
    {
        BubbleSort ob = new BubbleSort();
        int arr[] = {64, 34, 25, 12, 22, 11, 90};
        ob.bubbleSort(arr);
        System.out.println("Sorted array");
        ob.printArray(arr);
    }
}

Output:

6th Question
class BinarySearch {
    // Returns index of x if it is present in arr[l..
    // r], else return -1
    int binarySearch(int arr[], int l, int r, int x)
    {
        if (r >= l) {
            int mid = l + (r - l) / 2;
 
            // If the element is present at the
            // middle itself
            if (arr[mid] == x)
                return mid;
 
            // If element is smaller than mid, then
            // it can only be present in left subarray
            if (arr[mid] > x)
                return binarySearch(arr, l, mid - 1, x);
 
            // Else the element can only be present
            // in right subarray
            return binarySearch(arr, mid + 1, r, x);
        }
 
        // We reach here when element is not present
        // in array
        return -1;
    }
 
    // Driver method to test above
    public static void main(String args[])
    {
        BinarySearch ob = new BinarySearch();
        int arr[] = { 2, 3, 4, 10, 40 };
        int n = arr.length;
        int x = 10;
        int result = ob.binarySearch(arr, 0, n - 1, x);
        if (result == -1)
            System.out.println("Element not present");
        else
            System.out.println("Element found at index "
                               + result);
    }
}

Output:
7th question

// Binary Search Tree operations in Java

class BinarySearchTree {

class Node {

int key;

Node left, right;

public Node(int item) {

key = item;

left = right = null;

Node root;

BinarySearchTree() {

root = null;

void insert(int key) {


root = insertKey(root, key);

// Insert key in the tree

Node insertKey(Node root, int key) {

// Return a new node if the tree is empty

if (root == null) {

root = new Node(key);

return root;

// Traverse to the right place and insert the node

if (key < root.key)

root.left = insertKey(root.left, key);

else if (key > root.key)

root.right = insertKey(root.right, key);

return root;

void inorder() {

inorderRec(root);

// Inorder Traversal
void inorderRec(Node root) {

if (root != null) {

inorderRec(root.left);

System.out.print(root.key + " -> ");

inorderRec(root.right);

void deleteKey(int key) {

root = deleteRec(root, key);

Node deleteRec(Node root, int key) {

// Return if the tree is empty

if (root == null)

return root;

// Find the node to be deleted

if (key < root.key)

root.left = deleteRec(root.left, key);

else if (key > root.key)

root.right = deleteRec(root.right, key);

else {

// If the node is with only one child or no child

if (root.left == null)
return root.right;

else if (root.right == null)

return root.left;

// If the node has two children

// Place the inorder successor in position of the node to be deleted

root.key = minValue(root.right);

// Delete the inorder successor

root.right = deleteRec(root.right, root.key);

return root;

// Find the inorder successor

int minValue(Node root) {

int minv = root.key;

while (root.left != null) {

minv = root.left.key;

root = root.left;

return minv;

}
// Driver Program to test above functions

public static void main(String[] args) {

BinarySearchTree tree = new BinarySearchTree();

tree.insert(8);

tree.insert(3);

tree.insert(1);

tree.insert(6);

tree.insert(7);

tree.insert(10);

tree.insert(14);

tree.insert(4);

System.out.print("Inorder traversal: ");

tree.inorder();

System.out.println("\n\nAfter deleting 10");

tree.deleteKey(10);

System.out.print("Inorder traversal: ");

tree.inorder();

Output:

You might also like