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 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
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
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,...
Step 1: Your program will now take its input from this file instead of from the...
Step 1: Your program will now take its input from this file instead of from the user. Here's what you want to do: Remove the call to getUserInput(). You can also remove its definition and prototype if you like. For reasons that will become clear later, don't close the file until just before the return statement at the end of main. Run your code to make sure that the file is opened and read correctly. Hint: you may test the...
Implement two functions in C language: stringLength() - Takes a pointer to a string, and a...
Implement two functions in C language: stringLength() - Takes a pointer to a string, and a pointer to an int variable. It computes the length of the string and updates the int variable with the length. stringCopy() - Copies one string onto another using pointers #include<stdio.h> #define MAX_STR_LEN 2048 void stringLength(char *str, int *len) { // This function computes the length of the string // at the address in the pointer *str. Once the length // has been determined, it...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
I need C++ program that Read an input file of text.txt one word at a time....
I need C++ program that Read an input file of text.txt one word at a time. The file should consist of about 500 words. The program should remove all punctuations,keep only words. Store the words in a built-in STL container, such as vector or map.Can someone help with any additional comments that I can understand the logic?thank you
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT