answersLogoWhite

0


Best Answer

class Node

002

{

003

private:

004

int data;

005

006

public:

007

friend class List;

008

friend class stack;

009

010

Node* next;

011

012

Node(){data = 0;}

013

014

Node(int x)

015

{

016

next = 0;

017

setdata(x);

018

}

019

020

int getdata()

021

{

022

return data;

023

}

024

025

void setdata(int x)

026

{

027

data = x;

028

}

029

030

void print()

031

{

032

cout<<data;

033

}

034

035

~Node(){}

036

};

037

038

class List

039

{

040

private:

041

Node* Head;

042

Node* Tail;

043

044

public:

045

046

friend class stack;

047

List(){Head = 0; Tail = 0;}

048

049

void addnode(int x)

050

{

051

Node *current = new Node(x);

052

053

if(Head 0)

197

{

198

return true;

199

}

200

else

201

{

202

return false;

203

}

204

}

205

206

Node* topEl()

207

{

208

return (L.top());

209

}

210

211

void printstack()

212

{

213

L.printlist();

214

}

215

216

~stack()

217

{

218

L.deletelist();

219

}

220

221

};

222

223

int main()

224

{

225

stack operand1;

226

stack operand2;

227

stack carry;

228

stack result;

229

int tempsum;

230

int a,b,c;

231

int carry1;

232

Node *n1 = NULL;

233

Node *n2 = NULL;

234

Node *n3 = NULL;

235

236

operand1.push(3);

237

operand1.push(8);

238

operand1.push(0);

239

operand2.push(5);

240

operand2.push(3);

241

operand2.push(5);

242

243

244

245

while(!(operand1.isEmpty()) && !(operand2.isEmpty()))

246

{

247

n1 = operand1.topEl();

248

a = n1->getdata();

249

n2 = operand2.topEl();

250

b = n2->getdata();

251

tempsum = a + b;

252

253

if(tempsum > 9)

254

{

255

c = 0;

256

if(!(carry.isEmpty()))

257

{

258

n3 = carry.topEl();

259

c = n3->getdata();

260

carry.pop();

261

}

262

carry1 = tempsum / 10;

263

tempsum = tempsum % 10;

264

carry.push(carry1);

265

result.push(tempsum + c);

266

}

267

else

268

{

269

if(!(carry.isEmpty()))

270

{

271

n3 = carry.topEl();

272

c = n3->getdata();

273

result.push(tempsum + c);

274

carry.pop();

275

}

276

else

277

{

278

result.push(tempsum);

279

}

280

}

281

operand1.pop();

282

operand2.pop();

283

}

284

285

n1 = operand1.topEl();

286

a = n1->getdata();

287

n2 = operand2.topEl();

288

b = n2->getdata();

289

tempsum = a + b;

290

if(carry.isEmpty())

291

{

292

result.push(tempsum);

293

}

294

else

295

{

296

n3 = carry.topEl();

297

c = n3->getdata();

298

result.push(tempsum + c);

299

carry.pop();

300

}

301

operand1.pop();

302

operand2.pop();

303

304

result.printstack();

305

306

system("pause");

307

return 0;

308

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<conio.h>

struct node

{

int data ;

node *next;

node *prev ;

} ;

node* create_node(int x)

{

node *new1 ;

new1=new node ;

new1->data=x ;

new1->next=NULL ;

new1->prev=NULL;

return(new1);

}

void insert(node *&list , int x)

{

node *new1;

new1=create_node(x);

node *temp ;

temp=list ;

if(list==NULL)

list=new1;

else

{

while(temp->next != NULL)

temp=temp->next ;

temp->next=new1;

new1->prev=temp ;

}

}

void insert_r(node *&list , int x)

{

node *new1;

new1=create_node(x);

if(list==NULL)

list==new1 ;

else

{

list->prev=new1 ;

new1->next=list ;

list=new1 ;

}

}

void display(node *list)

{

node *temp ;

temp=list ;

while(temp != NULL)

{

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

temp=temp->next ;

}

}

void adding(node *p1,node *p2,node *&p3)

{

int x,y ;

int car=0;

node *t1=p1 ;

node *t2=p2 ;

while(t1->next != NULL)

t1=t1->next;

while(t2->next != NULL)

t2=t2->next;

for(;t1!=NULL&&t2!=NULL;t1=t1->prev,t2=t2->prev)

{

x=t1->data+t2->data+car ;

y=x%10;

if(y == 0)

car=1;

else

car=0;

if(x>10)

car=1;

insert_r(p3,y);

}

if(car!=0)

insert_r(p3,car);

}

int main()

{

node *p1=NULL;

node *p2=NULL;

node *p3=NULL;

char s1[100];

char s2[100];

char ch[2];

gets(s1);

gets(s2);

int s,i,j;

if(strlen(s1)>=strlen(s2))

s=strlen(s1);

else

s=strlen(s2);

for( i=0;s1[i]!='\0';i++)

{

ch[0]=s1[i];

ch[1]='\0';

insert(p1,atoi(ch));

}

for( j=0;s2[j]!='\0';j++)

{

ch[0]=s2[j];

ch[1]='\0';

insert(p2,atoi(ch));

}

if(i!=j)

if(j>i)

for(int k=i;k<j;k++)

insert_r(p1,0);

else

for(int k=j;k<i;k++)

insert_r(p2,0);

printf("\n first one");

display(p1);

printf("\n secound one");

display(p2);

adding(p1,p2,p3);

printf("\nresult one");

display(p3);

getchar();

return 0 ;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write A program to add and multiply two very large numbers using doubly linked list?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Does each node in a doubly linked list contain a link to the previous as well as the next node?

Yes, each node in a doubly linked list contain a link to the previous as well as the next node. That is the definition of the doubly linked list.


Convert single linked list to double linked list?

You copy a singly linked list into a doubly linked list by iterating over the singly linked list and, for each element, calling the doubly linked list insert function.


what are the differences between singly link list and doubly link list?

singly linked list stores only the address of next node while doubly linked list stores the address of previous node and next node and hence it is called doubly linked list. In singly linked list only forward traversing is possible while in doubly linked list forward and backward traversal is possible.


What is the difference between doubly linked list and circular linked list?

A doubly linked list allows traversal in both directions (forward and backward) by having each node point to both its next and previous nodes. A circular linked list is a type of linked list where the last node points back to the first node, forming a circular structure. This allows continuous traversal through the elements without a definitive end.


C program to implement deque using circular linked list?

You'll need to use a doubly-linked circular list, since otherwise when you pop off the tail element you'll need to whizz all the way round the list to find its predecessor. See the links section for an implementation of a doubly-linked circular list.


Is there a c program to multiply two polynomials using linked lists?

yes


Write a algorithm for doubly linked list in c?

sorry


Which operation is perform more efficiently by doubly linked list than by single linked list?

zsd


What is meant by doubly linked list?

In C programming, a double linked-list refers to a linked data structure that contains a set of links that have been linked sequentially.


What are the the two ways in representing the stack?

one-dimensional array, and doubly-linked list.


How do you write a Java program to implement weighted queue using circular doubly linked list?

Add weights to the elements of the queue and use an algorithm to sort the queue every time an element is added.


What is difference between linked list and singly linked list?

Answersingly linked list has the node inserted only at one end. and the pointer corresponds to the next pointer.but in a doubly linked list, the node pointer points to the both previous and the next node.singly linked list has two nodesdoubly linked list has three nodesA doubly linked list makes sense when you need to traverse the list in both directions. You aren't able to do that with a singly linked list.