[Data structure] stack Application Line Editor and data structure Editor

Source: Internet
Author: User
Tags line editor

[Data structure] stack Application Line Editor and data structure Editor

Enter a string of characters on the terminal. If the entered character is incorrect, enter #, indicating that the previous character is invalid. If you want to clear this line, enter @


For example:

'Input: hellow #

Output: hello


Input: hellow @

Output:



# Ifndef _ EDIT_H _ # define_EDIT_H _ # include <iostream> # include <stdlib. h> # include <malloc. h> using namespace std; # define STACKSIZE 100 typedef char ElemType; typedef struct {ElemType stack [STACKSIZE]; int top;} SeqStack; void InitStack (SeqStack * S ); // initialize the stack int StackEmpty (SeqStack S); // judge whether the stack is empty int GetTop (SeqStack S, ElemType * e ); // take the top element of the stack int PushStack (SeqStack * S, ElemType e); // The inbound stack int PopStack (SeqStack * S, ElemType * e ); // output stack int StackLength (SeqStack S); // calculate the stack length void ClearStack (SeqStack * S); // clear the stack void LineEdit (); // The row editing function # endif

# Include "Edit. h "void InitStack (SeqStack * S) // initialize stack S to empty stack {S-> top = 0;} int StackEmpty (SeqStack S) // judge whether the stack is empty. if the stack is empty, 1 is returned; otherwise, 0 {if (0 = S. top) {return 1 ;}else {return 0 ;}int GetTop (SeqStack S, ElemType * e) // gets the top element of the stack and returns the value of the top element to e, and return 1 indicates success, return 0 indicates failure {if (S. top <= 0) {cout <"Stack is empty! "<Endl; return 0;} else {* e = S. stack [S. top-1]; // return 1;} int PushStack (SeqStack * S, ElemType e) // Add Element e to stack, 1 is returned for successful element loading into the stack. Otherwise, 0 {if (S-> top> = STACKSIZE-1) {cout <"the stack is full and cannot be added to the stack! "; Return 0;} else {S-> stack [S-> top] = e; S-> top ++; return 1 ;}} int PopStack (SeqStack * S, elemType * e) // The output stack operation {if (S-> top <= 0) {cout <"the stack has no elements and cannot be output! "<Endl; return 0;} else {S-> top --; * e = S-> stack [S-> top]; return 1 ;}} int StackLength (SeqStack S) // return stack length {return S. top;} void ClearStack (SeqStack * S) // clear stack {S-> top = 0;} void LineEdit () // The row editing function {SeqStack S; char ch; elemType e; ElemType a [50]; int I, j = 0; InitStack (& S); cout <"input Character Sequence (# indicates that the previous character is invalid, @ indicates that the current row character is invalid) "<endl; ch = getchar (); while (ch! = '\ N') {switch (ch) {case' # ': if (! StackEmpty (S) {PopStack (& S, & ch); // stack top element exit stack} break; case '@': ClearStack (& S ); // clear the stack break; default: PushStack (& S, ch); // character stack feed} ch = getchar (); // read the next character} while (! StackEmpty (S) {PopStack (& S, & e); // output the stack character and store it in the array. a [j ++] = e ;} for (I = j-1; I> = 0; I --) {cout <a [I]; // output the correct Character Sequence} cout <endl; clearStack (& S); // prepares for the next input}

#include"Edit.h"int main(void){LineEdit();return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.