0% found this document useful (0 votes)
7 views

Q 3

Uploaded by

tegaraenglish97
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Q 3

Uploaded by

tegaraenglish97
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1. Write a program that reads from the console a sequence of positive integer numbers.

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;

// Reading input from the console


while (!string.IsNullOrEmpty(input = Console.ReadLine()))
{
numbers.Add(int.Parse(input));
}

// Calculating sum and average


int sum = 0;
foreach (int number in numbers)
{
sum += number;
}

double average = (numbers.Count > 0) ? (double)sum / numbers.Count : 0;

// Printing sum and average


Console.WriteLine("Sum: " + sum);
Console.WriteLine("Average: " + average);
}
}
2. Write a program that reads a sequence of integers (List<int>) ending with an empty line
and sorts them in an increasing order.

using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
List<int> numbers = new List<int>();
string input;

// Reading input from the console


while (!string.IsNullOrEmpty(input = Console.ReadLine()))
{
numbers.Add(int.Parse(input));
}

// Sorting the list in increasing order


numbers.Sort();

// Printing the sorted numbers


foreach (int number in numbers)
{
Console.WriteLine(number);
}
}
}
3. Write a program that removes from given sequence all negative numbers.

using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
List<int> numbers = new List<int>();
string input;

// Reading input from the console


while (!string.IsNullOrEmpty(input = Console.ReadLine()))
{
numbers.Add(int.Parse(input));
}

// Removing negative numbers


numbers.RemoveAll(number => number < 0);

// Printing the remaining numbers


foreach (int number in numbers)
{
Console.WriteLine(number);
}
}
}
4. Write a program that removes from given sequence all numbers that occur odd number of
times. Example: {4, 2, 2, 5, 2, 3, 2, 3, 1, 5, 2} → {5, 3, 3, 5}

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();

foreach (int number in grouped)


{
Console.WriteLine(number);
}
}
}
```[_{{{CITATION{{{_1{](https://github.com/oggg/TelerikAcademy/tree/277c39cd9feac58a73e
2af9035660419289aa0f0/DSA%2F02.%20Linear-Data-Structures%2F06.%20Task6%2FPro
gram.cs)
5. Write a program that finds in given array of integers (all belonging to the range [0..1000])
how many times each of them occurs.
Example: array = {3, 4, 4, 2, 3, 3, 4, 3, 2}
2→ 2 times
3→ 4 times
4→ 3 times

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>();

foreach (int number in array)


{
if (occurrences.ContainsKey(number))
{
occurrences[number]++;
}
else
{
occurrences[number] = 1;
}
}

foreach (var pair in occurrences)


{
Console.WriteLine($"{pair.Key}→ {pair.Value} times");
}
}
}
6. Implement the data structure linked list. Define a class ListItem<T> that has two fields:
value (of type T) and nextItem (of type ListItem<T>). Define additionally a class
LinkedList<T> with a single field firstElement (of type ListItem<T>).

public class ListItem<T>


{
public T value { get; set; }
public ListItem<T> nextItem { get; set; }
}

public class LinkedList<T>


{
public ListItem<T> firstElement { get; set; }
}
9. Write a program to create a linked list and remove all the duplicate
elements of the list.

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; }

public void Add(T value)


{
ListItem<T> newItem = new ListItem<T> { Value = value };
if (FirstElement == null)
{
FirstElement = newItem;
}
else
{
ListItem<T> current = FirstElement;
while (current.Next != null)
{
current = current.Next;
}
current.Next = newItem;
}
}

public void RemoveDuplicates()


{
HashSet<T> seenValues = new HashSet<T>();
ListItem<T> current = FirstElement;
ListItem<T> previous = null;
while (current != null)
{
if (seenValues.Contains(current.Value))
{
previous.Next = current.Next;
}
else
{
seenValues.Add(current.Value);
previous = current;
}
current = current.Next;
}
}

public void Print()


{
ListItem<T> current = FirstElement;
while (current != null)
{
Console.Write(current.Value + " ");
current = current.Next;
}
Console.WriteLine();
}
}

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();

Console.WriteLine("List after removing duplicates:");


list.Print();
}
}
7. What is a linear linked list?

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.

Advantages over linear linked list:

● Efficient traversal: No need to check for null to determine the end.


● Circular iteration: Simplifies algorithms that need to loop through the list
multiple times.

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.

You might also like