Question

In: Computer Science

Program Language C++ How do I take lines of string from an input file, and implement...

Program Language C++

How do I take lines of string from an input file, and implement them into a stack using a double linked list? Example input, command.txt, and output file.

Ex:

Input.txt

postfix: BAC-*

prefix:+A*B/C-EF

postfix:FE-C/B*A+

postfix:AB-C-D/

postfix:AB-CF*-D / E+

Command.txt

printList

printListBackwards

Output.txt

List:

postfix:BAC-*

prefix:+A*B/C-EF

postfix:FE-C/B*A+

postfix:AB-C-D/

postfix:AB-CF*-D/E+

Reversed List:

postfix:AB-CF*-D/E+

postfix:AB-C-D/

postfix:FE-C/B*A+

prefix:+A*B/C-EF

postfix:BAC-*

Solutions

Expert Solution

//package frame;

import java.io.*;
import java.util.*;

public class Dlink{
public static Node h=null; // h of list
  
//Doubly Linked list N
public static class Node {
String d;
Node p;
Node nxt;
Node(String d1) { d = d1; } // Constructor to create a new N
}
public static void push(String new_d) // Adding a N at the front of the list
{
Node nw = new Node(new_d);//allocate N
nw.nxt = h; //Make nxt of new N as h and pious as NULL
nw.p = null;
if (h != null) //change p of h N to new N
h.p = nw;
h = nw; //move the h to point to the new N
}
public static void append(String new_d) // Add a N at the end of the list
{
Node nw = new Node(new_d); //allocate N
Node l = h;
nw.nxt = null; //This new N is going to be the l N, so make nxt of it as NULL
if (h == null) { //If the Linked List is empty, then make the new N h
nw.p = null;
h = nw;
return;
}
while (l.nxt != null){ //Else traverse till the l N
l = l.nxt; }
l.nxt = nw; //Change the nxt of l N
nw.p = l; //Make l N as pious of new N
}
public void printlist(Node node) // This function prints contents of linked list starting from the given N
{
Node l = null;
while (node != null) {
System.out.println(node.d + " ");
l = node;
node = node.nxt;
}
System.out.println();
System.out.println("Reverse direction");
while (l != null) {
System.out.println(l.d + " ");
l = l.p;
}
}
public static void main(String[] args) throws FileNotFoundException
{
Dlink dll = new Dlink(); //Start with the empty list
ArrayList<String> lt= new ArrayList<String>(); //Start with the empty list
try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) //opening the file
{
String CLine;//variable
while ((CLine = br.readLine()) != null) {//read line by line
lt.add(CLine);//add the line to list
}
} catch (IOException e) {
e.printStackTrace();//catch the error if file not found
}
push(lt.get(0));
int ct = 1;   
while (lt.size() > ct) {//traverse through arraylist
append(lt.get(ct));//printing result
   //System.out.println(lt.get(ct));
ct++;
}
System.out.println("Created DLL is: ");
dll.printlist(dll.h); //printing the List
}
}

If you found this answer helpful please give a thumbs up.


Related Solutions

Write an assembly language program to input a string from the user. Your program should do...
Write an assembly language program to input a string from the user. Your program should do these two things: ***** I AM POSTING THIS QUESTION FOR THE THIRD TIME NOW,I WANT IT DONE USING INCLUDE IRVINE32.INC***** 1. count and display the number of words in the user input string. 2. Flip the case of each character from upper to lower or lower to upper. For example if the user types in:   "Hello thEre. How aRe yOu?" Your output should be:...
Use C language Write a program that reads in a series of lines of input character...
Use C language Write a program that reads in a series of lines of input character by character (using getchar()). The first line of the input contains an integer which specifies the number of remaining lines of input, each of which contains a floating point number. The integer value on the first line can be read with scanf(), but all of the following lines can only be read with getchar(). Each line after the first contains a single floating point...
Write a C program, called reverse, using standard I/O functions, to take a file as input...
Write a C program, called reverse, using standard I/O functions, to take a file as input then copies it to another file in reverse order. That is, the last byte becomes the first, the byte just before the last one becomes the second, etc. The program call should look like: reverse fileIn fileOut
I need to update this java program to take input about each employee from a file...
I need to update this java program to take input about each employee from a file and write their information and salary/pay information to a file. use input file payroll.txt Kevin Yang 60 20 Trey Adams 30 15 Rick Johnson 45 10 Cynthia Wheeler 55 11.50 Sarah Davis 24 10 Muhammad Rabish 66 12 Dale Allen 11 18 Andrew Jimenez 80 15 import java.util.Scanner; public class SalaryCalcLoop { double Rpay = 0, Opay = 0; void calPay(double hours, double rate)...
How do I create this program? Using C++ language! Write a program that reads data from...
How do I create this program? Using C++ language! Write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global varibles are the mean, standard deviation, and the number of data entered. All other varibles must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function....ALL...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input a description of several process schedules (i.e., lists of send, receive or print operations). The output of your program will be a linearization of these events in the order actually performed, annotated with Lamport clock values. The input of the program will be a collection of processes, each with a list of operations to perform. The processes are named p1...pn for some n (you...
How do I write a C++ program to call a frequency table from a csv file,...
How do I write a C++ program to call a frequency table from a csv file, using vector? Data given is in a csv file. Below is part of the sample data. Student ID English Math Science 100000100 80 90 90 100000110 70 60 70 100000120 80 100 90 100000130 60 60 60 100000140 90 80 80
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
The assignment is to build a program in Python that can take a string as input...
The assignment is to build a program in Python that can take a string as input and produce a “frequency list” of all of the wordsin the string (see the definition of a word below.)  For the purposes of this assignment, the input strings can be assumed not to contain escape characters (\n, \t, …) and to be readable with a single input() statement. When your program ends, it prints the list of words.  In the output, each line contains of a...
2. Write a Java program that reads a series of input lines from given file “name.txt”,...
2. Write a Java program that reads a series of input lines from given file “name.txt”, and sorts them into alphabetical order, ignoring the case of words. The program should use the merge sort algorithm so that it efficiently sorts a large file. Contents of names.text Slater, KendallLavery, RyanChandler, Arabella "Babe"Chandler, StuartKane, EricaChandler, Adam JrSlater, ZachMontgomery, JacksonChandler, KrystalMartin, JamesMontgomery, BiancaCortlandt, PalmerDevane, AidanMadden, JoshHayward, DavidLavery,k JonathanSmythe, GreenleeCortlandt, OpalMcDermott, AnnieHenry, DiGrey, MariaEnglish, BrookeKeefer, JuliaMartin, JosephMontgomery, LilyDillon, AmandaColby, LizaStone, Mary FrancesChandler, ColbyFrye, DerekMontgomery,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT