0% found this document useful (0 votes)
6 views5 pages

Class

The document discusses linked lists in C++. It includes functions to insert nodes at the beginning and end of the list, delete nodes from the beginning or end, print the list, and update or delete specific nodes based on their value or index. Several functions are demonstrated in a main method that operates on a linked list.

Uploaded by

zarifahmed180
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Class

The document discusses linked lists in C++. It includes functions to insert nodes at the beginning and end of the list, delete nodes from the beginning or end, print the list, and update or delete specific nodes based on their value or index. Several functions are demonstrated in a main method that operates on a linked list.

Uploaded by

zarifahmed180
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

class 1::::::::::::::::::

#include<iostream>
#include<cstdlib>
using namespace std;

struct Node{
int data;
struct Node* next;
};

struct Node* head;

void print()
{
struct Node* temp;
temp=head;
cout<<"The list is : "<<" ";
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
}

void insert_last(int x){


struct Node* temp=(struct Node*)malloc(sizeof(struct Node));
temp->data=x;
temp->next=NULL;
if(head==NULL){
head=temp;
}
else{
struct Node* temp_1;
temp_1=head;
while(temp_1->next!=NULL){
temp_1=temp_1->next;
}
temp_1->next=temp;
}
}

void insert_first(int x){


struct Node* temp=(struct Node*)malloc(sizeof(struct Node));
temp->data=x;
temp->next=head;
head=temp;
}

void Delete_first(int x){


if(x==1){
struct Node* temp=head;
head=temp->next;
free(temp);
return;
}
}

void Delete_last(int x){


if(x==2){
struct Node* temp=head->next;
struct Node* temp_1=head;
while(temp->next!=NULL){
temp_1=temp_1->next;
temp=temp->next;
}
temp_1->next=NULL;
return;
}
}

void update_value(int x,int y){


struct Node* temp;
temp=head;
while(temp!=NULL){
if(temp->data==x){
temp->data=y;
}
//cout<<temp->data<<" ";
temp=temp->next;
}
print();
}

void update_index(int x,int y){


struct Node* temp;
temp=head;
int i;
for(i=1;i<x;i++){
if(temp!=NULL){
temp=temp->next;
}
}
temp->data=y;
print();
}

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

struct node* temp=head;


head=temp->next;
head->prev=NULL;
return;

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

You might also like