Q 3
Q 3
The
sequence ends when empty line is entered. Calculate and print the sum and average of the
elements of the sequence. Keep the sequence in List<int>.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> numbers = new List<int>();
string input;
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> numbers = new List<int>();
string input;
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> numbers = new List<int>();
string input;
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 4, 2, 2, 5, 2, 3, 2, 3, 1, 5, 2 };
var grouped = numbers.GroupBy(n => n).Where(g => g.Count() % 2 ==
0).SelectMany(g => g).ToList();
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
int[] array = { 3, 4, 4, 2, 3, 3, 4, 3, 2 };
Dictionary<int, int> occurrences = new Dictionary<int, int>();
using System;
using System.Collections.Generic;
class ListItem<T>
{
public T Value { get; set; }
public ListItem<T> Next { get; set; }
}
class LinkedList<T>
{
public ListItem<T> FirstElement { get; set; }
class Program
{
static void Main()
{
LinkedList<int> list = new LinkedList<int>();
list.Add(1);
list.Add(2);
list.Add(2);
list.Add(3);
list.Add(3);
list.Add(4);
Console.WriteLine("Original List:");
list.Print();
list.RemoveDuplicates();
A linear linked list is a sequence of nodes where each node points to the next
node in the sequence. The last node points to null, indicating the end of the
list.
8. What is the need for a linked list? On what account, linked lists are better
than arrays?
Linked lists are dynamic in nature and allocate memory as needed. They allow
easy insertion and deletion of elements, utilize memory effectively, and can
implement other data structures like stacks and queues.
10 .What is a circular linked list? What are its advantages over linear linked
list? position in a circular linked list.
A circular linked list is a type of linked list where the last node points back to
the first node, forming a circle.
Position in a circular linked list is indicated by the node pointers, and traversal
continues from the end back to the head seamlessly.
What are the advantages of doubly linked list over singly linked list?
Doubly linked lists allow traversal in both directions, making it easier to visit a
node's predecessor. This facilitates efficient backward traversal and easier
manipulation of nodes compared to singly linked lists.