473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Swapping Nodes in a custom LinkedList

Hey there everyone,

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
  1.     class Node
  2.     {
  3.         private MusicCD data;
  4.  
  5.         public MusicCD Data
  6.         {
  7.             get { return data; }
  8.             set { data = value; }
  9.         }
  10.         private Node link;
  11.  
  12.         internal Node Link
  13.         {
  14.             get { return link; }
  15.             set { link = value; }
  16.         }
  17.  
  18.         public Node(MusicCD d)
  19.         {
  20.             this.data = d;
  21.         }
  22.     }
  23.  
LinkedList.cs
Expand|Select|Wrap|Line Numbers
  1. using System;
  2.  
  3.     class LinkedList
  4.     {
  5.         private Node head;     // first node in the linked list
  6.         private int count;
  7.  
  8.         public int Count
  9.         {
  10.             get { return count; }
  11.             set { count = value; }
  12.         }
  13.  
  14.         public Node Head
  15.         {
  16.             get { return head; }
  17.         }
  18.         public LinkedList()
  19.         {
  20.             head = null;     // creates an empty linked list
  21.             count = 0;
  22.         }
  23.  
  24.         public void DeleteFront()
  25.         {
  26.             if (count > 0)
  27.             {
  28.                 Node temp = head;
  29.                 head = temp.Link;
  30.                 temp = null;
  31.                 count--;
  32.             }
  33.         }
  34.  
  35.         public void AddMusicCDToFront(MusicCD cd)
  36.         {
  37.             Node newNode = new Node(cd);
  38.             newNode.Link = head;
  39.             head = newNode;
  40.             count++;
  41.         }
  42.  
  43.         public void DisplayInventory()
  44.         {
  45.             Node temp = head;
  46.             while (temp != null)
  47.             {
  48.                 Console.WriteLine(temp.Data);
  49.                 temp = temp.Link;
  50.             }
  51.         }
  52.  
  53.         public void RemoveMusicCDAtPosition(int n)
  54.         {
  55.             if (n <= count)
  56.             {
  57.                 Node tempNode = head;
  58.                 Node lastNode = null;
  59.                 if (n == 0)
  60.                 {
  61.                     head = null;
  62.                 }
  63.                 while (tempNode != null)
  64.                 {
  65.                     if (count == n - 1)
  66.                     {
  67.                         lastNode.Link = tempNode.Link;
  68.                     }
  69.                     count++;
  70.                     lastNode = tempNode;
  71.                     tempNode = tempNode.Link;
  72.                 }
  73.             }
  74.         }
  75.     }
  76.  
MusicCD.cs
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. public class MusicCD
  8. {
  9.     public int id
  10.     {
  11.         get { return id; }
  12.         set { id = value; }
  13.     }
  14.     public string singerName
  15.     {
  16.         get { return singerName; }
  17.         set { singerName = value; }
  18.     }
  19.     public string albumName
  20.     {
  21.         get { return albumName; }
  22.         set { albumName = value; }
  23.     }
  24.     public MusicCD(int id, string singerName, string albumName)
  25.     {
  26.         this.id = id;
  27.         this.singerName = singerName;
  28.         this.albumName = albumName;
  29.     }
  30. }
  31.  
I tried to add this into my LinkedList.cs
Expand|Select|Wrap|Line Numbers
  1. public void MusicCDSwap(int n1, int n2)
  2.         {
  3.             Node tempNode = head;
  4.         }
  5.  
but I'm stuck there, as I don't have the relevant knowledge on how to swap Nodes in a custom LinkedList.

Is my LinkedList missing something? Or is my Node missing things?
Dec 20 '13 #1
0 1333

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: J Peterman | last post by:
Im having a nightmare trying to understand these nodes and linked lists. I've posted my code for my node.h, node.cpp, linkedlist.h and linkedlist.cpp files in separates replies. Can someone...
3
by: Christopher Jeris | last post by:
Please help me understand the differences, in semantics, browser support and moral preferredness, between the following three methods of swapping content in and out of a page via JavaScript. I...
4
by: alanrn | last post by:
I am using a TreeView to display the hierarchy of a strongly-typed collection (inherited from CollectionBase). The order of the nodes in the TreeView is strictly tied to the order in which they...
5
by: CR | last post by:
I've been to figure out how to get AddSortLast function to add nodes in acsending order (a b c d) I figured it out how to get it to sort in decending order but I can't get it to sort in acsending...
6
by: jw | last post by:
hi all,i have such 2 classes for a linked list, class Node{ private: int value; Node *next; public: Node() { next=NULL; }
2
by: Justin Crites | last post by:
I have an object which I want to be serializable. I have marked with with . The object only has a single data member, which is a LinkedList<int>. This linked list is a private member and cannot...
15
by: mathon | last post by:
Hi, i am currently working on creating a LinkedList, for that I using a predefined Node-class which offers a LinkedList Toolkit to manipulate the Linked List...
20
by: martin-g | last post by:
Hi. Mostly I program in C++, and I'm not fluent in C# and .NET. In my last project I began to use LinkedList<and suddenly noticed that can't find a way to sort it. Does it mean I must implement...
2
by: ICPooreMan | last post by:
I have a page which I'm trying to add ajax functionality to however I'm having some difficulties. Basically I have a section of my page that looks like this <div id="pageDiv"> <!--a random...
0
by: KUTTAN | last post by:
I have an xml file as follows <?xml version="1.0" encoding="utf-8" ?> <Nodes> <Nodes1> <siteMapNode title="Product Cataloge">
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.