0% found this document useful (0 votes)
56 views16 pages

Name-Rahul Anand REG. NO-18BEC1161: 1) #Include #Include Using Namespace STD

The document contains 5 code snippets implementing different data structures and algorithms using linked lists in C++. The first snippet implements a linked list to store and print characters of a string. The second merges two sorted linked lists. The third removes a given node from a linked list. The fourth implements a queue using a linked list. The fifth implements a stack to concatenate two strings.

Uploaded by

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

Name-Rahul Anand REG. NO-18BEC1161: 1) #Include #Include Using Namespace STD

The document contains 5 code snippets implementing different data structures and algorithms using linked lists in C++. The first snippet implements a linked list to store and print characters of a string. The second merges two sorted linked lists. The third removes a given node from a linked list. The fourth implements a queue using a linked list. The fifth implements a stack to concatenate two strings.

Uploaded by

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

NAME-RAHUL ANAND

REG. NO-18BEC1161

1)

#include<iostream>

#include<string>

using namespace std;

class node

public:

char data;

node *next;

};

void push(node **head,char d)

node *n=new node();

if((*head)==NULL)

n->data=d;

n->next=NULL;

*head=n;

return;

}
n->data=d;

n->next=*head;

*head=n;

void print(node *head)

while(head)

cout<<(head)->data<<" ";

head=(head)->next;

int main()

string s;

node *head=NULL;

cin>>s;

for(int i=0;i<s.length();i++)

push(&head,s[i]);

}
print(head);

2)

#include<iostream>

using namespace std;

class node

public:

int data;

node *next;

};

void insert(node **head,int x)

node *temp=new node();

temp->data=x;

temp->next=*head;

*head=temp;
}

node *merge(node **head1,node **head2)

node *head=NULL;

node *temp1=*head1;

node *temp2=*head2;

while(temp1 && temp2)

if(temp1->data>temp2->data)

insert(&head,temp1->data);

temp1=temp1->next;

else if(temp1->data<temp2->data)

insert(&head,temp2->data);

temp2=temp2->next;

else

insert(&head,temp2->data);

insert(&head,temp1->data);

temp1=temp1->next;

temp2=temp2->next;

}
}

if(temp1)

insert(&head,temp1->data);

else if(temp2)

insert(&head,temp2->data);

return head;

int main()

int n;

cin>>n;

node* head1=NULL;

node* head2=NULL;

while(n--)

int x;

cin>>x;

insert(&head1,x);

int o;

cin>>o;
while(o--)

int x;

cin>>x;

insert(&head2,x);

node *head=NULL;

head=merge(&head1,&head2);

cout<<"new list"<<endl;

while(head)

cout<<head->data<<endl;

head=head->next;

}
3)

#include<iostream>

using namespace std;

class node

public:

int data;

node *next;
};

void *pres(node **head,int x)

node *temp=*head;

node *pre=*head;

while(temp)

if(temp->data==x)

pre->next=temp->next;

free(temp);

break;

pre=temp;

temp=temp->next;

void insert(node **head,int x)

node *temp=new node();

temp->data=x;

temp->next=*head;

*head=temp;

int main()
{

int a,n;

cin>>n;

node* head=NULL;

while(n--)

int x;

cin>>x;

insert(&head,x);

//reverse(&head);

cin>>a;

while(a--)

int x;

cin>>x;

pres(&head,x);

insert(&head,x);

node *temp=head;

while(temp)

cout<<temp->data<<endl;

temp=temp->next;
}

4)

#include<iostream>

#include<string>

using namespace std;

class node

public:

int data;

node *next;

};
void enqueue(node **head,node **last,int d)

node *n=new node();

n->data=d;

n->next=NULL;

if(*head==NULL)

*head=n;

*last=n;

else

(*last)->next=n;

*last=n;

int dequeue(node **head)

int p=((*head)->data);

*head=(*head)->next;

return (p);

}
void print(node **head)

int p=((*head)->data);

cout<<p<<" ";

*head=(*head)->next;

int main()

node *head=NULL;

node *last=head;

int i,j,n,k;

cin>>n;

for(i=0;i<n;i++)

cin>>k;

enqueue(&head,&last,k);

while(head)

int p= dequeue(&head);
if(p%2!=0)

cout<<p<" ";

break;

while(head)

print(&head);

5)

#include<iostream>

#include<string>

using namespace std;


class node

public:

char data;

node *next;

};

void push(node **head ,char d)

node *n=new node();

n->data=d;

n->next=*head;

*head=n;

cout<<d<<" pushed in to the stack\n";

char pop(node **head)

cout<<(*head)->data<<" poped from the stack\n";

char p=(*head)->data;

*head=(*head)->next;

return (p);

int main()
{

node *head=NULL;

string s1,s2;

cin>>s1;

int i;

for(i=0;i<s1.length();i++)

push(&head,s1[i]);

cin>>s2;

cout<<"\n";

for(i=0;i<s2.length();i++)

push(&head,s2[i]);

string s3="";

cout<<"\n";

while(head)

s3=s3+pop(&head);

cout<<"The resultant string is: "<<s3;


}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy