#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node* next;
};
//创建链表
struct Node* createList()
{
struct Node* headNode=(struct Node*)malloc(sizeof(struct Node));
headNode->next=NULL;
return headNode;
}
//创建结点
struct Node* createNode(int data)
{
struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->data=data;
newNode->next=NULL;
return newNode;
}
//遍历链表
void printList (struct Node *headNode)
{
struct Node* pMove=headNode->next;
while(pMove)
{
printf("%d\n",pMove->data);
pMove=pMove->next;
}
printf("\n");
}
//插入节点,参数 :插入的那个链表,插入节点的数据多少
void insertNodeByHead( struct Node *headNode,int data)
{
struct Node* newNode=createNode(data);
newNode->next=headNode->next;
headNode->next=newNode;
}
//删除结点
void deletNodeByAppoinNum(struct Node* headNode ,int num)
{
struct Node *posNode = headNode->next;
struct Node *posNodeFront = headNode;
if(posNode == NULL)
{
printf("无法删除,链表为空\n");
}
else
{
while(posNode->data != num)
{
posNodeFront=posNode;
posNode=posNodeFront->next;
if(posNode == NULL)
{
printf("没找到相关信息,无法删除\n");
return;
}
}
}
posNodeFront->next = posNode->next;
free(posNode);
}
int main()
{
struct Node *list=createList();
insertNodeByHead(list,1);
insertNodeByHead(list,2);
insertNodeByHead(list,3);
printList(list);
deletNodeByAppoinNum(list,2);
printList(list);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
struct student
{
char name[20];
int num;
int math;
};
struct Node {
struct student data;
struct Node* next;
};
//创建链表
struct Node* createList()
{
struct Node* headNode=(struct Node*)malloc(sizeof(struct Node));
headNode->next=NULL;
return headNode;
}
//创建结点
struct Node* createNode(struct student data)
{
struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->data=data;
newNode->next=NULL;
return newNode;
}
//遍历链表
void printList (struct Node *headNode)
{
struct Node* pMove=headNode->next;
printf("name\tnum\tmath\n");
while(pMove)
{
printf("%s\t%d\t%d\n",pMove->data.name,pMove->data.num,pMove->data.math);
pMove=pMove->next;
}
printf("\n");
}
//插入节点,参数 :插入的那个链表,插入节点的数据多少
void insertNodeByHead( struct Node *headNode,struct student data)
{
struct Node* newNode=createNode(data);
newNode->next=headNode->next;
headNode->next=newNode;
}
//删除结点
void deletNodeByAppoinNum(struct Node* headNode ,int num)
{
struct Node *posNode = headNode->next;
struct Node *posNodeFront = headNode;
if(posNode == NULL)
{
printf("无法删除,链表为空\n");
}
else
{
while(posNode->data.num != num)
{
posNodeFront=posNode;
posNode=posNodeFront->next;
if(posNode == NULL)
{
printf("没找到相关信息,无法删除\n");
return;
}
}
}
posNodeFront->next = posNode->next;
free(posNode);
}
int main()
{
struct Node *list=createList();
struct student info;
while(1)
{
printf("请输入学生的姓名 学号 数学成绩:\n");
setbuf(stdin,NULL);
scanf("%s%d%d",info.name,&info.num,&info.math);
insertNodeByHead(list,info);
printf("cotinue(Y/N)?\n");
setbuf(stdin,NULL);
int choice = getchar();
if(choice == 'N' || choice== 'n' )
{
break;
}
}
system("pause");
printList(list);
printf("请输入要删除学生的学号:");
scanf("%d",&info.num);
deletNodeByAppoinNum(list,info.num);
printList(list);
return 0;
}