欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

创建一个带头节点的单向链表,找数据域中最大值

程序员文章站 2024-03-21 14:14:46
...
#include <stdio.h>
#include <stdlib.h>
#pragma warning (disable:4996)
struct list
{   
	int data;
    struct list *next;
};

struct list *createlist(int data[], int n)
{   
	struct list *head = 0, *p, *q;
    int i;

    head = (struct list *) malloc(sizeof(struct list));
    head->data = data[0];
    p = q = head;
    for(i=1; i<n; i++)
    {   p = (struct list *) malloc(sizeof(struct list));
        p->data = data[i]; q->next = p; q = p;
    }
    p->next = NULL;
    return head;
}

/**********found**********/
int func(struct list * head) //注意指针类型
{   int pmax = head->data;
    struct list *p = head->next;
    while(p != NULL)
    {   if(p->data > pmax) pmax = p->data;
/**********found**********/
        p = p->next;
    }
/**********found**********/
  return pmax;
}

void main()
{
    int data[] = {123, 21, 65, 789, 32, 310, 671, 651, 81, 101}, pmax;
 
    struct list *head;
    head = createlist(data, 10);
    pmax = func(head);
    printf("Max=%d\n", pmax);
}