Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How can I make a complicated linked list

Dao Tran MinhDao Tran Minh Posts: 4Member
I design a class "Item", and two classes "Table", "Chair" inherited from class "Item".
I want to make a linked list that each node can be "Table" or "Chair", so I decide to use the linked list of "Item", hope that each object can be declared like this:
Item one_node = new Table();
But then, I can not use the variable "one_node" as an object of "Table".
That is my problem. Anyone can help me to create a linked list where each node can be an object of some classes inherited from one class? And can the depth of the inheritance be more than one?
Sorry if my english is bad.

Comments

  • yuvalyuval Posts: 13Member
    : I design a class "Item", and two classes "Table", "Chair" inherited from class "Item".
    : I want to make a linked list that each node can be "Table" or "Chair", so I decide to use the linked list of "Item", hope that each object can be declared like this:
    : Item one_node = new Table();
    : But then, I can not use the variable "one_node" as an object of "Table".
    : That is my problem. Anyone can help me to create a linked list where each node can be an object of some classes inherited from one class? And can the depth of the inheritance be more than one?
    : Sorry if my english is bad.
    :

  • yuvalyuval Posts: 13Member
    [b][red]This message was edited by yuval at 2003-6-29 5:54:40[/red][/b][hr]
    Dao,

    1. Your english is good.
    2. try this:

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    namespace ListOfClass
    {
    ///
    /// Summary description for Form1.
    ///
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    ///
    /// Required designer variable.
    ///
    private System.ComponentModel.Container components = null;

    public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();

    //
    // TODO: Add any constructor code after InitializeComponent call
    //
    }

    ///
    /// Clean up any resources being used.
    ///
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    ///
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    ///
    private void InitializeComponent()
    {
    this.label1 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    //
    // label1
    //
    this.label1.Location = new System.Drawing.Point(72, 40);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(152, 104);
    this.label1.TabIndex = 0;
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
    this.label1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false);

    }
    #endregion

    ///
    /// The main entry point for the application.
    ///
    [STAThread]
    static void Main()
    {
    Application.Run(new Form1());
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {

    Table table1 = new Table();
    Table table2 = new Table();
    Chair chair1 = new Chair();
    Chair chair2 = new Chair();

    ArrayList items = new ArrayList();

    items.Add (table1);
    items.Add (table2);
    items.Add (chair1);
    items.Add (chair2);

    table1.color=Color.Red ;
    table2.color =Color.Black;
    chair1.color = Color.Green;
    chair2.color = Color.Brown;




    foreach (Item i in items)
    {
    string type= i.GetType().ToString();

    if (type=="ListOfClass.Table")
    {
    label1.Text += ((Table)i).color.ToString ()+"

    ";

    }

    if (type=="ListOfClass.Chair")
    {
    label1.Text +=(((Chair)i).color).ToString() +"

    ";
    }
    }


    }
    }

    class Item
    {

    public Item()
    {

    }

    }

    class Table:Item
    {
    public Color color ;
    public int width ;
    public int length;
    public string style;


    public Table()
    {
    }



    }

    class Chair:Item
    {
    public Color color ;
    public int width ;
    public int length;
    public string style;


    public Chair()
    {
    }


    }
    }


    3. Copy the text (cntl C).
    4. Open a new window project. Delete everything.
    5. paste the above code.
    6. See if it fits your needs.

    best of luck!


Sign In or Register to comment.