As a prerequisite, here is the code for my two classes, Station and SubwaySystem:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assignment7
{
    class Station
    {
        //attributes
        private string stName;
        private int stID;
        private Station next;
        private Station previous;

        //properties
        public string StName { get { return this.stName; } }
        public int StID { get { return this.stID; } }
        public Station Next { get; set; }
        public Station Previous { get; set; }

        //constructor
        public Station(string name, int ID)
        {
            this.stName = name;
            this.stID = ID;
        }

        //ToString override
        public override string ToString()
        {
            return "Name: " + stName + ", ID number: " + stID;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assignment7
{
    class SubwaySystem
    {
        //attributes
        Dictionary<int, Station> stationDictionary = new Dictionary<int, Station>();

        //methods
        /**
         * Inserts a new station ahead of one with the given ID
         **/
        void InsertAhead(int id, Station insert)
        {
            if (stationDictionary.Count == 0)
            {
                stationDictionary.Add(insert.StID, insert);
                insert.Next = insert;
                insert.Previous = insert;
            }
            else
            {
                if (stationDictionary.ContainsKey(id) == false)
                    Console.WriteLine("There is no element at the given ID.");
                else if (stationDictionary.ContainsKey(insert.StID))
                    Console.WriteLine("The ID of the station being added already exists.");
                else
                {
                    stationDictionary.Add(insert.StID, insert);
                    insert.Next = stationDictionary.ElementAt<KeyValuePair<int, Station>>(id).Value.Next;
                    insert.Previous = stationDictionary.ElementAt<KeyValuePair<int, Station>>(id).Value;
                    stationDictionary.ElementAt<KeyValuePair<int, Station>>(id).Value.Previous = insert;
                    insert.Previous.Next = insert;
                }
            }
        }//End InsertAhead
        /**
         * Inserts a new station before one with the given ID
         **/
        void InsertBehind(int id, Station insert)
        {
            if (stationDictionary.Count == 0)
            {
                stationDictionary.Add(insert.StID, insert);
                insert.Next = insert;
                insert.Previous = insert;
            }
            else
            {
                if (stationDictionary.ContainsKey(id) == false)
                    Console.WriteLine("There is no element at the given ID.");
                else if (stationDictionary.ContainsKey(insert.StID))
                    Console.WriteLine("The ID of the station being added already exists.");
                else
                {
                    stationDictionary.Add(insert.StID, insert);
                    insert.Next = stationDictionary.ElementAt<KeyValuePair<int, Station>>(id).Value;
                    insert.Previous = stationDictionary.ElementAt<KeyValuePair<int, Station>>(id).Value.Previous;
                    insert.Next.Previous = insert;
                    insert.Previous.Next = insert;
                }
            }
        }
        /**
         * Removes station with specified ID
         **/
        void RemoveStation(int id)
        {
            if (stationDictionary.ContainsKey(id) == false)
                Console.WriteLine("There is no element at the given ID.");
            else
            {
                stationDictionary.Remove(id);
            }
        }
        /**
         * Starts at the station with the specified id, following the references forward around the subway line,
         * writing the station name and id until returning to the starting station.
         **/
        void TraverseSubwayLine(int startId)
        {
            //Make sure dictionary has some elements
            if (stationDictionary.Count == 0)
                Console.WriteLine("There are no stations in the dictionary.");

                //Make sure a station at the given id exists
            else if (stationDictionary.ContainsKey(startId) == false)
                Console.WriteLine("There is no element at the given ID.");
            else
            {
                //Write out the first station
                Station currentStation = stationDictionary.ElementAt<KeyValuePair<int, Station>>(startId).Value;
                Console.WriteLine(currentStation.ToString());
                currentStation = currentStation.Next;

                //Check for next station pretest, then loop until all are written
                while (stationDictionary.ElementAt<KeyValuePair<int, Station>>(startId).Key != currentStation.StID)
                {
                    Console.WriteLine(currentStation.ToString());
                    currentStation = currentStation.Next;
                }
            }
        }

        static void Main(string[] args)
        {
            SubwaySystem subSystem = new SubwaySystem();
            //1. Insert a new station with ID 1 and name Boardwalk.
            subSystem.stationDictionary.Add(1, new Station("Boardwalk", 1));
            //2. Insert a new station with ID 6 and name Park Place ahead of the station with ID 1.
            subSystem.InsertAhead(1, new Station("Boardwalk", 6));
            //3. Insert a new station with ID 3 and name New York Ave. behind the station with ID 1.
            subSystem.InsertBehind(1, new Station("New York Ave.", 3));
            //4. Insert a new station with ID 1 and name B&O Railroad ahead of the station with ID 20.
            subSystem.InsertAhead(20, new Station("B&O Railroad", 1));
            //5. Insert a new station with ID 7 and name Baltic Ave. ahead of the station with ID 3.
            subSystem.InsertAhead(3, new Station("Baltic Ave.", 7));
            //6. Insert a new station with ID 2 and name Marvin Gardens behind the station with ID 1.
            subSystem.InsertBehind(1, new Station("Marvin Gardens", 2));
            //7. Insert a new station with ID 4 and name Illinois Ave. behind the station with ID 7.
            subSystem.InsertAhead(7, new Station("Illinois Ave.", 4));
            //8. Insert a new station with ID 6 and name Mediterranean Ave. behind the station with ID 3.
            subSystem.InsertBehind(3, new Station("Mediterranean Ave.", 6));
            //9. Record the list of subway stations in the order they lie on the subway line.
            subSystem.TraverseSubwayLine(1);
            //10. Remove the station with ID 15.
            subSystem.RemoveStation(15);
            //11. Remove the station with ID 3.
            subSystem.RemoveStation(3);
            //12. Record the list of subway stations in the order they lie on the subway line.
            subSystem.TraverseSubwayLine(1);
        }
    }
}

Now at line 34 of the SubwaySystem class I get an out of range exception, and I have no idea why. Any help?

Recommended Answers

All 9 Replies

I didn't go over your code in great detail. I was able to run it with the following output:

There is no element at the given ID.
The ID of the station being added already exists.
Name: Boardwalk, ID number: 1
Name: Boardwalk, ID number: 6
Name: New York Ave., ID number: 3
Name: Marvin Gardens, ID number: 2
There is no element at the given ID.
Name: Boardwalk, ID number: 1
Name: Boardwalk, ID number: 6
Name: New York Ave., ID number: 3
Name: Marvin Gardens, ID number: 2

I think (unless I am confused by the usage) you are using a roundabout and incorrect method of accessing the dictionary. I know you had another thread about this but it doesn't look like you were able to change it. I'll give you a couple of lines and you can fix the rest in turn.
Since your id is an integer you can use the [] notation to access elements at a particular integer key:
(from lines 61 and 62 of the second file)

insert.Next = stationDictionary[id].Next;
//since stationDictionary[key] give you Value for that key
insert.Previous = stationDictionary[id];

Also, when you are using the automatic properties you don't need to have an explicitly defined private variable to hold the data-- so you don't need lines 13 and 14 of the first file.

I didn't go over your code in great detail. I was able to run it with the following output:

There is no element at the given ID.
The ID of the station being added already exists.
Name: Boardwalk, ID number: 1
Name: Boardwalk, ID number: 6
Name: New York Ave., ID number: 3
Name: Marvin Gardens, ID number: 2
There is no element at the given ID.
Name: Boardwalk, ID number: 1
Name: Boardwalk, ID number: 6
Name: New York Ave., ID number: 3
Name: Marvin Gardens, ID number: 2

I think (unless I am confused by the usage) you are using a roundabout and incorrect method of accessing the dictionary. I know you had another thread about this but it doesn't look like you were able to change it. I'll give you a couple of lines and you can fix the rest in turn.
Since your id is an integer you can use the [] notation to access elements at a particular integer key:
(from lines 61 and 62 of the second file)

insert.Next = stationDictionary[id].Next;
//since stationDictionary[key] give you Value for that key
insert.Previous = stationDictionary[id];

Also, when you are using the automatic properties you don't need to have an explicitly defined private variable to hold the data-- so you don't need lines 13 and 14 of the first file.

Oh jeez, I can't believe I couldn't think of something so simple. Thanks a lot, I'm gonna try that now.

Okay so that method worked for fixing most of it, but now at line 104 I'm getting a null reference exception. In fact, all of my lines like that have been getting null reference exceptions and I can't figure out why in the hell it's happening. How exactly did you get it to run? Here's the method I'm having trouble with, specifically.

void TraverseSubwayLine(int startId)
        {
            //Make sure dictionary has some elements
            if (stationDictionary.Count == 0)
                Console.WriteLine("There are no stations in the dictionary.");

                //Make sure a station at the given id exists
            else if (stationDictionary.ContainsKey(startId) == false)
                Console.WriteLine("There is no element at the given ID.");
            else
            {
                //Write out the first station
                Station currentStation = stationDictionary[startId];
                Console.WriteLine(currentStation.ToString());
                currentStation = currentStation.Next;

                //Check for next station pretest, then loop until all are written
                while (stationDictionary[startId] != currentStation)
                {
                    Console.WriteLine(currentStation.ToString());
                    currentStation = currentStation.Next;
                }
            }

It would be lines 20 and 21 there giving me trouble now.

And just kidding again... before that, lines 63 and 64 of the code before are giving me a null reference exception.

It would be lines 20 and 21 there giving me trouble now.

It's line 18. Look at what you are comparing.

(stationDictionary[startID] = resolves to string) != (currentStation = int)

lines 63 and 64 of the code before are giving me a null reference exception

Check over lines 61 and 62 again. Make sure there are no marked errors or warnings in your program before you try it.

It's line 18. Look at what you are comparing.

(stationDictionary[startID] = resolves to string) != (currentStation = int)

Check over lines 61 and 62 again. Make sure there are no marked errors or warnings in your program before you try it.

Okay so I changed 18 to

while (stationDictionary[startId].StID != currentStation.StID)

and it's still giving me a null reference exception. Those should both be ints, so I'm not sure why.. As for lines 61 and 62 there aren't any marked errors or warnings.

Okay so my programming partner fixed up the null reference errors nicely.
Now my problem is the output is not correct... my methods don't appear to be working correctly. I've examined them and can't quite figure out what's causing the problem... Here's the new code for SubwaySystem, class Station has remained unchanged.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assignment7
{
    class SubwaySystem
    {
        //attributes
        Dictionary<int, Station> stationDictionary = new Dictionary<int, Station>();

        //methods
        /**
         * Inserts a new station ahead of one with the given ID
         **/
        void InsertAhead(int id, Station insert)
        {
            if (stationDictionary.Count == 0)
            {
                stationDictionary.Add(id, insert);
                insert.Next = insert;
                insert.Previous = insert;
            }
            else
            {
                if (stationDictionary.ContainsKey(id) == false)
                    Console.WriteLine("There is no element at the given ID.");
                else if (stationDictionary.ContainsKey(insert.StID))
                    Console.WriteLine("The ID of the station being added already exists.");
                else
                {
                    Station stationAhead = stationDictionary[id].Next;

                    stationDictionary.Add(insert.StID, insert);
                    insert.Next = stationDictionary[id].Next;
                    insert.Previous = stationDictionary[id];
                    stationDictionary[id].Next = insert;
                    stationAhead.Previous = insert;
                }
            }
        }//End InsertAhead
        /**
         * Inserts a new station behind one with the given ID
         **/
        void InsertBehind(int id, Station insert)
        {
            if (stationDictionary.Count == 0)
            {
                stationDictionary.Add(insert.StID, insert);
                insert.Next = insert;
                insert.Previous = insert;
            }
            else
            {
                if (stationDictionary.ContainsKey(id) == false)
                    Console.WriteLine("There is no element at the given ID.");
                else if (stationDictionary.ContainsKey(insert.StID))
                    Console.WriteLine("The ID of the station being added already exists.");
                else
                {
                    Station stationBehind = stationDictionary[id].Previous;

                    stationDictionary.Add(insert.StID, insert);
                    insert.Next = stationDictionary[id];
                    insert.Previous = stationDictionary[id].Previous;
                    stationDictionary[id].Previous = insert;
                    stationBehind.Next = insert;
                }
            }
        }
        /**
         * Removes station with specified ID
         **/
        void RemoveStation(int id)
        {
            if (stationDictionary.ContainsKey(id) == false)
                Console.WriteLine("There is no element at the given ID.");
            else
            {
                stationDictionary.Remove(id);
            }
        }
        /**
         * Starts at the station with the specified id, following the references forward around the subway line,
         * writing the station name and id until returning to the starting station.
         **/
        void TraverseSubwayLine(int startId)
        {
            //Make sure dictionary has some elements
            if (stationDictionary.Count == 0)
                Console.WriteLine("There are no stations in the dictionary.");

                //Make sure a station at the given id exists
            else if (stationDictionary.ContainsKey(startId) == false)
                Console.WriteLine("There is no element at the given ID.");
            else
            {
                //Write out the first station
                Station currentStation = stationDictionary[startId];
                Console.WriteLine(currentStation.ToString());
                currentStation = currentStation.Next;

                //Check for next station pretest, then loop until all are written
                while (stationDictionary[startId] != currentStation)
                {
                    Console.WriteLine(currentStation);
                    currentStation = currentStation.Next;
                }
            }
        }

        static void Main(string[] args)
        {
            SubwaySystem subSystem = new SubwaySystem();
            //1. Insert a new station with ID 1 and name Boardwalk.
            subSystem.InsertAhead(1, new Station("Boardwalk", 1));
            //2. Insert a new station with ID 6 and name Park Place ahead of the station with ID 1.
            subSystem.InsertAhead(1, new Station("Park Place", 6));
            //3. Insert a new station with ID 3 and name New York Ave. behind the station with ID 1.
            subSystem.InsertBehind(1, new Station("New York Ave.", 3));
            //4. Insert a new station with ID 1 and name B&O Railroad ahead of the station with ID 20.
            subSystem.InsertAhead(20, new Station("B&O Railroad", 1));
            //5. Insert a new station with ID 7 and name Baltic Ave. ahead of the station with ID 3.
            subSystem.InsertAhead(3, new Station("Baltic Ave.", 7));
            //6. Insert a new station with ID 2 and name Marvin Gardens behind the station with ID 1.
            subSystem.InsertBehind(1, new Station("Marvin Gardens", 2));
            //7. Insert a new station with ID 4 and name Illinois Ave. behind the station with ID 7.
            subSystem.InsertAhead(7, new Station("Illinois Ave.", 4));
            //8. Insert a new station with ID 6 and name Mediterranean Ave. behind the station with ID 3.
            subSystem.InsertBehind(3, new Station("Mediterranean Ave.", 6));
            //9. Record the list of subway stations in the order they lie on the subway line.
            subSystem.TraverseSubwayLine(1);
            //10. Remove the station with ID 15.
            subSystem.RemoveStation(15);
            //11. Remove the station with ID 3.
            subSystem.RemoveStation(3);
            //12. Record the list of subway stations in the order they lie on the subway line.
            subSystem.TraverseSubwayLine(1);
            Console.ReadLine();
        }
    }
}

This generates output

There is no element at the given ID.
The ID of the station being added already exists.
Name: Boardwalk, ID number: 1
Name: Park Place, ID number: 6
Name: New York Ave., ID number: 3
Name: Baltic Ave., ID number: 7
Name: Illinois Ave., ID number: 4
Name: Marvin Gardens, ID number: 2
There is no element at the given ID.
Name: Boardwalk, ID number: 1
Name: Park Place, ID number: 6
Name: New York Ave., ID number: 3
Name: Baltic Ave., ID number: 7
Name: Illinois Ave., ID number: 4
Name: Marvin Gardens, ID number: 2

Which is clearly not in the correct order, nor did it remove station 3 when told.

It did remove station 3. Print out the key value pairs in the dictionary before and after. What happened is you delete the id but you never change the station.Next value for Park Place so that when you are stepping through your list you set station = station.Next and print it even though it doesn't exist anymore.
They are not in the right order because the dictionary holds them in the order it added them. Go through in order by index and see if the index exists and if so print it.

It did remove station 3. Print out the key value pairs in the dictionary before and after. What happened is you delete the id but you never change the station.Next value for Park Place so that when you are stepping through your list you set station = station.Next and print it even though it doesn't exist anymore.
They are not in the right order because the dictionary holds them in the order it added them. Go through in order by index and see if the index exists and if so print it.

Ah, of course. Thanks a lot. Sorry, doing this program and diff eq at the same time is frying my brain.

No prob. It wasn't evident outright. I had to step through it a few times dumping the dictionary as I went along.

diff eq at the same time

Fun is...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.