Class
Class
#include<iostream>
#include<cstdlib>
using namespace std;
struct Node{
int data;
struct Node* next;
};
void print()
{
struct Node* temp;
temp=head;
cout<<"The list is : "<<" ";
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
}
int main()
{
int i,j,n,p,h,r,a,b;
char ch,ch_1;
head=NULL;
cout<<"How many numbers you want to take : ";
cin>>n;
cout<<"If you want to insert in first position press 'f' "<<"\n";
cout<<"If you want to insert in last position press 'l' "<<"\n ";
cin>>ch;
if(ch=='l'){
for(i=1,p=1;i<=n;i++,p++){
cout<<"\n";
cout<<"Enter the "<<p<<"th number : "<<"\n";
cin>>j;
insert_last(j);
print();
}
}
if(ch=='f'){
for(i=1,p=1;i<=n;i++,p++){
cout<<"\n";
cout<<"Enter the "<<p<<"th number : "<<"\n";
cin>>j;
insert_first(j);
print();
}
}
cout<<"\n"<<"\n";
cout<<"Delete first press 'f' "<<"\n";
cout<<"Delete last press 'l' "<<"\n";
cout<<"\n";
cout<<"Enter anyting to skip"<<"\n";
cin>>ch_1;
if(ch_1=='f'){
Delete_first(1);
print();
}
if(ch_1=='l'){
Delete_last(2);
print();
}
cout<<"\n"<<"\n";
cout<<"Update value : "<<"\n"<<"\n";
cout<<"Enter the number to be checked : ";
cin>>h;
cout<<"Enter the number to be replaced : ";
cin>>r;
update_value(h,r);
cout<<"\n"<<"\n";
cout<<"Update index : "<<"\n"<<"\n";
cout<<"Enter the index to be checked : ";
cin>>a;
cout<<"Enter the number to be replaced : ";
cin>>b;
update_index(a,b);
return 0;
}
...................................................................................
class 2:::::::::::::::::::::::::::::::::::::::
#include <iostream>
#include<cstdlib>
using namespace std;
struct node {
int data;
struct node *prev;
struct node *next;
};
struct node *head;
void print(){
struct node *temp;
temp=head;
cout << "The list is" << endl;
while(temp != NULL){
cout<< temp -> data;
temp=temp->next;
}
}
void insert_first(int x){
struct node * newItem= (struct node *)malloc(sizeof(struct node));
newItem ->data=x;
newItem ->prev=NULL;
if(head!=NULL){
head->prev=newItem;
}
newItem->next=head;
head=newItem;
}
void insert_last(int x){
struct node * newItem= (struct node *)malloc(sizeof(struct node));
newItem ->data=x;
if(head == NULL) {
newItem -> prev = NULL;
newItem -> next = NULL;
newItem=head;
}
else{
struct node *current_last;
current_last=head;
while(current_last->next != NULL){
current_last=current_last->next;
}
current_last->next=newItem;
newItem->prev=current_last;
}
}
void Delete_first(){
}
void Delete_last(){
struct node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp=temp->prev;
temp->next=NULL;
}
void print_rev(){
struct node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
cout<<endl;
cout<<"The reversed list is below!"<<endl<<endl;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->prev;
}
}
void Delete(){
struct node* temp=head;
struct node *del,*pre;
//del=to be deleted, pre=previous node of the deleted, temp=post/next value
//of the deleted value
while(temp!=NULL){
if(temp->data%2==0){
del=temp;
temp=del->next;
pre=del->prev;
pre->next=temp;
temp->prev=pre;
free(del);
}
else{
temp=temp->next;
}
}
}
int main()
{
int x,y,z;char ch;
int n,j;
cout << "Enter how many elements to insert:" << endl;
cin >> n;
for(int i=1;i<=n;i++){
cout<<"\n";
cout<<"Enter the "<<i<<"th number : "<<"\n";
cin >> j;
cout << "If you want to insert first type 'f' else type 'l'"<<endl;
cin >> ch;
if(ch=='f')
insert_first(j);
else insert_last(j);
//print();
}
// cin >> x >>y >>z;
// insert_first(x);
// insert_first(y);
// insert_first(z);
print();
print_rev();
Delete();
print();
return 0;
}