Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Implementation Of Linked Lists Using Collections Name Space In C#
WhatsApp
Vijayaragavan S
Aug 24
2016
802
0
0
using
System;
using
System.Collections.Generic;
namespace
LinkList {
class
Program {
static
LinkedList <
int
> ll =
new
LinkedList <
int
> ();
static
LinkedListNode <
int
> node;
static
void
Main(
string
[] args) {
Console.WriteLine(
" LINKED LIST DEMO"
);
int
ch, x;
Console.WriteLine(
"Linked List Operations"
);
Console.WriteLine(
"1.AddFirst\n2.AddLast\n3.RemoveFirst\n4.RemoveLast\n5.Remove Specified\n6.Display\n7..Exit"
);
while
(
true
) {
Console.Write(
"Enter your Choice : "
);
ch = Convert.ToInt16(Console.ReadLine());
switch
(ch) {
case
1:
Console.Write(
"Enter the Element to AddFirst : "
);
x = Convert.ToInt16(Console.ReadLine());
ll.AddFirst(x);
display();
break
;
case
2:
Console.WriteLine(
"Enter the Element to AddLast : "
);
x = Convert.ToInt16(Console.ReadLine());
ll.AddLast(x);
display();
break
;
case
3:
if
(ll.Count == 0) {
Console.WriteLine(
"Nothing to Delete...!!!"
);
break
;
}
else
{
Console.WriteLine(
"First Element Removed Successfully"
);
ll.RemoveFirst();
display();
break
;
}
case
4:
if
(ll.Count == 0) {
Console.WriteLine(
"Nothing to Delete...!!!"
);
break
;
}
else
{
Console.WriteLine(
"Last Element Removed Successfully"
);
ll.RemoveLast();
display();
break
;
}
case
5:
if
(ll.Count == 0) {
Console.WriteLine(
"Nothing to Delete...!!!"
);
break
;
}
else
{
Console.WriteLine(
"Enter the Element to Remove"
);
x = Convert.ToInt16(Console.ReadLine());
bool
b = ll.Remove(x);
if
(b ==
true
) {
Console.WriteLine(
"Element Removed Successfully"
);
display();
break
;
}
else
{
Console.WriteLine(
"Specified Node Does not Exist"
);
break
;
}
}
case
6:
display();
break
;
default
:
Environment.Exit(0);
break
;
}
}
}
public
static
void
display() {
if
(ll.Count == 0) {
Console.WriteLine(
"Nothing to Display...!!!"
);
}
else
{
Console.Write(
"Elements in the List are:"
);
for
(node = ll.First; node !=
null
; node = node.Next) Console.Write(node.Value +
" "
);
Console.WriteLine();
}
}
}
}
Implementation Of Linked Lists
Collections Name Space
C#
Up Next
Implementation Of Linked Lists Using Collections Name Space In C#