C语言版带头指针的单向链表

带头结点
/*****************************************/
/*****************************************/
/************带头结点单向链表**************/
/*****************************************/
/*****************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student
{
    //学生学号
    int id;
    //学生姓名
    char name[20];
    //分数
    int grade;
    //头指针位置
    struct Student *next;
};
struct Student *head;
//创建头结点
void AddHeadNode()
{
    if(head==NULL)
    {
        head=(struct Student *)malloc(sizeof(struct Student));        
        head->id=1000;
        strcpy(head->name,"test");
        head->grade=100;
        head->next=NULL;
    }
}
//创建结点列表
void  CreatNodeList()
{
    struct Student *temp=head;
    int ExitType=0;
    while(1)
    {
        struct Student *p=(struct Student *)malloc(sizeof(struct Student));        
        char s[20];
        printf("请输入学号:");
        scanf("%d",&p->id);
        printf("请输入姓名:");
        scanf("%s",&p->name);
        printf("请输入成绩:");
        scanf("%d",&p->grade);
        p->next=NULL;    
        if(head->next==NULL)
            head->next=p;
        temp->next=p;
        temp=p;
        printf("继续按1,退出按2!");
        scanf("%d",&ExitType);
        if(ExitType==2 || ExitType!=1)
            break;
    }    
}
//遍历
void foreachNode()
{
    struct Student *p=head->next;
    printf("开始遍历:\n");
    while(p!=NULL)
    {
        printf("学号:%d,姓名:%s,成绩:%d\n",p->id,p->name,p->grade);
        p=p->next;
    }
}
//查找某结点
void FindById(int ID)
{
    struct Student *p=head->next;
    printf("开始查找....\n");
    while(p!=NULL)
    {
        if(p->id==ID)
        {
            printf("已找到该节点!");
            break;
        }
        p=p->next;
    }
    if(p==NULL)
        printf("没有找到哦!");
}
// 删除某结点
void DeleteById(int ID)
{
    struct Student *p=head->next;
    struct Student *Last=head;
    printf("开始查找中...\n");
    while(p!=NULL)
    {
        if(p->id==ID)
        {
            printf("找到要删除的结点了请稍后...\n");
            Last->next=p->next;
            free(p);
            break;
        }
        else
        {
            Last=p;
            p=p->next;
        }
    }
}
//主函数
void main()
{
    AddHeadNode();
    CreatNodeList();
    FindById(1001);
    DeleteById(1001);
    foreachNode();
    system("pause");
}
不带头结点
/*****************************************/
/*****************************************/
/************不带头结点单向链表**************/
/*****************************************/
/*****************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int NodeList;
struct Student
{
    //学生学号
    int id;
    //学生姓名
    char name[20];
    //分数
    int grade;
    //头指针位置
    struct Student *next;
};
struct Student * head;
//创建链表
/*
struct Student * CreateList(struct Student students[],int n)
{
struct Student *temp;
struct Student *head;
for(int i=0;i<n;i++)
{
struct Student *p=(struct Student *)malloc(sizeof(struct Student));
p->id=students[i].id;
strcpy(p->name,students[i].name);
p->grade=students[i].grade;
p->next=NULL;
if(i==0)
head=p;
else
temp->next=p;
temp=p;
}
return head;
//
}*/
//创建列表
struct Student * CreateNode()
{
    struct Student *temp;
    head=NULL;
    int ExitType=0;
    while(1)
    {
        struct Student *p=(struct Student *)malloc(sizeof(struct Student));        
        int ID,Marks;
        char s[20];
        printf("请输入学号:");
        scanf("%d",&p->id);
        printf("请输入姓名:");
        scanf("%s",&p->name);
        printf("请输入成绩:");
        scanf("%d",&p->grade);
        p->next=NULL;    
        if(head==NULL)
            head=p;
        else
            temp->next=p;
        temp=p;
        NodeList++;
        printf("继续按1,退出按2!");
        scanf("%d",&ExitType);
        if(ExitType==2 || ExitType!=1)
            break;
    }    
    return head;
}
//删除节点
void DeleteNode(int ID)
{
    struct Student *p=head;
    struct Student *Last=head;
    printf("开始遍历:\n");
    while(p!=NULL)
    {
        if(p->id==ID)
        {
            printf("Find  OK! Delete wait...\n");
            if(p==head)
                head=p->next;
            else
                Last->next=p->next;
            free(p);
            break;
        }
        else
        {
            Last=p;
            p=p->next;
        }
    }
    if(p==NULL)
    {
        printf("Node Delete Ok\n");
    }
}
//添加节点
void InsertNode(int ID)
{
    struct Student *p=head;
    printf("开始插入:\n");
    while(p!=NULL)
    {
        if(p->id==ID)
        {
            printf("Find InsertArea OK!");
            struct Student *pp=(struct Student *)malloc(sizeof(struct Student));        
            printf("开始插入节点:\n");
            if(p==head)
            {
                pp->id=1030;
                strcpy(pp->name,"头结点插入测试");
                pp->grade=102;
                pp->next=head->next;
                head->next=pp;
            }
            else
            {
                pp->id=1030;
                strcpy(pp->name,"测试");
                pp->grade=102;
                pp->next=p->next;
                p->next=pp;
            }
            break;
        }
        else
            p=p->next;
    }
}
//查找
void SerchById(int ID)
{
    struct Student *p=head;
    printf("开始遍历:\n");
    while(p!=NULL)
    {
        if(p->id==ID)
        {
            printf("Find  OK!");
            break;
        }
        else
            p=p->next;
    }
    if(p==NULL)
        printf("Not Found!");
}
//遍历链表
void ForeachNode()
{
    struct Student *p=head;
    printf("开始遍历:\n");
    while(p!=NULL)
    {
        printf("学号:%d,姓名:%s,成绩:%d\n",p->id,p->name,p->grade);
        p=p->next;
    }
}
//主函数开始
void main()
{
    struct Student *head=CreateNode();
    ForeachNode();
    DeleteNode(1003);
    InsertNode(1005);
    ForeachNode();
    system("pause");
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值