首页 > 其他分享 >2008秋季-线性表的链式存储(仅单链表)

2008秋季-线性表的链式存储(仅单链表)

时间:2023-11-08 10:06:48浏览次数:41  
标签:head 单链 nodetype 线性表 temp int next 2008 struct

/*---------------------------------------------------------

 Title: 单链表

 Date: September 1, 2008

 Fuction: 单链表的初始化,创建,插入,删除,查找结点。

 参考PPT讲稿或者教材 2.2.4 节.(p56-63)

 ----------------------------------------------------------*/

#include<stdio.h>

#include<stdlib.h>
struct nodetype//定义结点
{

 int data;//数据域(可以是int,char,float,数组,结构体等类型)
 struct nodetype * next;

 //next指针域,指向下一个结点。
};

struct nodetype * InitialLinkList()

{
//初始化链表,返回头指针
 struct nodetype * head;

 head=(struct nodetype *)malloc(sizeof(struct nodetype));//
 head->next=NULL;

 return head;

}

void CreateLinkListInRear(struct nodetype * head, int numbers[], 

                          int LengthOfNumbers)

{

    //尾接法创建单链表

    //从数组numbers[]中取出元素建立单链表

    //LengthOfNumbers为元素个数
    int i;

    struct nodetype * temp,* rear;

    rear=head;

    for(i=0;i<LengthOfNumbers;i++)

    {

        temp=(struct nodetype *)malloc(sizeof(struct nodetype));

        temp->data=numbers[i];

        temp->next=NULL;

        rear->next=temp;

        rear=temp;

    }

}

void CreateLinkListInHead(struct nodetype * head, int numbers[],

                          int LengthOfNumbers)

{

    //头插法建立单链表

    //从数组numbers[]中取出元素建立单链表

    //LengthOfNumbers为元素个数
    int i;

    struct nodetype * temp;

    for(i=0;i<LengthOfNumbers;i++)

    {

        temp=(struct nodetype *)malloc(sizeof(struct nodetype));

        temp->data=numbers[i];

        temp->next=head->next;

        head->next=temp;


    }

}

struct nodetype * SearchInLinkList(struct nodetype * head, int x)

{

    //在链表中查找x。成功返回结点指针,失败返回NULL。
    struct nodetype * p=head->next;

    while(p!=NULL)

    {

    if(p->data==x)

        return p;

    else

        p=p->next;

    }

    return NULL;


}

void InsertNumberIntoLinkList(struct nodetype * head,int key,int x)

{

 //在key之后插入x
 struct nodetype * location,*temp;

 if((location=SearchInLinkList(head,key))!=NULL)

    {

        temp=(struct nodetype *)malloc(sizeof(struct nodetype));

        temp->data=x;

        temp->next=location->next;

        location->next=temp;

    }

 else

     printf("\nNot Found, Insert failed!\n");

}

void DeleteNumberFromLinkList(struct nodetype * head,int x)

{

 //从链表中删除x。
 struct nodetype * p,*q;

 q=head;

 p=head->next;   

 while (p!=NULL && p->data!=x) 

  {q=p;

   p=p->next;

  }

 if(p!=NULL)

  {q->next=p->next;

   free(p); /*释放被删结点所占的内存空间*/

  }

 else

   printf("invaild delete position\n");

}

void PrintIntegerLinkList(struct nodetype * head)

{//显示链表结点数据
    struct nodetype *temp;

    temp=head->next;

    while(temp!=NULL)

    {

        printf("%d ",temp->data);

        temp=temp->next;

    }


}

void main()

{

 struct nodetype *head,*head2;

 int y[5]={4,5,6,7,8};

 head2=InitialLinkList();//初始化
 CreateLinkListInRear(head2,y,5);//以数组y中元素创建链表
 DeleteNumberFromLinkList(head2,6);//删除6
 PrintIntegerLinkList(head2);//显示删除6后链表
}

标签:head,单链,nodetype,线性表,temp,int,next,2008,struct
From: https://blog.51cto.com/emanlee/8245159

相关文章

  • 2008秋-计算机软件基础-结构体与指针复习
    //结构体与指针#include<string.h>#include<stdio.h>structstudent{intnumber;charname[10];};voidmain(){structstudenta;structstudent*ptr=&a;a.number=10;//ptr->number=10;strcpy(a.name,"li");//strcpy(ptr->......
  • 2008秋-顺序栈-顺序存储结构的栈
    /*---------------------------------------------------------Title:SequenceStack(顺序栈)顺序栈-顺序存储结构的栈请先阅读教材67页,2.3.2,2.3.3节,栈的定义及基本运算(注意以下程序为简化后的,仅供入门学习之用)-------------------------------------------------......
  • 2008秋-链栈-链式存储结构的栈
    /*---------------------------------------------------------Title:LinkStack(链栈)链栈-链式存储结构的栈请先阅读教材67页,2.3.2,2.3.4节,栈的定义及基本运算(注意以下程序为简化后的,仅供入门学习之用)----------------------------------------------------------......
  • 2008秋-计算机软件基础-循环顺序队列
    /*---------------------------------------------------------Title:SequenceQueue(顺序队列)顺序队列-顺序存储结构的队列请先阅读教材74-76页,2.4.1-2.4.3节,队列的定义及基本运算(注意:以下程序为简化后的,仅供入门学习之用)--------------------------------------......
  • 2008秋季-计算机软件基础-循环链队列
    /*---------------------------------------------------------Title:LinkQueue(链队列)链队列-链式存储结构的队列请先阅读教材74-77页,2.4.1-2.4.4节,队列的定义及基本运算(注意:以下程序为简化后的,仅供入门学习之用)---------------------------------------------......
  • 2008秋-计算机软件基础-第三章- 二叉排序树
    /*---------------------------------------------------------Title:二叉排序树(BinarySortingTree)请先阅读教材91-93,96-99页,3.2.3,3.2.7节,(注意以下程序为简化后的,仅供入门学习之用)----------------------------------------------------------*/#includ......
  • Visual Studio 2008安装ASP.NET MVC 2 RTM
    1首先,要安装VisualStudio2008SP1,下载地址http://www.microsoft.com/en-us/download/details.aspx?id=109862下载ASP.NETMVC2RTM(英文版,2.5M,AspNetMVC2_VS2008.exe)下载地址http://www.microsoft.com/en-us/download/details.aspx?id=220793双击AspNetMVC2_VS2008.e......
  • 解题 [HNOI2008] GT考试
    题目:[HNOI2008]GT考试阿申准备报名参加GT考试,准考证号为\(N\)位数\(X_1,X_2…X_n\(0\leX_i\le9)\),他不希望准考证号上出现不吉利的数字。他的不吉利数字\(A_1,A_2,\cdots,A_m\(0\leA_i\le9)\)有\(M\)位,不出现是指\(X_1,X_2\cdotsX_n\)中没有恰好一段等于\(A_......
  • .Net 5.0 程序在 Linux 环境访问 SqlServer 2008R2 莫名报错:Connection reset by peer
    〇、问题详情同样的代码,在Windows上运行的好好的,拿到CentOS7上运行就出现如下报错:【ex.message】:Aconnectionwassuccessfullyestablishedwiththeserver,butthenanerroroccurredduringtheloginprocess.(provider:TCPProvider,error:35-Aninternal......
  • 题解:[SCOI2008] 城堡
    应该是联赛前最后一次任性了,浪费的时间有点多,不过也揭露了我的基础知识和代码能力都很弱的问题,得加油啊。先stodwt。给定一棵基环树森林,起初有\(m\)个点已被选进\(S\)里,你需要再选\(k\)个点加入到\(S\)中,最小化其余点到\(S\)距离的最大值。这个问题直接做非常困难,......