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

/*---------------------------------------------------------
 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后链表
}
posted @   emanlee  阅读(846)  评论(2编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示