Hi! I'm learning about linked lists and have created the following class: struct Node { int data; Node *next; Node *prev; }; class BasicList { public: /* CONSTRUCTORS / DESTRUCTORS */ BasicList(); // Default Constructor /* INSERTION METHODS */ void Prepend(int n); // Insert n into list as the first item void Append(int n); // Insert n into list as the last item void InsertAfter(int key, int n); // Insert n into list after key void InsertBefore(int key, int n); // Insert n into list before key void Swap(int first, int second); // Swap specified elements void Remove(int key); // Remove first occurance of key from list /* QUERY METHODS */ void DisplayNodes() const; // Display the data item from each node in the list bool IsEmpty() const; // Return true if the list is empty int Maximum() const; // Return the largest member in the list int Minimum() const; // Return the smallest member in the list int Occurances(int key) const; // Return the number of occurances of key int NumNodes() const; // Return the number of nodes in the list /* HELPER FUNCTIONS */ Node *GetAddress(int n); // Returns the address of the first instance of n found in list int *GetDataArray(); // Return a pointer to an array containing the data. Last item in array is NULL private: /* MEMBERS */ Node *head; // pointer to empty head node Node *tail; // pointer to empty tail node };
All the functions work wonderfully so far. I want to add a method that will sort the linked list data. I'm not really sure where to begin. I checked Wikipedia, and found the following URL which presents a MergeSort algorithm. I'm not sure if this is the best way. Any opinions? http://en.wikipedia.org/wiki/Merge\_sort
budidharma
Posts
-
Sorting Linked Lists -
Visual C++ Express && Code DocumentationHello, I'm a student working towards a BS in CS. I use Visual Studio C++ as my IDE. I go through a lot of paper writing out solutions to problems. I've found that this greatly helps me when designing my code and it's something I've found suggested many many times. Last year, I was playing around with Visual Studio 2005 Beta and found something called "Whitehorse." It was excellent! It allowed me to create diagrams and drawings of my code. It actually would generate drawing and diagrams from my code and modify my code to match a diagram if I asked it to. I haven't found similiar functionality in Visual C++ 2005 Express. I'm (very sadly) assuming that it's not there. I'm REALLY hoping that someone may have an alternative solution for something that works well, and is free. I REALLY appreciate any suggestions. Thank You, Nathan Farrar http://yogensha.com/
-
Reading and Writing XML Configuration FilesI've been reading through the projects here on codeproject, but i still don't fully grasp all of this. I'm writing a simple class that I can use manage application settings in my forms. Note, it is not meant to handle nested elements. There will be one root element "" and inside that will be the settings as key/value pairs. Here's what I've got so far: class Config { private string m_Path; private string m_Err; private XmlDocument xmlDoc; private XmlNode xmlNode; // The Config constructor - supply the path name to the file, load the document public Config(string Path) { m_Path = Path; Load(); } // Load the stored settings into the XmlDocument public bool Load() { try { XmlTextReader xtr = new XmlTextReader(m_Path); } catch (Exception ex) { m_Err = ex.ToString(); return false; } try { xmlDoc = new XmlDocument(); xmlDoc.Load(xtr); } catch (Exception ex) { m_Err = ex.ToString(); return false; } return true; } // Attempt to return the value, given the key name public string GetValue(string key) { // ITERATE THROUGH KEYS, IF FOUND, RETURN THE VALUE, OTHERWISE, return "NULL"; return "NULL"; } // Attempt to set the value of the given key. If it does not exist, create a new key/value // Upon creation or modification of the XmlDocument, write the document to the configuration file public bool SetValue(string key, string value) { // ITERATE THROUGH THE VALUES, FIND THE CORRECT NODE // IF NODE IS FOUND, MODIFY THE VALUE // IF NODE IS NOT FOUND, CREATE THE NEW VALUE return true; } // Return an arraylist containing all key/value pairs as such: "key:value" public ArrayList GetAllValues() { ArrayList aryList = new ArrayList(); // Finish Me! // foreach(key k in XmlDocument) // aryList.Add((string)(key) + (string)(value)); return aryList; } public bool WriteFile()
-
Number of Rows in a DataSet TableThanks!
-
Number of Rows in a DataSet TableI have a DataSet that contains a table "results" which is popuated with data from a database. I need to know the number of rows in the "results" table. Is there a simple way to grab this information? A property or some such? Thanks.
-
ADO.NET: DataSet, DataGridViewFinally just figured it out. I had to specifically set the datagridview datasource to a table index contained within the dataset. I was just setting it to the dataset.
-
ADO.NET: DataSet, DataGridViewSpecifically, in the second file, this is where the datagridview's datasource is set. this.dataGridViewResults.DataSource = this.dataSetPT; Really though, I'm trying to stumble through this and have no idea what I'm doing.
-
ADO.NET: DataSet, DataGridViewSure. I'm using VS 2005 rc3. The code was autogenerated by the IDE. Here's the entire two files, most was autogenerated, excluding the stuff I posted in that first post. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.OleDb; namespace PTAnalyzer { public partial class PTAnalyzer : Form { // DATA ACCESS CODE public OleDbConnection conn; public OleDbDataAdapter dAdapter; public DataSet dSet; public string conString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\ptrack6.mdb"; public PTAnalyzer() { InitializeComponent(); // DATA ACCESS INITIALIZATION try { conn = new OleDbConnection(conString); dAdapter = new OleDbDataAdapter("SELECT * FROM game WHERE player_id = 11", conn); dAdapter.Fill(dataSetPT, "game"); } catch (Exception ex) { MessageBox.Show("Error : " + ex.Message); } //try-catch } } } namespace PTAnalyzer { partial class PTAnalyzer { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (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.dataSetPT = new System.Data.DataSet(); this.dataGridViewResults = new System.Windows.Forms.DataGridView(); ((System.ComponentModel.ISupportInitialize)(this.dataSetPT)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.dataGridViewResults)).BeginInit(); this.SuspendLayout();
-
ADO.NET: DataSet, DataGridViewSorry, that dSet isn't actually used. I should delete that line. The data set is "dataSetPT."
-
ADO.NET: DataSet, DataGridViewThis is my first time trying to work with a database. Let me explain what I've done real quick, then post my code, and you can tell me how I'm retarded. (Please) First, I added an existing database to the project as a new data source. I did not have it automatically generate a typed dataset. Next, in the design window, I added an untyped dataset to my form window. Then I placed a datagridview object in the window. Under the DataGridView properties, I set the datasource to my untyped dataset. I then modified my form code, as so:
public partial class PTAnalyzer : Form { // DATA ACCESS CODE public OleDbConnection conn; public OleDbDataAdapter dAdapter; public DataSet dSet; public string conString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\ptrack6.mdb"; public PTAnalyzer() { InitializeComponent(); // DATA ACCESS INITIALIZATION try { conn = new OleDbConnection(conString); dAdapter = new OleDbDataAdapter("SELECT * FROM game WHERE player_id = 11", conn); dSet = new DataSet(); //refreshes rows in the DataSet dAdapter.Fill(dataSetPT, "game"); } catch (Exception ex) { MessageBox.Show("Error : " + ex.Message); } //try-catch } }
No errors, it runs fine. But nothing is displayed within the datagridview object. What am I doing wrong here? Thanks. -
C# and SQL Express PrimerLet me rephrase that. I work constantly with C#, and I've worked with SQL databases, just not both together. I have a very simple understanding of ADO.NET. I'm most interested in finding out the correct way to use "data sources" via the VS IDE's interface as connections to databases. I hope that makes sense. Thanks again.
-
C# and SQL Express PrimerI'm looking for a tutorial/primer that will explain the very basics of using Visual Studio, C#, and SQL Express to build applications. I've not worked with databases and c# before - so I'm new to this. Anyone know of a good place to find this information? I've been looking, but haven't found anything good. Thanks.
-
Formatting Console.WriteLineAlright, I'm writing a bunch of integers to the screen of different lengths, followed by other numbers. Some are 3 digits long, some are five digits long, and the maximum is about 6 digits long. If the number is less than 6 digits long, I want it to write extra spaces after the number, to make it 6 total characters, before displaying more data. Etc. How do I do this? Thanks, As Always.
-
HashTable Beginning QuestionAlright, i just figured that out as well: foreach(string Key in HandCounter.keys) Console.WriteLine(Key + ": " + HandCounter[Key].ToString()); Very simple, jeez.
-
HashTable Beginning QuestionI just got this to work. How do I enumerate through it, displaying the Key Value pairs?
-
HashTable Beginning QuestionThis is my first time working with hashtables. Here's what I'm trying to do, and what I need to do. First, I have a HashTable that's initialized with the capacity of 169 (the number of possible pocket hands in holdem poker). I deal two random cards to a player - and then create a string that looks like this, from the cards: AKo, AKs, KKo, 32s ... etc. It'll always be in that format. Now, I have a hashtable called HandCounter. What I want to do, is check if that string exists as a "key" in the hash table. If it does, then get the value from that key and increment the value that is stored by 1. If it doesn't, then create a new key/value pair with the pocket string as the key and the int value 1. Here's what I thought the code would probably look like: if(HandCounter.ContainsKey(Hand.ToPocketString())) { HandCounter[Hand.ToPocketString()]++; } else { HandCounter.Add(Hand.ToPocketString(), 1); } Though something is wrong here. I'm attempting to use a string as the key, but it says it requires a type of "object" for the key. I'm attempting to use the value as an integer, but it says it uses type "object" as the value as well. Do I need to typecast these?
-
Class that can read/modify private values of another classOk, scared me for a second. I thought I was missing some fundamental understanding of C#. I just realized after reading through my code, that by simply writing three more public methods, I can transfer the dealer logic to the table logic - and the logic is better placed in the table anyways. public BetAction[] GetPossibleActions(int Position); // Return an array of possible actions, given the game context, for the specified player. public Update(BetAction Action, int Position); // Update the game context, given the player action at the specified position. public int NextPlayerToAct(); // Return the position of the next player to act, given active players By simply calling GetPossibleActions(NextPlayerToAct()), you'll recieve an array of possible actions. If it's empty - the hand's over and the simulator (or whatever is controlling input to the table) will simply call StartNextRound() (which may call FinalizeHand(), if your on the last hand). If there are possible actions, (I have written public properties for the table which allow viewing, but not modifying table values), whatever selects the decision can view those properties and make a decision, then simply call the Update(SpecifiedAction, CurrentActingPlayer) which will update the game context and the player object. ... In other words, after thinking about this for awhile, I realized I don't need another class with private access. Thanks!
-
Class that can read/modify private values of another classSomething you said, just made me question myself. All these classes are defined in the same namespace: PokerLib, and compiled as a class library. Are you saying that classes defined inside the same namespace CAN access private members of other classes defined in the same namespace?
-
Class that can read/modify private values of another classIs there a way to write a class that can modify private values of another class? In this situation ... I have a table (a black box which contains methods like AddPlayer, RemovePlayer, DealNewHand) ... but I don't want to directly work with the values. I want to call only certain methods that will modify the table exactly how it should be modified. But ... I need to directy be able to read/modify some of those private values, for simulating play. I want to write a class Dealer which will be able directly read/modify some of those values. The dealer, for example, would be responsible for knowing who's turn it is to act (needs access to the private players objects stored in the table) and for updating the corresponding table values. So, is there a way for me to give the Dealer Class access to the private members of the Table class, or am I going to need to make the members of the table class that I want the dealer only to be able to see, public? As always, thanks!
-
Overloading the assignment operatorI can't find any documentation on this anywhere ... is it possible? I have a class: public Player { ... private fields ... public methods } And there are many times I need to simply say this Player equals that Player and have all the values from one copied into the next. I have created a pubic method inside player, called "Copy()" which simply creates a new player from the current players data and returns it. So I can do something like: player1 = player2.Copy() But this is VERY annoying. Can I simply overload the assignment operator so that I can do: player1 = player2 and it will copy all the data from player 2 into player 1, rather than setting player1 equal to the reference of player2?