在SLL中查找负值

时间:2017-07-30 18:50:17

标签: c++

我需要遍历单个链接列表并找到否定节点,删除它们并返回删除节点的数量。

这是我到目前为止的代码,但我总是从函数

返回counter = 1

for循环和if语句中是否有任何错误,或者是其他内容

bool IntSLList::DeleteNegativeNodes()
{
int counter=0;
if(IsEmpty() == true)
    counter++;

if(head->val<0)
{
    DeleteFromHead();
    counter++;
}

if(tail->val<0)
{
    DeleteFromTail();
    counter++;
}


IntSLLNode *node, *pred, *tmp;
node = pred = NULL;


for(pred = head, node = head->next; 
    node != NULL; 
    node = node->next, pred = pred->next)
{
    if(node->val<0)
    {
        node->flag = 1;

    }
}
for(tmp = head; tmp != NULL; tmp = tmp->next)
{
    if(tmp->flag==1)
    counter++;
    delete tmp;


}
return counter;
}

int main()
{
int n,x,z;
IntSLList list1;


cout <<"Insert number of nodes u'd like inserted in list" << endl;
cin >> n;

for(int i=0;i<n;i++)
{
    cin >> x;
    list1.AddToTail(x);
}
z=list1.DeleteNegativeNodes();

cout << "Number of negative deletes nodes is : " << z << endl;

 }

2 个答案:

答案 0 :(得分:1)

问题在于返回值的类型。检查方法的签名:

<ion-content class="dynamicModal">
  <!--<button outline (click)="close()">Close</button>-->
  <ion-list>
    <div *ngFor="let propertyInfo of modalOptions.inputMetadata">
      <inline-edit [(ngModel)]="modalOptions.entity && modalOptions.entity[propertyInfo.propertyName]"
                   [label]="propertyInfo.label" [editOptions]="modalOptions.editOptions"
                   [required]="propertyInfo.required" [additionalData]="propertyInfo.additionalData"
                   [type]="propertyInfo.inputType" [componentType]="propertyInfo.componentType">
      </inline-edit>
    </div>
    <ion-list-header>
      <button ion-button full color="secondary">
        Save<ion-icon name="add"></ion-icon>
      </button>
    </ion-list-header>
  </ion-list>
</ion-content>

返回类型为 $(document).ready(function(){ $("#trendingnexticon").on('click' ,function(){ $("#trendingtable").animate({right: '800px'}); }); }); $(document).ready(function(){ $("#trendingpreviousicon").on('click' ,function(){ $("#trendingtable").animate({left: '100px'}); }); }); 。当您从方法返回<div id="trendingdiv" class="" style="overflow-x:scroll; overflow: hidden;"> <table id="trendingtable" class="table" style="position:relative;"> <a id="trendingpreviousicon" style="cursor:pointer; margin-top: 62px; position:absolute; z-index:1;" class="previous round">&#8249;</a> <a id="trendingnexticon" style="cursor:pointer; margin-left: 1250px; margin-top: 62px; position:absolute; z-index:1;" class="next round">&#8250;</a> <tr> <?php while ($tsingers = mysqli_fetch_assoc($tsingersquery)): ?> <td> <div class="media" style="border: 1px solid #e6e6e6; width:400px; padding: 0px;"> <img src="images/<?=$tsingers['image'];?>" alt="<? =$tsingers['name'];?>" class="pull-left img-responsive" style="width: 200px; height: 150px;" id="artistimg"> <div id="trendingmediabody" class="media-body"> <p id="trendingname" style="font-size:14px; font-weight: bolder;"><?=$tsingers['name'];?></p> <p id="trendingcategory" style="font-size:12px; font-weight: bolder;"><?=$tsingers['category'];?></p></br></br> </div> </div> </td> <?php endwhile; ?> </tr> </table> </div> 类型bool IntSLList::DeleteNegativeNodes() 时,implicitly convertedbool。零值变为counter。所有其他值都变为int

在来电方面:

bool

false值隐式转换为true。因此,你得到z=list1.DeleteNegativeNodes();

bool的返回类型更改为int以解决问题。

答案 1 :(得分:0)

在你的第二个

if(tmp->flag==1)

我认为你必须使用

if(node->flag==1)