![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jul 2005
Posts: 1
Rep Power: 0
![]() |
C# - LinkedList Issues
--------------------------------------------------------------------------------
I encountered a few problems when trying to execute a few operations for LinkedList implementaions using C#-Sharp programming codes. Hereby, i am posting the codes that i have written and hopefully some programmers could provide me with some pointers as to how the problems can be resolve or highlights to me what went wrong or provide with some sample solutions. When i tried to rxecute the operations for btnAddRear and btnAddPosition, there are no effect at all. Hence, i am suspecting the codes have went wrong somewhere in the entire list. It would be very much appreciated that any programmers gurus could highlight to me what actually went wrong. Thanx in advance. Looking forward to the solutions to unravel the mystery puzzles. Below are the main highlights that have yet to be resolve. 1. Delete a node at the end 2. Insert and delete a node at a particular position (if the user enters 5 the node should be inserted at the fifth position in the LinkedList. 3. Insert and delete nodes containing even numbers only. 4. Insert and delete nodes of numbers specified by users. (including min and max numbers). This is the class node for the variables: using System; namespace LinkedList { public class Node { private object data; private Node link; private Node cursor; private int pos; private Node next; private Node prev; public Node (object d, Node l, Node c) { data = d; link = l; cursor = c; } public object Data { get { return data; } set { data = value; } } public Node Link { get { return link; } set { link = value; } } public Node Cursor { get { return cursor; } set { cursor = value; } } } } Formed LinkedList.cs rivate void btnAddFront_Click(object sender, System.EventArgs e) { Node n = new Node(this.tbxData.Text, null, cursor); n.Link = head; if (head == null) // linked list is initially empty { tail = n; } head = n; size++; // size = size + 1 this.lblSize.Text = size.ToString(); this.Visualize(head); } private void btnDeleteFront_Click(object sender, System.EventArgs e) { if(head == null) // the linked list is empty { MessageBox.Show("Linked List is empty!"); return; } head = head.Link; size--; this.lblSize.Text = size.ToString(); this.Visualize(head); } private void btnAddRear_Click(object sender, System.EventArgs e) { Node n = new Node(this.tbxData.Text, null, null); if(head == null)// the LinkedList is empty { head = n; tail = n; } else { // connect the last node to the new node tail.Link = n; // make tail points to the last node tail = n; } size++; // size = size + 1 this.lblSize.Text = size.ToString(); this.Visualize(head); } private void btnDeleteRear_Click(object sender, System.EventArgs e) { if(head == null || index <= 0 || index > size) { return; Node prev = null; Node current = head; for(int i = 0 ; i < index ; i++) { prev = current; current = current.Link; } object temp = current.Data; if(prev != null) { prev.Link = current.Link; } else { head = current.Link; } if(prev.Link == null) { tail = prev; } size--; this.lblSize.Text = size.ToString(); this.Visualize(head); } /**object temp = null; if(tail != null) // the linked list is empty { temp = tail.Data; tail = tail.Link; size--; this.lblSize.Text = size.ToString(); this.Visualize(head); }**/ } private void btnAddPosition_Click(object sender, System.EventArgs e) { cursor = head; for (int i = 1; (i<pos) && (cursor!= null); i++) { cursor = cursor.Link; this.lblPosNum.Text = pos.ToString(); this.Visualize(cursor); } } |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 5
![]() |
post that inside "code" tags
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials. --WilliamSChips on Slashdot |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|