链表

来源:互联网 发布:pink cat动作数据下载 编辑:程序博客网 时间:2024/06/06 13:16

一、链表特点

链表是由许多相同数据类型的数据项按特定排列顺序排列而成的线性表。特性是其各个数据项在内存中的排列是不连续且随机存放的,需要”动态分配内存“时,最适合链表的结构设计,可以让内存运用更具有弹性。

在C语言中,动态分配内存主要使用malloc()与free()函数,定义于头文件stdlib.h文件中。举例如下:

#include <stdio.h>#include <stdlib.h>#include <string.h> int main(){    char *str1="Hello World!";    char* str2=(char*)malloc(sizeof(char)*(strlen(str1)));    /* 动态分配与str1相同大小的内存空间 */    strcpy(str2,str1);/* 将str1字符串复制到str2字符串 */     printf("str1=%p str1=%s\n",str1,str1);    printf("-------------------------------------\n");    printf("str2=%p str2=%s\n",str2,str2);    printf("-------------------------------------\n");    free(str2);/* 释放str2内存空间 */     system("pause");    return 0;}

C++ 中的动态分配变量,使用new等关键字获取内存地址,用delete释放内存。代码片段举例如下:

int* m=new int;*m=50;cout<<"当前指针m所指向的地址:"<<m<<endl;delete m;cout<<"执行delete m 后指针m指向的地址:"<<m<<endl;

二、单向链表

一个单向链表由两个元素组成,数据字段和指针,指针则指向下一个元素在内存中的地址。
接下来是一段建立学生节点单向链表的算法

typedef struct student s_data;s_data *ptr; //Access pointer s_data *head;//Chain table pointers_data *new_data;//Pointer to the location of the new elementhead=(s_data*)malloc(sizeof(s_data));ptr=head;ptr->next=NULL;do{  printf("name IDnumber score: ");  scanf("%s %s %d",ptr->name,ptr->no,&ptr->score);  new_data=(s_data*)malloc(sizeof(s_data));//Add new element  ptr->next=new_data;  new_data->next=NULL;  ptr=ptr->next;}

三、遍历单向链表

即使用指针运算访问链表中的每个节点。

#include <stdio.h>#include <stdlib.h>int main(){    int select,student_no=0,num=0;    float Msum=0,Esum=0;    struct student    {        char name[20];        int Math;        int Eng;        char no[10];        struct student *next;    };    typedef struct student s_data;    s_data *ptr;        /* 存取指针 */    s_data *head;       /* 链表头指针 */    s_data *new_data;   /* 新增元素所在位置的指针 */    head = (s_data*) malloc(sizeof(s_data)); /* 建立链表头 */     head->next=NULL;    ptr = head;      do    {        printf("(1)新增 (2)离开 =>");        scanf("%d", &select);        if (select != 2)        {            printf("姓名 学号 数学成绩 英语成绩:");            new_data = (s_data*) malloc(sizeof(s_data));    /* 新增下一个元素 */            scanf("%s %s %d %d",new_data->name,new_data->no,&new_data->Math,&new_data->Eng);            ptr->next=new_data; /*存取指针设置为新元素所在位置 */            new_data->next =NULL;   /* 下一个元素的next先设置为null */            ptr=ptr->next;            num++;        }    } while (select != 2);    ptr = head->next;    /* 设置存取指针从头开始 */    putchar('\n');    while (ptr!= NULL)    {        printf("姓名:%s\t学号:%s\t数学成绩:%d\t英语成绩:%d\n",          ptr->name,ptr->no,ptr->Math,ptr->Eng);        Msum+=ptr->Math;        Esum+=ptr->Eng;        student_no++;        ptr= ptr ->next;    /* 将ptr移往下一个元素 */    }    printf("---------------------------------------------------------\n");    printf("本链表学生数学平均成绩:%.2f 英语平均成绩:%.2f\n",Msum/student_no,Esum/student_no);     system("pause");    return 0;}

四、单向链表插入新节点

举例如下:

struct employee{      int num,score;    char name[10];    struct employee *next;};typedef struct employee node;typedef node *link;link findnode(link head,int num){     link ptr;     ptr=head;     while(ptr!=NULL)     {        if(ptr->num==num)           return ptr;        ptr=ptr->next;     }     return ptr;}link insertnode(link head,link ptr,int num,int score,char name[10])   {      link InsertNode;    InsertNode=(link)malloc(sizeof(node));    if(!InsertNode)       return NULL;    InsertNode->num=num;    InsertNode->score=score;    strcpy(InsertNode->name,name);    InsertNode->next=NULL;    if(ptr==NULL)  /*插入第一个节点*/     {       InsertNode->next=head;       return InsertNode;    }        else    {        if(ptr->next==NULL)/*插入最后一个节点*/        {           ptr->next=InsertNode;        }        else /*插入中间节点*/        {            InsertNode->next=ptr->next;            ptr->next=InsertNode;             }    }    return head;}
0 0
原创粉丝点击