Implementering af "Linked lists"
Hej Eksperter,Jeg er igang med et lille forsøgsprojekt, for at lære at programmere i visual studio C#.
Projektet er et lille musikprogram som ser ud som i nedenstående kode, og skal alle de steder der anvendt arrays/records bruge hægtede lister / linked list, men ved ikke hvordan jeg skal gribe det an.
Nuværende kode:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class bla : Form
{
public class playlist
{
public String title;
public playlist next;
}
static playlist head = null;
static void headInsert(playlist elem)
{
elem.next = head;
head = elem;
}
static void headRemove()
{
head = head.next;
}
static int aAlbum = 3;
public Album[] albs = new Album[aAlbum];
private void sange()
{
int cd = comboBox1.SelectedIndex;
listBox2.Items.Clear();
for (int i = 0; i < albs[comboBox1.SelectedIndex].sange.Length; i++)
{
listBox2.Items.Add(albs[comboBox1.SelectedIndex].sange[i].sTitel);
}
}
private int albumtidm(int cd)
{
int sum = 0;
for (int y = 0; y < albs[cd].sange.Length; y++)
{
sum = sum + albs[cd].sange[y].sTid;
}
return sum;
}
public Bla()
{
InitializeComponent();
for (int i = 0; i < aAlbum; i++)
{
albs[i] = new Album();
}
albs[0].aTitel = "";
albs[0].kunstner = "";
albs[0].aSpilletid = 0;
albs[0].sange = new Sang[12];
for (int i = 0; i < albs[0].sange.Length; i++)
{
albs[0].sange[i] = new Sang();
}
albs[0].sange[0].sTitel = "";
albs[0].sange[1].sTitel = "";
albs[0].sange[2].sTitel = "";
albs[0].sange[3].sTitel = "";
albs[0].sange[4].sTitel = "";
albs[0].sange[5].sTitel = "";
albs[0].sange[6].sTitel = "";
albs[0].sange[7].sTitel = "";
albs[0].sange[8].sTitel = "";
albs[0].sange[9].sTitel = "";
albs[0].sange[10].sTitel = "";
albs[0].sange[11].sTitel = "";
albs[0].sange[0].sTid = 101;
albs[0].sange[1].sTid = 189;
albs[0].sange[2].sTid = 198;
albs[0].sange[3].sTid = 173;
albs[0].sange[4].sTid = 290;
albs[0].sange[5].sTid = 205;
albs[0].sange[6].sTid = 233;
albs[0].sange[7].sTid = 221;
albs[0].sange[8].sTid = 196;
albs[0].sange[9].sTid = 196;
albs[0].sange[10].sTid = 218;
albs[0].sange[11].sTid = 383;
albs[1].aTitel = "";
albs[1].kunstner = "";
albs[1].aSpilletid = 0;
albs[1].sange = new Sang[11];
for (int i = 0; i < albs[1].sange.Length; i++)
{
albs[1].sange[i] = new Sang();
}
albs[1].sange[0].sTitel = "";
albs[1].sange[1].sTitel = "";
albs[1].sange[2].sTitel = "";
albs[1].sange[3].sTitel = "";
albs[1].sange[4].sTitel = "";
albs[1].sange[5].sTitel = "";
albs[1].sange[6].sTitel = "";
albs[1].sange[7].sTitel = "";
albs[1].sange[8].sTitel = "";
albs[1].sange[9].sTitel = "";
albs[1].sange[10].sTitel = "";
albs[1].sange[0].sTid = 173;
albs[1].sange[1].sTid = 197;
albs[1].sange[2].sTid = 237;
albs[1].sange[3].sTid = 225;
albs[1].sange[4].sTid = 192;
albs[1].sange[5].sTid = 200;
albs[1].sange[6].sTid = 230;
albs[1].sange[7].sTid = 253;
albs[1].sange[8].sTid = 247;
albs[1].sange[9].sTid = 272;
albs[1].sange[10].sTid = 187;
albs[2].aTitel = "";
albs[2].kunstner = "";
albs[2].aSpilletid = 0;
albs[2].sange = new Sang[10];
for (int i = 0; i < albs[2].sange.Length; i++)
{
albs[2].sange[i] = new Sang();
}
albs[2].sange[0].sTitel = "";
albs[2].sange[1].sTitel = "";
albs[2].sange[2].sTitel = "";
albs[2].sange[3].sTitel = "";
albs[2].sange[4].sTitel = "";
albs[2].sange[5].sTitel = "";
albs[2].sange[6].sTitel = "";
albs[2].sange[7].sTitel = "";
albs[2].sange[8].sTitel = "";
albs[2].sange[9].sTitel = "";
albs[2].sange[0].sTid = 215;
albs[2].sange[1].sTid = 212;
albs[2].sange[2].sTid = 255;
albs[2].sange[3].sTid = 207;
albs[2].sange[4].sTid = 211;
albs[2].sange[5].sTid = 241;
albs[2].sange[6].sTid = 168;
albs[2].sange[7].sTid = 241;
albs[2].sange[8].sTid = 201;
albs[2].sange[9].sTid = 195;
for (int i = 0; i < aAlbum; i++)
{
comboBox1.Items.Add(albs[i].kunstner);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.Items.Clear();
listBox2.Items.Clear();
listBox1.Items.Add(albs[comboBox1.SelectedIndex].aTitel);
}
private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
int cd = comboBox1.SelectedIndex;
albs[cd].aSpilletid = 0;
sange();
textBox6.Text = albs[cd].aSpilletid / 60 + "min " + albs[cd].aSpilletid % 60 + "sec";
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
int cd = comboBox1.SelectedIndex;
int titel = listBox2.SelectedIndex;
textBox1.Text = albs[cd].kunstner.ToString();
textBox2.Text = albs[cd].aTitel.ToString();
textBox3.Text = albs[cd].sange[titel].sTitel;
textBox4.Text = albs[cd].sange.Length.ToString();
textBox5.Text = albs[cd].sange[titel].sTid / 60 + "min " + albs[cd].sange[titel].sTid % 60 + "sec";
}
private void button1_Click(object sender, EventArgs e)
{
int cd = comboBox1.SelectedIndex;
albs[cd].sange[listBox2.SelectedIndex].sTitel= textBox3.Text;
sange();
}
private void button2_Click(object sender, EventArgs e)
{
int sum = 0;
for (int i = 0; i < aAlbum; i++)
{
sum = sum + albumtidm(i);
}
sum = sum / aAlbum;
MessageBox.Show(sum / 60 + "min" + sum % 60 + "sec");
}
private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
playlist tmp;
int Sang = listBox2.SelectedIndex;
int cd = comboBox1.SelectedIndex;
tmp = new playlist();
tmp.title = albs[cd].sange[Sang].sTitel;
headInsert(tmp);
listBox3.Items.Clear();
for (playlist tmp1 = head; tmp1 != null; tmp1 = tmp1.next)
{
listBox3.Items.Add(tmp1.title);
}
}
private void button4_Click(object sender, EventArgs e)
{
headRemove();
listBox3.Items.Remove(listBox3.SelectedItem);
}
private void bla_Load(object sender, EventArgs e)
{
}
}
public class Album
{
public String aTitel;
public String kunstner;
public int aSpilletid;
public Sang[] sange;
}
public class Sang
{
public String sTitel;
public int sTid;
}
}
På forhånd tak.