带头结点
/*****************************************/
/*****************************************/
/************带头结点单向链表**************/
/*****************************************/
/*****************************************/
#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");
}