完整的链表操作(定义-创建-插入-删除-输出)

#include<stdio.h> 
#include<stdlib.h>

struct Student
{
    char cName[20]; //姓名 
    int iNumber;    //学号 
    struct Student* pNext;  //指向下一个结点的指针 
};

int iCount; //全局变量表示链表长度

struct Student* Create()
{
    struct Student *pHead=NULL; //初始化链表头指针为空
    struct Student *pEnd,*pNew;
    iCount=0;   /*初始化链表长度*/
    pEnd=pNew=(struct Student*)malloc(sizeof(struct Student)); 
    printf("please first enter Name ,then Number\n");
    scanf("%s",&pNew->cName);
    scanf("%s",&pNew->iNumber);
    while(pNew->iNumber!=0)
    {
        iCount++;
        if(iCount==1)
        {
            pNew->pNext=pHead;  //使得指向为空
            pEnd=pNew;  //跟踪新加入的结点
            pHead=pNew;     //头指针指向首结点 
        }
        else
        {
            pNew->pNext=NULL;   //新结点的指针为空
            pEnd->pNext=pNew;   //原来的尾结点指向新结点
            pEnd=pNew;      //pEnd指向新结点 

         } 
         pNew=(struct Student*)malloc(sizeof(struct Student));  //再次分配结点内存空间 
         scanf("%s",&pNew->cName);
         scanf("%s",&pNew->iNumber);
    }
     free(pNew);
     return pHead;
 } 

 void Print(struct Student* pHead)
 {
    struct Student *pTemp;  //循环所用的临时指针
     int iIndex=1;  //表示链表中结点的序号

     printf("The List has %d members:\n",iCount) ;  //信息提示
     pTemp=pHead;   //指针得到首结点的地址

     while(pTemp!=NULL) 
     {
        printf("the NO.%d member is:\n",iIndex);    
        printf("the name is:%s\n",pTemp->cName);    //输出姓名 
        printf("the number is :%d\n",pTemp->iNumber);   //输出学号 
        pTemp=pTemp->pNext; //移动临时指针到下一个结点 
        iIndex++;       //进行自加运算 
     }
 }

 struct Student* Insert(struct Student* pHead) 
 {
    struct Student* pNew;   //指向新分配的空间 
    printf("---Insert member at first---\n");
    /*分配内存空间,并返回指向该内存的空间的指针*/ 
    pNew=(struct Student*)malloc(sizeof(struct Student));
    scanf("%s",&pNew->cName);
    scanf("%s",&pNew->iNumber);

    pNew->pNext=pHead;  //新结点指针指向原来的首结点 
    pHead=pNew; //头指针指向新结点
     iCount++;  //增加链表结点数量
     return pHead; 

 }

 void Delete(struct Student* pHead,int iIndex)      //pHead表示头结点,iIndex表示要删除的结点的下标
 {
    int i;  //控制循环变量
     struct Student* pTemp; //临时指针
     struct Student* pPre;  //表示要删除结点前的结点
     pTemp=pHead;   //得到头结点
     pPre=pTemp;

     printf("---Delete NO%d member---\n",iIndex);
     for(i=1;i<iIndex;i++)      //for循环使得pTemp指向要删除的结点 
     {
        pPre=pTemp;
        pTemp=pTemp->pNext;
      } 
      pPre->pNext=pTemp->pNext;     //连接删除结点两边的结点 
      free(pTemp);      //释放掉要删除结点的内存空间 
      iCount--;     //减少链表中的元素个数 
  } 

  int main() 
  {
    struct Student* pHead;  //定义头结点
      pHead=Create();   //创建结点
      pHead=Insert(pHead);  //插入结点 
      Delete(pHead,2);  //删除第二个结点的操作 
      Print(pHead);     //输出链表 
      return 0;     //程序结束 

  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值