Blog

Define a function length (not len) that expects a singly linked structure as an argument.

1. Define a function length (not len) that expects a singly linked structure as an argument. The function returns the number of items in the structure.

2. Define a function named insert that inserts an item into a singly linked structure at a given position. The function expects three arguments: the item, the position, and the linked structure (the latter may be empty).

The function returns the modified linked structure. If the position is greater than or equal to the structure’s length, the function inserts the item at its end. An example call of the function, where head is a variable that either is an empty link or refers to the first node of a structure, is head = insert(1, data, head).

Define a function named remove that removes the item at a given position from a singly linked structure.

Define a function named remove that removes the item at a given position from a singly linked structure. This function expects a position as a first argument, with the precondition 0 <= position=””>< length=”” of=”” structure.=”” its=”” second=”” argument=”” is=”” the=”” linked=”” structure,=”” which,=”” of=”” course,=”” cannot=”” be=”” empty.=”” the=”” function=”” returns=”” a=”” tuple=”” containing=”” the=”” modified=”” linked=”” structure=”” and=”” the=”” item=”” that=”” was=”” removed.=”” an=”” example=”” call=”” is=”” (head,=”” item)=”remove(1,”>

Where does this strategy break down?

Someone suggests that you might not need a stack to match parentheses in expressions after all. Instead, you can set a counter to 0, increment it when a left parenthesis is encountered, and decrement it whenever a right parenthesis is seen. If the counter ever goes below zero or remains positive at the end of the process, there was an error; if the counter is zero at the end and never goes negative, the parentheses all match correctly. Where does this strategy break down? (Hint: There might be braces and brackets to match as well.)

 

Write a program that converts infix expressions to postfix expressions.

Write a program that converts infix expressions to postfix expressions. This program should use the Token and Scanner classes developed in the case study. The program should consist of a main function that performs the inputs and outputs, and a class named IFToPFConverter. The main function receives an input string and creates a scanner with it. The scanner is then passed as an argument to the constructor of the converter object. The converter object’s convert method is then run to convert the infix expression using the algorithm described in this chapter. This method returns a list of tokens that represent the postfix string. The main function then displays this string. You should also define a new method in the Token class, getPrecedence(), which returns an integer that represents….

Modify the program of Project 6 so that it checks the infix string for syntax errors as it converts to postfix.

Modify the program of Project 6 so that it checks the infix string for syntax errors as it converts to postfix. The error-detection and recovery strategy should be similar to the one used in the case study. Add a method named conversionStatus to the IFToPFConverter class. When the converter detects a syntax error, it should raise an exception, which the main function catches in a try-except statement. The main function can then call conversionStatus to obtain the information to print when an error occurs. This information should include the portion of the expression scanned until the error is detected. The error messages should also be as specific as possible.