头文件:
#ifndef LISTSTU
#define LISTSTU
#define N 20
#define NULL 0
#define TYPE struct stu
#define LEN sizeof(struct stu)
#pragma warning(disable:4996)
struct stu{
char name[N];
int num;
int age;
struct stu *next;
};
TYPE *creat(int n);
TYPE *creatR(int n);
int search(TYPE *head, int num);
TYPE *delete1(TYPE *head, int i);
TYPE *insert1(TYPE *head,int i, TYPE *node);
void printStu(TYPE *head);
void Destory(TYPE *head);
#endif
资源文件:
#include<stdio.h>
#include<stdlib.h>
#include "liststu.h"
#pragma warning(disable:4996)
/***********************
函数名:creat()
形参: 整数 n
功能:结构体初始化
作者:刘 洋
时间:2016/12/7 16:45
************************/
TYPE *creat(int n){
TYPE *head, *p, *q;
head = (TYPE *)malloc(LEN);
//head->name[0] = '\0';
head->next = NULL;
//尾插法,正序排列
p = head;
int i;
for (i = 0; i < n; i++){
q = (TYPE*)malloc(LEN);
printf("输入姓名,学号,年龄:");
scanf("%s%d%d",q->name,&q->num, &q->age);//如果输入的字符有空格,就使用gets
p->next = q;
p = q;
}
p->next = NULL;
return(head);
}
/***********************
函数名:creatR()
形参: 整数 n
功能:结构体初始化(头插法)逆序
作者:刘 洋
时间:2016/12/7 16:45
************************/
TYPE *creatR(int n){
TYPE *head, *q;
int i;
head = (TYPE *)malloc(LEN);
//head->name[0] = '\0';
head->next = NULL;
//头插法,逆序排列
for (i = 0; i < n; i++){
q = (TYPE*)malloc(LEN);
printf("输入姓名,学号,年龄:");
scanf("%s%d%d", q->name, &q->num, &q->age);//如果输入的字符有空格,就使用gets
q->next = head->next;
head->next= q;
}
return(head);
}
/***********************
函数名:printStu()
形参: 结构体头结点 head
功能: 打印结构体链表
作者: 刘 洋
时间: 2016 / 12 / 7 16:45
* *********************** */
void printStu(TYPE *head){
if (head == NULL) return;
TYPE *p = head->next;
while (p != NULL){
printf("name=%s,number=%d,age=%d\n",p->name,p->num, p->age);
p = p->next;
}
printf("\n");
}
/***********************
函数名:search()
形参: 结构体头结点 head, 学号 number
功能: 是否存在number,存在返回位置i ,不存在返回 0
作者: 刘 洋
时间: 2016 / 12 / 7 16:45
* *********************** */
int search(TYPE *head, int num){
int flag = 1;
TYPE *p = head->next;
while (p != NULL&&p->num != num){
flag++;
p = p->next;
}
if (p == NULL)
return 0;
else
return flag;
}
/***********************
函数名:delete1()
形参: 结构体头结点 head, 学号 number
功能: 删除第i个节点
作者: 刘 洋
时间: 2016 / 12 / 7 16:45
* *********************** */
TYPE *delete1(TYPE *head, int i){
TYPE *pre = head,*q;
int j = 0;
while (pre != NULL && j<i-1 ){
pre = pre->next;
j++;
}
if (pre == NULL)
return head;
else{
q = pre->next;
if (q == NULL)
return head;
pre->next = q->next;
free(q);
return head;
}
}
/***********************
函数名:insert1()
形参: 结构体头结点 head,插入位置 i ,链表节点 node
功能: 插入链表节点
作者: 刘 洋
时间: 2016 / 12 / 7 16:45
************************ */
TYPE *insert1(TYPE *head, int i, TYPE *node){
int j = 0;
TYPE *p = head, *q;
//寻找i-1的位置
while (j < i - 1 && p != NULL){
j++;
p = p->next;
}
//找不到i-1位置
if (p == NULL)
return head;
else{
node->next = p->next;
p->next = node;
return head;
}
}
/*************************
函数名:Destory()
形参: 结构体头结点 head,
功能: 释放链表占用的空间
作者: 刘 洋
时间: 2016 / 12 / 7 16:45
* *********************** */
void Destory(TYPE *head){
TYPE *pre = head, *p = pre->next;
while (p != NULL){
free(pre);
pre = p;
p = pre->next;
}
free(pre);
}
主函数:
#include<stdio.h>
#include<stdlib.h>
#include "liststu.h"
#pragma warning(disable:4996) //用于scanf的使用
int main(){
printf("start program!\n");
int m;
printf("enter number n:");
scanf_s("%d",&m);
TYPE *studentlist=creat(m);
printStu(studentlist);
int localNum = search(studentlist, 567);
printf("存在的位置local:%d", localNum);
printf("\n");
printf("输入要删除第i个节点i:");
int number;
scanf("%d", &number);
studentlist = delete1(studentlist,number);
printStu(studentlist);
stu *node;
node = (stu *)malloc(sizeof(stu));
printf("输入姓名:");
scanf("%s", node->name);
node->num = 123;
node->age = 23;
node->next = NULL;
studentlist = insert1(studentlist,2,node);
printStu(studentlist);
//删除所有
//Destory(studentlist);
printStu(NULL);
system("pause");
return 0;
}