Q2) Problem Statement:Write a Program to perform the basic operations like insert,delete,display and search in list. List contains String object items where these operations are to be performed.Sample Input and Output 1 :1. Insert2. Search3. Delete4. Display5. ExitEnter your choice :1Enter the item to be inserted:BottleInserted successfully
ExitEnter your choice :1Enter the item to beinserted:WaterInserted successfully1. Insert2. Search3. Delete4. Display5. ExitEnter your choice :1Enter the item to beinserted:CapInserted successfullyEnter your choice :1Enter the item to beinserted:MonitorInserted successfully1. : 2Enter the item to search : MouseItem not found in the list.Enter your choice : 2Enter the item to search : MonitorItem found in the list.. Exit : 3Enter the item to delete : Mouse : 4The Items in the list are : BottleWater Cap Monitor : 3Enter the item to delete : CapDeleted successfullyEnter your choice : 4The Items in the list are : BottleWater Monitor
using System;
using System.Collections.Generic;
namespace App
{
class Program
{
static int search(List<string> items, string name)
{
for (int i = 0; i < items.Count; i++)
{
if (items[i] == name)
{
return i;
}
}
return -1;
}
static void Main(string[] args)
{
string name;
int index;
int ch = -1;
List<string> items = new List<string>();
while (ch != 5)
{
Console.WriteLine("1. Insert");
Console.WriteLine("2. Search");
Console.WriteLine("3. Delete");
Console.WriteLine("4. Display");
Console.WriteLine("5. Exit");
Console.Write("Enter your choice: ");
ch = int.Parse(Console.ReadLine());
switch (ch)
{
case 1:
Console.Write("Enter the item to be inserted: ");
name = Console.ReadLine();
items.Add(name);
Console.WriteLine("Inserted successfully");
break;
case 2:
Console.Write("Enter the item to search: ");
name = Console.ReadLine();
index = search(items, name);
if (index == -1)
{
Console.WriteLine("Item not found in the list.");
}
else
{
Console.WriteLine("Item found in the list...");
}
break;
case 3:
Console.Write("Enter the item to delete: ");
name = Console.ReadLine();
index = search(items, name);
if (index == -1)
{
Console.WriteLine("Item not found in the list.");
}
else
{
items.RemoveAt(index);
Console.WriteLine("Deleted successfully.");
}
break;
case 4:
Console.WriteLine("The Items in the list are: ");
for (int i = 0; i < items.Count; i++)
{
Console.WriteLine(items[i]);
}
break;
case 5:
//exit
break;
}
}
}
}
}
Comments
Leave a comment