其他分享
首页 > 其他分享> > 线性表——找出单链表值最大的结点(顺序查找)

线性表——找出单链表值最大的结点(顺序查找)

作者:互联网

 

//"LinkList.h"
#include<iostream>
using namespace std;

#define ElemType int

typedef struct LNode{
    ElemType data;
    struct LNode *next;
}LNode,*LinkList;

string InitList(LinkList &L){
    L = new LNode;
    L->next = NULL;
    return "OK";
}

string ListInsert(LinkList &L,int i,ElemType e){
    LinkList P = L;
    int j=0;
    while (P && (j<i-1))
    {       
        P = P->next;++j;    
    }
    LinkList S = new LNode;
    S->data = e;
    S->next=P->next;
    P->next=S;
    return "OK";   
}

ElemType GetElement(LinkList L,int i){
    LinkList p = L;
    int j = 1;
    while(p && j<i){
        p=p->next;
        j++;
    }
    if(!p||j>i){
        return -1;
    }
    return p->data;
}

string DeleteList(LinkList &L,int i){
    LinkList p = L;
    int j = 0;
    while ((p->next)&&(j<i-1))
    {
        p=p->next;
        j++;
    }
    if(!(p->next)||(j>i-1))
        return "ERROR";
    LinkList q = p->next;
    p->next=q->next;
    delete q;
    return "OK";
    
}

string ShowList(LinkList L){
    LinkList p= L;
    while(p->next){
        p=p->next;
        cout<<p->data;
    }
    return "OK";
}

string FillList(LinkList &L){
    
    int p =1;
    char a = '1';
    while (a != '!')
    {
        ElemType b ;
        cin >> b;
        ElemType e = (ElemType)b - 48;
        a = b;
        if(a != '!')
            cout << ListInsert(L,p,b)<<endl;
        p++;
    }
 return "OK";
}
    
string FillListWithNum(LinkList &L,int i){
    
    int e;
    
    for(int j =1;j<=i;j++){
        cin >> e;
        ListInsert(L,j,e);
    }
 return "OK";
}
//     ShowList(L);

// DeleteList(L,2);
// ShowList(L);
//     return 0;
// }

 

 

#include<iostream>
#include"LinkList.h"
using namespace std;

/*
 找出单链表值最大的结点

*/

int main(){

    LinkList LA;

    InitList(LA);

    FillListWithNum(LA,8);

    LNode *pa = LA->next;
    
    LNode *pb = LA->next;


    while(pa){

        if(pb->data<pa->data){
            pb=pa;
        }
        pa=pa->next;

    }
 
    delete LA;

        cout<<endl;
        cout<<pb->data;
        system("pause");
        return 0;
    }

 

标签:结点,return,线性表,表值,int,ElemType,next,LinkList,LNode
来源: https://www.cnblogs.com/LuMinghao/p/13996797.html