Tuesday, October 10, 2023

Stack using linked list

 Stack using linked list (pranjali)



#include<stdio.h>

#include<malloc.h>

struct node

  {

    int data;

    struct node *link;

  };

 struct node * top= NULL;  //Initially stack is empty

  void push()

   {

       struct node*temp;

       temp=(struct node *)malloc(sizeof(struct node));

       printf("Enter a value");

       scanf("%d",&temp->data);

       temp->link=top;

       top=temp;

   void pop()

    {

    if(top==NULL)

    {

    printf("Stack underflow");

}

else

{

struct node*temp=top;

top=top->link;

printf("poped value=%d",temp->data);

free(temp);

}

}

    void display()

  {

  struct node *temp=top;

  if(top==NULL)

  {

  printf("Empty stack");

   }

  else

   {

 while(temp!=NULL)

    {

    printf("%d",temp->data);

    temp=temp->link;

}

  }

  

int main()

    {

    while(1)

    {

    printf("enter 1 for push\n");

    printf("enter 2 for pop\n");

    printf("enter 3 for display\n");

    printf("enter 4 for exit");

   

int choice;

    scanf("%d",&choice);

    switch(choice)

    {

    case 1:

    push();

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

printf("end of the program");

break;

default:

printf("wrong choice\n"); 

  }

  if (choice==4)

  break;

}

}

sparse matrix representation in 2d array

sparse matrix representation in 2d array(arik mukherjee)



 # include<iostream>

using namespace std;


int main()

{

int a[10][10],row,col,count=0,i,j,k=1;

int s[10][3];

cout<<"total number of row";

cout<<"total number of column";

cin>>row;

cin>>col;

cout<<"enter value";

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

{

for(j=0;j<col;j++)

{

cin>>a[i][j];

}

}

cout<<"matrix element\n";

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

{

for(j=0;j<col;j++)

{

cout<<a[i][j]<<" ";

}

cout<<"\n";

}

//count total number of nonzero elements

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

{

for(j=0;j<col;j++)

{

if(a[i][j]!=0)

{

count++;

}

}

}

cout<<count;

s[0][0]=row;

s[0][1]=col;

s[0][2]=count;

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

{

for(j=0;j<col;j++)

{

if(a[i][j]!=0)

{

s[k][0]=i;

s[k][1]=j;

s[k][2]=a[i][j];

k++;

}

}

}

cout<<"\n output array \n";

for(i=0;i<count+1;i++)

{

for(j=0;j<3;j++)

{

  cout<<s[i][j]<<" ";

}

cout<<"\n";

}

}