Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main(string[] args)
- {
- // my first linkedlist
- LinkedList MyList = new LinkedList();
- MyList.InsertFirst(100);
- MyList.InsertFirst(55);
- MyList.InsertFirst(75);
- MyList.DisplayList();
- Console.WriteLine("Insert last ");
- MyList.InsertLast(800);
- MyList.DisplayList();
- Console.WriteLine(" to delete first one ");
- MyList.DeleteFirst();
- MyList.DisplayList();
- Console.ReadLine();
- }
- }
- public class LinkedList
- {
- private Node First;
- public bool IsEmpty()
- {
- return (First==null);
- }
- public void InsertFirst(int data)
- {
- Node newnode = new Node();
- newnode.data = data;
- newnode.next = First;
- First = newnode;
- }
- public Node DeleteFirst()
- {
- Node temp = First;
- First = First.next;
- return temp;
- }
- public void DisplayList()
- {
- Console.WriteLine("from first to last");
- Node current = First;
- while (current != null)
- {
- current.DisplayNode();
- current = current.next;
- }
- Console.WriteLine();
- }
- public void InsertLast(int data)
- {
- Node current = First;
- while (current.next != null)
- {
- current = current.next;
- }
- Node newnode = new Node();
- newnode.data = data;
- current.next = newnode;
- }
- }
- public class Node
- {
- public int data;
- public Node next;
- public void DisplayNode()
- {
- Console.WriteLine("<" + data + " >");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement