Single Dimensional Array301
Single Dimensional Array301
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int array[5] = {10, 20, 30, 40, 50};
cout << "Array Index 0 = " << array[0];
cout << "\n\nArray Index 1 = " << array[1];
cout << "\n\nArray Index 2 = " << array[2];
cout << "\n\nArray Index 3 = " << array[3];
cout << "\n\nArray Index 4 = " << array[4];
getch();
return 0;
}
if(head == NULL)
{
head = new_node;
}
else
{
node *ptr = head;
if(head == NULL)
{
head = new_ptr;
tail = new_ptr;
new_ptr -> next = head;
}
else
{
tail -> next = new_ptr;
tail = new_ptr;
tail -> next = head;
}
cout<<"\n\n Node is Created Successfully...";
}
void linklist::display()
{
system("cls");
int c=0;
cout<<"\n\n\t\t\t\tDisplay Record";
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
node *ptr = head;
while(c != size)
{
cout<<"\n\n Value : "<<ptr -> data;
ptr = ptr -> next;
c++;
}
}
}
void linklist::search()
{
system("cls");
int c=0,n,found=0;
cout<<"\n\n\t\t\t\tSearch Record";
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
cout<<"\n\n Enter Value : ";
cin>>n;
node *ptr = head;
while(c != size)
{
if(n == ptr -> data)
{
cout<<"\n\n Value "<<n<<" is Found...";
found++;
break;
}
ptr = ptr -> next;
c++;
}
if(found == 0)
cout<<"\n\n Value "<<n<<" can't Found...";
}
}
void linklist::update()
{
system("cls");
int n,c=0,found=0;
cout<<"\n\n\t\t\t\tUpdate Record";
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
cout<<"\n\n Enter Value : ";
cin>>n;
node *ptr = head;
while(c != size)
{
if(n == ptr -> data)
{
cout<<"\n\n New Value : ";
cin>>n;
ptr -> data = n;
found++;
cout<<"\n\n Value Updated Successfully...";
break;
}
ptr = ptr -> next;
c++;
}
if(found == 0)
cout<<"\n\n Value "<<n<<" Can't Found...";
}
}
void linklist::sum_count_avg()
{
system("cls");
int c=0,sum=0;
cout<<"\n\n\t\t\t Sum, Count, Avg Record";
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
node *ptr = head;
while(c != size)
{
sum = sum + ptr -> data;
ptr = ptr -> next;
c++;
}
cout<<"\n\n Number of Nodes : "<<size;
cout<<"\n\n Sum : "<<sum;
cout<<"\n\n Average : "<<sum/size;
}
}
void linklist::del()
{
system("cls");
int n,found=0,c=1;
cout<<"\n\n\t\t\t\tDelete Record";
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
node *ptr = head;
cout<<"\n\n Enter Value : ";
cin>>n;
if(size == 1 && n == head -> data)
{
head = NULL;
tail = NULL;
size--;
cout<<"\n\n Node Deleted Successfully...";
found++;
delete ptr;
}
else if(n == head -> data)
{
head = head -> next;
tail -> next = head;
size--;
cout<<"\n\n Node Deleted Successfully...";
found++;
delete ptr;
}
else if(n == tail -> data)
{
while(ptr -> next != tail)
{
ptr = ptr -> next;
}
node *pre = tail;
tail = ptr;
tail -> next = head;
size--;
cout<<"\n\n Node Deleted Successfully...";
found++;
delete pre;
}
else
{
node *pre_ptr = head;
ptr = head -> next;
while(c != size)
{
if(n == ptr -> data)
{
pre_ptr -> next = ptr -> next;
size--;
cout<<"\n\n Node Deleted Successfully...";
found++;
delete ptr;
break;
}
pre_ptr = ptr;
ptr = ptr -> next;
c++;
}
}
if(found == 0)
cout<<"\n\n Value "<<n<<" Can't Found...";
}
}
main()
{
linklist l;
int choice,n;
p:
system("cls");
cout<<"\n\n\t\t\t\tControl Panel";
cout<<"\n\n 1. Insert Record";
cout<<"\n 2. Display Record";
cout<<"\n 3. Search Record";
cout<<"\n 4. Update Record";
cout<<"\n 5. Sum, Count, Avg Record";
cout<<"\n 6. Delete Record";
cout<<"\n 7. Exit";
cout<<"\n\n Your Choice : ";
cin>>choice;
switch(choice)
{
case 1:
system("cls");
cout<<"\n\n\t\t\t\tInsert Record";
cout<<"\n\n Enter Value : ";
cin>>n;
l.insert(n);
break;
case 2:
l.display();
break;
case 3:
l.search();
break;
case 4:
l.update();
break;
case 5:
l.sum_count_avg();
break;
case 6:
l.del();
break;
case 7:
exit(0);
default:
cout<<"\n\n Invalid Value...Please Try Again...";
}
getch();
goto p;
}
};
class Linked_List
{
private:
Node *Head;
Node *Tail;
public:
Linked_List()
{
Head = NULL;
Tail = NULL;
}
void Enqueue(int value)
{
Node *newNode = new Node;
newNode -> data = value;
newNode -> next_add = NULL;
if(Head == NULL)
{
Head = newNode;
Tail = newNode;
}
else
{
Tail -> next_add = newNode;
Tail = newNode;
}
cout<<"\n\n *** Node "<<value<<" Value Enqueue Successfully ***";
}
void Dequeue()
{
if(Head == NULL)
{
cout<<"\n\n *** Linked List is Empty ***";
}
else
{
Node *ptr = Head;
Head = Head -> next_add;
cout<<"\n\n *** Node "<<ptr -> data<<" Value Dequeue
Successfully ***";
delete ptr;
}
}
void Display()
{
if(Head == NULL)
{
cout<<"\n\n *** Linked List is Empty ***";
}
else
{
cout<<"\n\n Queue Values: ";
Node *ptr = Head;
while(ptr != NULL)
{
cout<<ptr -> data<<" ";
ptr = ptr -> next_add;
}
}
}
~Linked_List()
{}
};
main()
{
Linked_List obj;
obj.Enqueue(5);
obj.Enqueue(2);
obj.Enqueue(12);
obj.Enqueue(1);
obj.Enqueue(14);
obj.Enqueue(18);
obj.Enqueue(22);
obj.Display();
getch();
return 0;
}
};
class Linked_List
{
private:
Node *Head;
Node *Tail;
public:
Linked_List()
{
Head = NULL;
Tail = NULL;
}
void Enqueue(int value)
{
Node *newNode = new Node;
newNode -> data = value;
newNode -> next_add = NULL;
if(Head == NULL)
{
Head = newNode;
Tail = newNode;
}
else
{
Tail -> next_add = newNode;
Tail = newNode;
sort();
}
count++;
cout<<"\n\n *** Node "<<value<<" Value Enqueue Successfully ***";
}
void sort()
{
int temp;
for(int i=1;i<=count;i++)
{
Node *ptr = Head;
for(int j=1;j<=count;j++)
{
if(ptr -> data < ptr -> next_add -> data)
{
temp = ptr -> data;
ptr -> data = ptr -> next_add -> data;
ptr -> next_add -> data = temp;
}
ptr = ptr -> next_add;
}
}
}
void Dequeue()
{
if(Head == NULL)
{
cout<<"\n\n *** Linked List is Empty ***";
}
else
{
Node *ptr = Head;
Head = Head -> next_add;
cout<<"\n\n *** Node "<<ptr -> data<<" Value Dequeue
Successfully ***";
delete ptr;
}
}
void Display()
{
if(Head == NULL)
{
cout<<"\n\n *** Linked List is Empty ***";
}
else
{
cout<<"\n\n Queue Values: ";
Node *ptr = Head;
while(ptr != NULL)
{
cout<<ptr -> data<<" ";
ptr = ptr -> next_add;
}
}
}
~Linked_List()
{}
};
main()
{
Linked_List obj;
obj.Enqueue(7);
obj.Enqueue(12);
obj.Enqueue(3);
obj.Enqueue(17);
obj.Enqueue(10);
obj.Display();
getch();
return 0;
}