Home Contact Study Material Jaipur Photo UGC NET Computer Dictionary OS Data Structre Sorting algorithm Blog Favorite Links linux springsocial Research Research Group hadoop assembly programing C Prgming DSA Program Compiler Construction python Virtual Box ccna Ghazals or Songs Blank My Photos



Data Structure Program:-

 

Perform stack operation by reading record from file and write output to file.

 

$vi customerin.dat

001:Jeevan:1234
002:Tanya:1234
003:Sulaiman:1234
004:Nishank:1234
005:Kreeti:1234
006:Kaavya:1234
007:Athul:1234
008:Tejas:1234

$vi customerout.dat

006:Kaavya:1234
005:Kreeti:1234
004:Nishank:1234
003:Sulaiman:1234
002:Tanya:1234
001:Jeevan:1234

----------------------------------Program in CPP------------------------------------

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<iomanip>

using namespace std;

int top=0;

class stack
{
    public:
    char cid[10];
    char cname[20];
    char ctel[15];
}s[20];

void create();
void push();
void pop();
void display();
void output(int);
void store();

int main()
{
    int ch;
    create();
    do
    {
        cout<<"1. Push"<<endl;
        cout<<"2. Pop"<<endl;
        cout<<"3. Display"<<endl;
        cout<<"4. Exit"<<endl;
        cout<<"Enter your choice : ";
        cin>>ch;
        switch(ch)
        {
            case 1: push();break;
            case 2: pop();break;
            case 3: display();break;
            case 4: cout<<"Thank You"<<endl;store();break;
            default: cout<<"Wrong Choice"<<endl;break;
        }
    }while(ch!=4);
}

void create()
{
    ifstream x;
    x.open("customerin.dat");
    char str[50];
    while(!x.eof())
    {
        x.getline(str,50);
        int x=0,z=1;
        for(int i=0;str[i]!='\0';++i)
        {
            if(str[i]!=':')
            {
                switch(z)
                {
                    case 1:s[top].cid[x]=str[i];++x;break;
                    case 2:s[top].cname[x]=str[i];++x;break;
                    case 3:s[top].ctel[x]=str[i];++x;break;
                }
            }
            else
            {
                ++z;
                x=0;
            }
        }
        ++top;
        if((top+1)==20)
        {
            cout<<"Max record limit reached!!"<<endl;
            break;
        }
    }
    --top;
    x.close();
    if((top+1)<7)
    {
        cout<<"Record contains less than 7 records!!"<<endl;
        return;
    }
    cout<<"Records of file pushed to stack!"<<endl;
}

void push()
{
    if((top-1)==20)
    {
        cout<<"Max record limit reached!!"<<endl;
        return;
    }
    cout<<"Customer ID: ";
    cin>>s[top].cid;
    cout<<"Customer Name: ";
    cin>>s[top].cname;
    cout<<"Customer Tel: ";
    cin>>s[top].ctel;
    ++top;
    cout<<"Entry made successfully!"<<endl;
}

void pop()
{
    if(top<0)
    {
        cout<<"Stack Empty!!"<<endl;
        return;
    }

    cout<<"Following record popped :"<<endl;
    output(top-1);
    --top;


}

void display()
{
    if(top<0)
    {
        cout<<"Stack Empty!!"<<endl;
        return;
    }
        
    cout<<setw(15)<<"Customer ID"<<setw(15)<<"Customer Name"<<setw(15)<<"Customer Tel"<<endl<<endl;
    for(int i=top-1;i>=0;--i)
    {
        output(i);
    }
}

void output(int i)
{
    cout<<setw(15)<<s[i].cid<<setw(15)<<s[i].cname<<setw(15)<<s[i].ctel<<endl;
}

void store()
{
    ofstream x;
    x.open("customerout.dat");
    for(int i=top-1;i>=0;--i)
    {
        x<<s[i].cid<<":"<<s[i].cname<<":"<<s[i].ctel<<endl;
    }
    x.close();
}

 

===========================================READING AND WRITING FROM FILE USING STACK in JAVA============================

 

import java.io.*;

class Customer{
    String cusId;
    String cusName;
    String teleno;
    
    public Customer(String a, String b, String c) {
        cusId = a;
        cusName = c;
        teleno = b;
    }

    public String toString() {
        return cusId + " " + cusName + " " + teleno;    
    }
}

class Stack {

    Customer[] list;
    int size = -1;

    public Stack(int size){
        list = new Customer[size];
    }

    public void push(Customer p) {
        if(size == list.length){
            System.out.println("Overflow occured");
        }

        size++;
        list[size] = p;
    }

    public Customer pop() {
        if(size < 0) {
            // System.out.println("Underflow occured");
            size = -1;
            return null;
        }    

        Customer temp = list[size];
        list[size] = null;
        size--;

        return temp;
    }

    public Customer peek() {
        if(size >= 0)
            return list[size];
        return null;
    }

    public boolean isEmpty() {
        if(size == -1)
            return true;
        return false;
    }

    public void display() {
        for(int i = 0; i <= size; i++) {
            System.out.println(list[i]);    
        }    
    }
}


public class StackTest{
    public static void main(String[] args) {
        Stack p = new Stack(10);

        try{
            String line;
            BufferedReader ip = new BufferedReader(new FileReader("customerin.dat"));
            while((line = ip.readLine()) != null) {
                String[] s = line.split(" ");
                String id =  s[0];
                String name = s[1];
                String no = s[2];
                
            //  System.out.println(id + " " + pin + " " + name);
                Customer temp = new Customer(id, name, no);
                p.push(temp);
            }

            System.out.println("Objects pushed to stack: ");
            p.display();


            System.out.println("Popping objects from stack to customerout.dat...");
            BufferedWriter op = new BufferedWriter(new FileWriter("customerout.dat"));
            Customer temp;
            while((temp = p.pop()) != null) {
                op.append(temp.toString());
                op.append("\n");
                op.flush();
            }


            ip.close();
            op.close();

        }  catch(Exception e) {
            e.printStackTrace();
        }
    }   
}

 

 

---------------------------------------------------------------------------------------------------------------------------------------

I. IMPLEMENTATION of  MERGESORT (use Recursive Version)  using key

field

Write a C/C++/JAVA  program to implement MERGESORT for the

given Data Set (IDNO, NAME, CGPA):

1) You have to read input records of data set from an input file “min.dat” stored

locally in your directory and copy them to an array / array of structures using any

iterative construct of your choice. (You can use vi editor to create input data file.

Make sure that the input data file contains at least 10 records).

 

2.) Show the output on Screen (standard output)  after each Step (Pass). i.e. You

have to display the output for each step when the sorting is going on.

3) Observe the execution time of your program [use   time  or timex  command in

LINUX system; you can check the syntax for time or timex using man command]

(* You should not use Built in Function MSORT in standard template library. write

your own code)

 

 

-------------------------vi mergin.dat----------------------------------------------------

4:XYZ:8.8
2:JAC:9.1
1:ABC:9.0
5:Ved:8.0
7:YAC:7.9
3:Ala:8.0
6:Aka::8.0

--------------------------MergeSort.java------------------------------------------------------------

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;

class Data
{
    int id;
    String name;
    double cgpa;
    public Data(int a, String b, double c)
    {
        id=a;
        name=b;
        cgpa=c;
    }
    public String toString()
    {
        return ("ID: " + id + " Name: " + name + " CGPA: " + cgpa);
    }
}

public class mergesort
{
    static int count=0;

    public static void main(String[] args)
    {
        Scanner inp= new Scanner(System.in);
        ArrayList<Data> list= new ArrayList<Data>();
        int ch;
        do
        {
            System.out.println("MENU");
            System.out.println("1. Insert");
            System.out.println("2. MergeSort");
            System.out.println("3. Display");
            System.out.println("4. Exit");
            System.out.print("Enter your choice: ");
            ch=inp.nextInt();
            switch(ch)
            {
                case 1: insert(list); break;
                case 2: list=merge(list); break;
                case 3: display(list);break;
                case 4: System.out.println("Thank You!"); break;
                default: System.out.println("Wrong Choice!"); break;
            }
        }while(ch!=4);
    }

    public static ArrayList<Data> insert(ArrayList<Data> list)
    {
        int size=0;
        int a=0;
        try
        {
            BufferedReader reader= new BufferedReader(new FileReader("mergein.dat"));
            String line;
            while((line=reader.readLine())!= null)
            {
                ++size;
            }
        }
        catch(Exception e){}

        try
        {
            BufferedReader reader= new BufferedReader(new FileReader("mergein.dat"));
            String line;
            while((line=reader.readLine())!=null)
            {
                if(a==count)
                    break;
                ++a;
            }
            ++count;
            if(a==size)
            {
                System.out.println("All contents of the file are read!");
                return list;
            }
            String[] token= line.substring(0, line.length()).split(":");
            Data d= new Data(Integer.parseInt(token[0]), token[1], Double.parseDouble(token[2]));
            list.add(d);
        }
        catch(Exception e){}
        return list;
    }

    public static ArrayList<Data> merge(ArrayList<Data> list)
    {
        Data[] s= new Data[list.size()];
        for(int i=0;i<list.size();++i)
        {
            s[i]=list.get(i);
        }
        Data[] ans=merge1(s);
        list.clear();
        for(int i=0;i<ans.length;++i)
            list.add(ans[i]);
        return list;
    }

    public static void display(ArrayList<Data> list)
    {
        for(int i=0;i<list.size();++i)
            System.out.println(list.get(i));
    }

    public static Data[] merge1(Data[] s)
    {
        if(s.length>1)
        {
            int n=s.length/2-1;
            Data[] s1=new Data[n+1];
            Data[] s2=new Data[s.length-n-1];

            for(int i=0;i<=n;++i)
            {
                s1[i]=s[i];
                System.out.print(s1[i].id + " ");
            }

            for(int i=0;i<s.length-n-1;++i)
            {
                s2[i]=s[n+i+1];
                System.out.print(s2[i].id + " ");
            }
            System.out.println();

            s1=merge1(s1);
            for(int i=0;i<s1.length;++i)
                System.out.print(s1[i].id + " ");
            System.out.println();

            s2=merge1(s2);
            for(int i=0;i<s1.length;++i)
                System.out.print(s2[i].id + " ");
            System.out.println();

            Data[] ans=merge2(s1,s2);
            for(int i=0;i<ans.length;++i)
                System.out.print(ans[i].id + " ");
            System.out.println();
            return ans;
        }
        return s;
    }

    public static Data[] merge2(Data[] s1, Data[] s2)
    {
        Data[] s = new Data[s1.length + s2.length];
        int a = s1.length - 1, b = s2.length - 1;
        int i = 0, j = 0, n = 0;
        while(a!=-1 && b!=-1)
        {
            if (s1[i].id < s2[j].id)
            {
                s[n] = s1[i];
                ++i;
                --a;
            }
            else
            {
                s[n] = s2[j];
                ++j;
                --b;
            }
            ++n;
        }
        while(a!=-1)
        {
            s[n] = s1[i];
            ++i;
            --a;
            ++n;
        }
        while(b!=-1)
        {
            s[n] = s2[j];
            ++j;
            --b;
            ++n;
        }
        return s;
    }
}