admin 管理员组

文章数量: 1087678

链表头插尾插

尾插法
好好学习,天天向上

#include<stdio.h>
#include<stdlib.h>struct Student
{char cName[20];int iNumber;struct Student* pNext;
};//定义一个结构体类型
int icount;//记录节点数struct Student* Creat()
{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("%d",&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;}pNew=(struct Student*)malloc(sizeof(struct Student));printf("please %d enter name ,then Number\n",icount);scanf("%s",&pNew->cName);scanf("%d",&pNew->iNumber);}free(pNew);return pHead;//返回头指针,头指针指向第一个节点
}
//输出显示链表内容
void Print(struct Student* pHead)
{struct Student* pTemp;int ilndex=1;printf("---the List has %d members:---\n",icount);putchar('\n');pTemp=pHead;while(pTemp!=NULL){printf("the NO%d member is:\n",ilndex);printf("the name is :%s\n",pTemp->cName);printf("the number is %d\n",pTemp->iNumber);putchar('\n');pTemp=pTemp->pNext;//指向下一个节点ilndex++;}
}int main(){struct Student* pHead;pHead=Creat();Print(pHead);return 0;
}

尾插法
只是在创建链表时与头插法有不同

struct Student* Creat()
{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("%d",&pNew->iNumber);while(pNew->iNumber!=0){icount++;if(icount==1){pNew->pNext=pHead;pEnd=pNew;pHead=pNew;}else{pNew->pNext=pEnd;pHead=pNew;pEnd=pNew;}pNew=(struct Student*)malloc(sizeof(struct Student));printf("please %d enter name ,then Number\n",icount);scanf("%s",&pNew->cName);scanf("%d",&pNew->iNumber);}free(pNew);return pHead;
}


3.链表插入删除

#include<stdio.h>
#include<stdlib.h>struct Student
{char cName[20];int iNumber;struct Student* pNext;
};
int icount;struct Student* Creat()
{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("%d",&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;}pNew=(struct Student*)malloc(sizeof(struct Student));printf("please %d enter name ,then Number\n",icount);scanf("%s",&pNew->cName);scanf("%d",&pNew->iNumber);}free(pNew);return pHead;
}
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("%d",&pNew->iNumber);pNew->pNext=pHead;pHead=pNew;icount++;return pHead;} void Delete(struct Student* pHead,int ilndex)
{int i;struct Student* pTemp;struct Student* pPre;pTemp=pHead;pPre=pTemp;printf("---delete NO%d member---\n",ilndex);for(i=0;i<ilndex;i++){pPre=pTemp;pTemp=pTemp->pNext;}pPre->pNext=pTemp->pNext;free(pTemp);icount--;	
}
void Print(struct Student* pHead)
{struct Student* pTemp;int ilndex=1;printf("---the List has %d members:---\n",icount);putchar('\n');pTemp=pHead;while(pTemp!=NULL){printf("the NO%d member is:\n",ilndex);printf("the name is :%s\n",pTemp->cName);printf("the number is %d\n",pTemp->iNumber);putchar('\n');pTemp=pTemp->pNext;ilndex++;}
}int main(){struct Student* pHead;pHead=Creat();pHead=insert(pHead);Delete(pHead,2);Print(pHead);return 0;
}

本文标签: 链表头插尾插