Professional Documents
Culture Documents
Counter array:
#include<iostream>
using namespace std;
int main () {
int a[3][3];
int counter = 0;
for (int i=0; i<3; i++){
for(int j=0; j<3; j++) {
counter = counter+1;
a[i][j] = counter;
}
}
}
}
for (int i=0; i<=2; i++){
for(int j=0; j<=2; j++) {
cout<<"Enter elements in the matrix 2 at index "<<i<<"
"<<j<<endl; cin>>b[i][j];
}
}
for (int i=0; i<=2; i++){
for(int j=0; j<=2; j++) {
mul[i][j] = a[i][j] * b[i][j];
cout<<mul[i][j]<<" ";
}
}
return 0;
}
Linked list:
#include<iostream>
#include<string>
using namespace std;
struct node{
string data;
node *next;
node(string value):data(value),next(NULL){ }
};
class linkedlist {
private:
node* head;
public:
linkedlist():head(NULL){
}
void append(string value){
node* newnode = new node(value);
if(!head){
head=newnode;
}
else{
node* current = head;
while(current->next){
current = current->next;
}
current->next=newnode;
}
}
void display(){
node*current=head;
while (current!=NULL){
cout<<current->data<<"->";
current = current->next;
}
cout<<"\nNULL";
}
}
if(!current){
return;
}
newnode->next = current->next;
current->next = newnode;
}
void deletenode(string value) {
node* current = head;
node* prev = NULL;
current = current->next;
}
if (current) {
if(prev) {
prev->next = current->next;
}
else {
head = current->next;
}
delete current;
}
}
};
int main () {
linkedlist list;
list.append("muntaha");
list.append("rida");
list.append("laraib");
cout<<"Linked List: "<<endl;
list.display();
cout<<endl;
list.insertatend("aila");
cout<<"Linked list after inserting at the end: "<<endl;
list.display();
cout<<endl;
list.insertatposition("kashaf",3);
cout<<"Linked list after inserting at the position 2: "<<endl;
list.display();
cout<<endl;
list.deletenode("kashaf");
cout<<"Linked list after deleting node with value 2: "<<endl;
list.display();
cout<<endl;
return 0;
}
Output:
Linked list:
#include<iostream>
using namespace std;
struct node{
int data;
node *next;
node(int value):data(value),next(NULL){ }
};
class linkedlist {
private:
node* head;
public:
linkedlist():head(NULL){
}
void append(int value){
node* newnode = new node(value);
if(!head){
head=newnode;
}
else{
node* current = head;
while(current->next){
current = current->next;
}
current->next=newnode;
}
}
void display(){
node*current=head;
while (current!=NULL){
cout<<current->data<<"->";
current = current->next;
}
cout<<"\nNULL";
}
if (position==0) {
newnode->next=head;
head=newnode;
return;
}
node* current = head;
for (int i= 0; i<position - 1 && current; ++i) { current
= current->next;
}
if(!current){
return;
}
newnode->next = current->next;
current->next = newnode;
}
int main () {
linkedlist l;
l.append(1);
l.append(2);
l.append(3);
cout<<"Linked List: "<<endl;
l.display();
cout<<endl;
l.insertatbeginning(0);
cout<<"Linked list after inserting at the beginning: "<<endl;
l.display();
cout<<endl;
l.insertatposition(5,2);
cout<<"Linked list after inserting at the position 2: "<<endl;
l.display();
cout<<endl;
l.deletenode(2);
cout<<"Linked list after deleting node with value 2: "<<endl;
l.display();
cout<<endl;
return 0;
}
Output 1:
Double linked list:
#include<iostream>
using namespace std;
class node {
public:
int data;
node* prev;
node* next;
class doublylinkedlist {
private:
node* head;
node* tail;
public:
doublylinkedlist () {
head = NULL;
tail = NULL;
}
void append (int value) {
node* newnode = new node(value); if (head
== NULL) {
head = newnode;
tail = newnode;
}
else {
tail -> next = newnode;
newnode -> prev = tail;
tail = newnode;
}
}
void display () {
node* current = head;
while (current != NULL) {
cout<<current -> data<<" ";
current = current -> next;
}
cout<<endl;
}
void displayreverse () {
node* current = tail;
while (current != NULL) {
cout<<current -> data<<" ";
current = current -> prev;
}
cout<<endl;
}
};
cout<<"Forward: ";
dll.display();
cout<<"Reverse: ";
dll.displayreverse();
return 0;
}
Output:
Stack:
#include<iostream>
using namespace std;
const int MAX_SIZE = 100;
class stack {
private:
int arr[MAX_SIZE];
int top;
public:
stack () {
top = -1;
}
bool isEmpty() {
return top == -1;
}
bool isFull() {
return top == MAX_SIZE - 1;
}
int pop() {
if(isEmpty()) {
cout<<"Stack Underflow!" <<endl;
return -1;
}
return arr[top--];
}
int peek() {
if(isEmpty()) {
cout<<"Stack is Empty!" <<endl;
return -1;
}
return arr[top];
}
};
int main() {
stack st;
st.push(2);
st.push(4);
st.push(6);
Output:
Stack with double linked list:
#include<iostream>
using namespace std;
class node {
public:
int data;
node* next;
node(int value) {
data = value;
next = NULL;
}
};
class stack {
private:
node* top;
public:
stack() {
top = NULL;
}
bool isEmpty() {
return top == NULL;
}
int pop() {
if (isEmpty()) {
cout<<"Stack Underflow!"<<endl;
return -1;
}
node* temp = top;
int poppedvalue = temp -> data;
top = top->next;
delete temp;
return poppedvalue;
int peek() {
if (isEmpty()){
cout<<"Stack is empty!"<<endl;
return -1;
}
return top->data;
}
};
int main () {
stack st;
st.push(1);
st.push(3);
st.push(5);
cout<<"Top Element: "<<st.peek() <<endl;
Output: