Я создаю приложение для моего C# класса в школе, которое читает команды из текстового файла и отображает результаты в консоли. В основном в файле будут команды, которые должны добавить узел и удалить узел из односвязного списка. Так что, если во входном файле он читает I: 1, I: 7, он выведет Node1: 1 и Node2: 7 на консоль.
Я знаю, как анализировать файл для отображения таких вещей, как hello world и т. Д. c. Но я не уверен в том, как мне проанализировать файл для поиска указанной строки c и затем вывести результаты на основе этой строки (в этом случае добавление и удаление узлов из списка). Есть ли ресурсы о том, как это сделать?
Вот что у меня есть для моей программы:
public class LinkedList {
Node head; //the head of list
public class Node {
public int data;
public Node next;
//constructor
public Node(int d) {
data = d;
next = null;
} //end of constructor
}
public void printList() { //traversing list and printing the contents starting from head(1)
Node n = head;
while (n != null) {
Console.Write(n.data + " ");
n = n.next;
}
}
public void push(int new_data) {
Node new_node = new Node(new_data); //allocate new node, put in data
new_node.next = head; //make next of new node as head
head = new_node; //move the head to point to new node
}
//main method to create a linked list with 3 nodes
public static void Main(String[] args) {
//starting with an empty list
LinkedList llist = new LinkedList();
llist.head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
//now 3 nodes have been allocated. head, second, and third
llist.head.next = second; //linking the head (1st) with the second node. these are both now linked.
second.next = third; //linking second with third. now linked. all 3 are linked now
llist.printList();
}
} //end of class program