Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- :)
- Author: Splux
- */
- using System;
- class node <T>
- {
- //methods
- public node()
- {
- next = null;
- }
- public node(T data)
- {
- next = null;
- _data = data;
- }
- public T getData()
- {
- return _data;
- }
- //variables
- public node<T> next;
- private T _data;
- }
- class list <T>
- {
- //methods
- public list() // just null root
- {
- root = null;
- }
- public list(T data)//create first node ( the root ) with data
- {
- root = new node<T>(data);
- }
- public void add(T data)
- {
- if (root == null)
- {
- root = new node<T>(data);
- return;
- }
- node<T> temp = root;
- while(temp.next != null) // loop to last node and add one more
- {
- temp = temp.next;
- }
- temp.next = new node<T>(data);
- }
- public void delete()
- {
- if (root == null) // no point to go any further...
- return;
- else if(root.next == null) // no point in looping to find the last node
- {
- root = null;
- return;
- }
- else
- {
- node<T> temp = root;
- //loop through the whole list and find the last value
- while (temp.next.next != null) // since I already check for root.next I can go through the whole list and check for temp.next.next
- // without screwing things up
- temp = temp.next;
- temp.next = null; // and then null the last object and let the system clean it up
- }
- }
- public void print_all()
- {
- node<T> temp = root;
- // loop through and print out value of everything in the list that is not null
- while(temp != null)
- {
- Console.WriteLine("{0}", temp.getData());
- temp = temp.next;
- }
- }
- //variables
- private node<T> root;
- }
- class app
- {
- public static void Main()
- {
- list<int> enLista = new list<int>(3);
- enLista.add(4);
- enLista.add(6);
- enLista.print_all();
- enLista.delete();
- enLista.print_all();
- list<char> chlist = new list<char>();
- chlist.add('d');
- chlist.add('r');
- chlist.print_all();
- chlist.delete();
- chlist.print_all();
- chlist.add('e');
- chlist.print_all();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment