Создать связный мультисписок на основе указателей с использованием принципов ООП - C#
Формулировка задачи:
Здравствуйте. Имею задание: создать связный мультисписок на основе указателей с использованием принципов ООП. Для реализации решил выбрать C#. И вот вопрос: мне нужен указатель объекта на другой объект того же класса, который я объявляю как класс из двух полей: значения и указателя. Ругаться начинает. То есть в структурах все нормально:
А в классах не хочет:
Не подскажите, как можно этого избежать? Просто мне надо будет от этого класса наследовать потом еще несколько других, поэтому тупо запихнуть все в структуру не получится...
Листинг программы
- unsafe struct abcd
- {
- int val;
- abcd* next;
- }
Листинг программы
- unsafe public abstract class ListElem
- {
- private string value;
- private ListElem* next;
- }
Error 2 Cannot take the address of, get the size of, or declare a pointer to a managed type ('ExamList.Form1.ListElem')
Грубо говоря мне нужен класс, каждый объект которого знает, кто стоит после него в списке.
Решение задачи: «Создать связный мультисписок на основе указателей с использованием принципов ООП»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace ExamList
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- public SimpleList firstSimpleElm;
- public abstract class MyList
- {
- public string value { get; set; }
- }
- public class SimpleList: MyList
- {
- public SimpleList next;
- public SimpleList(string value)
- {
- this.value = value;
- this.next = null;
- }
- static public void Add(ref SimpleList localFirstSimpleElm, string value)
- {
- if (value == "") return;
- SimpleList newSimpleElm = new SimpleList(value);
- if (localFirstSimpleElm == null)
- {
- localFirstSimpleElm = newSimpleElm;
- return;
- }
- SimpleList elm = localFirstSimpleElm;
- while (elm.next != null)
- elm = elm.next;
- elm.next = newSimpleElm;
- }
- static public string PrintList(SimpleList firstElm)
- {
- if (firstElm == null) return "";
- string result = "";
- SimpleList currentSimpleElm = firstElm;
- do
- {
- result += currentSimpleElm.value + "\n";
- try { currentSimpleElm = currentSimpleElm.next; }
- catch { }
- } while (currentSimpleElm != null);
- return result;
- }
- }
- private void prevButton_Click(object sender, EventArgs e)
- {
- }
- private void nextButton_Click(object sender, EventArgs e)
- {
- }
- private void newSimpleButton_Click(object sender, EventArgs e)
- {
- SimpleList.Add(ref firstSimpleElm, newSimpleListBox.Text);
- contentLabel.Text = SimpleList.PrintList(firstSimpleElm);
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д