Удалить из списка первую фамилию, начинающуюся с буквы, вводимой с клавиатуры - C#
Формулировка задачи:
Создать очередь, информационные поля которой содержат строки из файла (список фамилий учащихся). Удалить из списка первую фамилию, начинающуюся с буквы, вводимой с клавиатуры. (Учесть, что такая фамилия может оказаться первой в списке.)
Программа исправно удаляет, если первый элемент
Если иной, то удаляется 3-ий элемент
Разбор дебагером и проверка значений не помогли
Подскажите где искать ошибку
Заранее спасибо!
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace task632
- {
- class Node // Узел для списка, стека, очереди
- {
- public string inf;
- public Node next;
- public Node(string inf, Node next) // Конструктор
- {
- this.inf = inf;
- this.next = next;
- }
- }
- class MyQueue // Класс очередь
- {
- Node head; // Голова
- Node tail;
- public MyQueue()
- {
- head = null;
- tail = null;
- }
- public bool QueueIsEmpty() // Проверка на пустоту
- {
- return head == null;
- }
- public void InQueue(string inf) // Положить в хвост очереди
- {
- Node p = new Node(inf, null);
- if (QueueIsEmpty())
- {
- head = p; tail = p;
- }
- else
- {
- tail.next = p; tail = p;
- }
- }
- public int Search() // Ищем индекс фамилии
- {
- Node p = head;
- int i = 0;
- Console.WriteLine("Введите букву");
- Char Ch = Convert.ToChar(Console.ReadLine());
- do
- {
- int found = p.inf.IndexOf(Ch);
- if (found == 0)
- return i;
- else
- i++;
- p = p.next;
- }
- while (p != null);
- return -1;
- }
- public void Delete(int index) // Удаляем фамилию по индексу
- {
- if (index != 0)
- {
- Node p = head;
- for (int i = index; i < index + 1; i++)
- p = p.next;
- if (p.next != null)
- p.next = p.next.next;
- }
- else
- head = head.next;
- }
- public void Print()
- {
- Node p = head;
- Console.WriteLine();
- do
- {
- Console.WriteLine(" {0} ", p.inf);
- p = p.next;
- }
- while (p != null);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- int n = 5;
- FileStream File = new FileStream("C:\\text.txt", FileMode.Open);
- StreamReader f1 = new StreamReader(File);
- MyQueue queue = new MyQueue();
- string m = null;
- for (int i = 0; i < n; i++)
- {
- m = f1.ReadLine();
- queue.InQueue(m);
- }
- f1.Close();
- File.Close();
- int index = queue.Search();
- if (index != -1)
- queue.Delete(index);
- queue.Print();
- Console.ReadKey();
- }
- }
- }
Решение задачи: «Удалить из списка первую фамилию, начинающуюся с буквы, вводимой с клавиатуры»
textual
Листинг программы
- public void Delete(int index) // Удаляем фамилию по индексу
- {
- if (index != 0)
- {
- Node p = head;
- Node before;
- Node after;
- for (int i = 0; i < index-1; ++i)
- {
- p = p.next;
- }
- before = p;
- after = p.next.next;
- before.next = after;
- }
- else
- head = head.next;
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д