Help me write funcion deletenumber
I Tried but still not done !
hope you help me...Thanks you very much
Ex: 00042312 ->> delete the first zero 42312
0023014 ->> 23014
My code here
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class List {
private:
int count;
Node* pHead;
public:
List() ;
void addFirst(int) ;
void addLast(int);
void display();
int empty();
void removeFirst();
void clear();
int size();
List* deletethefirstzero(List*L); //HELP ME WRITE CODE HERE . THANKS YOU VERY MUCH
~List();
};
List::List() {
pHead = NULL;
count = 0;
}
void List::addFirst(int newdata) {
Node* pTemp = new Node;
pTemp->data = newdata;
pTemp->next = pHead;
pHead = pTemp;
count++;
}
void List::addLast(int newdata){
Node *pTail = pHead;
if(pHead == NULL){
addFirst(newdata);
}
else{
while(pTail->next != NULL){
pTail = pTail->next;
}
Node *pTemp = new Node();
pTemp->data = newdata;
pTemp->next = NULL;
pTail->next = pTemp;
// pTail = pTemp;
count ++;
}
}
void List::clear()
{
while (! empty()) removeFirst();
return;
}
List::~List() {
Node* pTemp = pHead;
while (pTemp!=NULL) {
pTemp = pTemp->next;
delete pHead;
pHead = pTemp;
}
}
void List::display() {
Node* pTemp = pHead;
while (pTemp!=NULL)
{
cout << pTemp->data<<", ";
pTemp = pTemp->next;
}
cout<<endl;
}
int List::empty()
{
return (pHead==NULL);
}
void List::removeFirst()
{
if (pHead!=NULL)
{
Node* pTemp = pHead;
pHead = pHead->next;
delete pTemp;
count --;
}
return;
}
int List::size()
{
return count;
}
List* deletethefirstzero(List*L){
//HELP ME WRITE CODE HERE . THANKS YOU VERY MUCH
}
int main()
{
List *aList = new List();
aList->addFirst(1);
aList->addFirst(2);
aList->addFirst(0);
aList->addFirst(4);
aList->addFirst(0);
aList->addFirst(0);
aList->addFirst(0);
aList->display();
return 0;
}
|
At first you declare function
deletethefirstzero
as a member of List but you implement it as a global function.
Here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
class List
{
// ...
void deletethefirstzero();
// ...
};
// ...
void List::deletethefirstzero(){
if(pHead!=NULL)
{
while(pHead->data==0)
{
removeFirst();
}
}
}
|
Last edited on
Topic archived. No new replies allowed.