求链表最大节点函数
int Findbig(LinkList la) { struct LNode * pa , * pb , * pc , *u;//三个结点指针 int count = 0; pa = (struct LNode*)malloc(sizeof(struct LNode)); pa = la->next; pc = la; //使用la的头结点作为lc的头节点 while (pa) { if (pa->data > count) { count = pa->data; } pa = pa->next; } return count; }
求链表最大节点整体代码
#include <stdio.h> #include <string.h> #include <strlib.h> typedef struct LNode { int data; struct LNode* next; }LNode; typedef struct LNode* LinkList; //将指向结点结构体的指针命名为LinkList //构造一个空线性表L,创建成功返回OK struct LNode* InputData(struct LNode* head, int n); //求链表最大值 int Findbig(LinkList la); //输出链表 void output(struct LNode* head); //对链表进行赋值 struct LNode* InputData(struct LNode* head, int n) { struct LNode* p = NULL, * q = NULL; int data; p = (struct LNode*)malloc(sizeof(struct LNode)); head = p; q = p; while (n--) { scanf_s("%d", &data); p = (struct LNode*)malloc(sizeof(struct LNode)); p->data = data; q->next = p; q = p; } p->next = NULL; return head; } //进行求链表最大值 int Findbig(LinkList la) { struct LNode * pa , * pb , * pc , *u;//三个结点指针 int count = 0; pa = (struct LNode*)malloc(sizeof(struct LNode)); pa = la->next; pc = la; //使用la的头结点作为lc的头节点 while (pa) { if (pa->data > count) { count = pa->data; } pa = pa->next; } return count; } //输出链表 void output(struct LNode* head) { struct LNode* h = head->next;//跳过头节点 if (h == NULL) {//判空 printf("The Link is null"); return; } while (h != NULL) { printf("%d ", h->data); h = h->next; } free(h); } int main(void) { struct LNode* L1 = NULL; int n; printf("输入第一条链表的长度:"); scanf_s("%d", &n); //链表赋值 L1 = InputData(L1, n); //求交集 int count = Findbig(L1); printf("%d", count); }