I'm new to C# and was wondering how do I swap Nodes in a LinkedList.
Here's my code:
Node.cs
Expand|Select|Wrap|Line Numbers
- class Node
- {
- private MusicCD data;
- public MusicCD Data
- {
- get { return data; }
- set { data = value; }
- }
- private Node link;
- internal Node Link
- {
- get { return link; }
- set { link = value; }
- }
- public Node(MusicCD d)
- {
- this.data = d;
- }
- }
Expand|Select|Wrap|Line Numbers
- using System;
- class LinkedList
- {
- private Node head; // first node in the linked list
- private int count;
- public int Count
- {
- get { return count; }
- set { count = value; }
- }
- public Node Head
- {
- get { return head; }
- }
- public LinkedList()
- {
- head = null; // creates an empty linked list
- count = 0;
- }
- public void DeleteFront()
- {
- if (count > 0)
- {
- Node temp = head;
- head = temp.Link;
- temp = null;
- count--;
- }
- }
- public void AddMusicCDToFront(MusicCD cd)
- {
- Node newNode = new Node(cd);
- newNode.Link = head;
- head = newNode;
- count++;
- }
- public void DisplayInventory()
- {
- Node temp = head;
- while (temp != null)
- {
- Console.WriteLine(temp.Data);
- temp = temp.Link;
- }
- }
- public void RemoveMusicCDAtPosition(int n)
- {
- if (n <= count)
- {
- Node tempNode = head;
- Node lastNode = null;
- if (n == 0)
- {
- head = null;
- }
- while (tempNode != null)
- {
- if (count == n - 1)
- {
- lastNode.Link = tempNode.Link;
- }
- count++;
- lastNode = tempNode;
- tempNode = tempNode.Link;
- }
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- public class MusicCD
- {
- public int id
- {
- get { return id; }
- set { id = value; }
- }
- public string singerName
- {
- get { return singerName; }
- set { singerName = value; }
- }
- public string albumName
- {
- get { return albumName; }
- set { albumName = value; }
- }
- public MusicCD(int id, string singerName, string albumName)
- {
- this.id = id;
- this.singerName = singerName;
- this.albumName = albumName;
- }
- }
Expand|Select|Wrap|Line Numbers
- public void MusicCDSwap(int n1, int n2)
- {
- Node tempNode = head;
- }
Is my LinkedList missing something? Or is my Node missing things?