一、单链表的创建
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct slist
{
int id;
struct slist* next;
}L;
L* create_node(int data)
{
L* p = malloc(sizeof(L));
if (NULL == p)
{
printf("malloc error!\n");
return NULL;
}
memset(p, 0, sizeof(L));
p->id = data;
p->next = NULL;
}
void tail_insert(L* pH, L* new)
{
L* p = pH;
while (NULL != p->next)
{
p = p->next;
}
p->next = new;
};
void top_insert(L* pH, L* new)
{
L* p = pH;
new->next = p->next;
p->next = new;
}
void Print_node(L* pH)
{
L* p = pH;
p = p->next;
while (NULL != p->next)
{
printf("id:%d\n", p->id);
p = p->next;
}
printf("id:%d\n", p->id);
}
int detele_list_node(L* pH, int data)
{
L* p = pH;
L* prev = NULL;
while (NULL != p->next)
{
prev = p;
p = p->next;
if (p->id == data)
{
if (p->next != NULL)
{
prev->next = p->next;
free(p);
}
else
{
prev->next = NULL;
free(p);
}
return 0;
}
}
printf("没有要删除的节点\n");
return -1;
}
void trave_list(L* pH)
{
L* p = pH->next;
L* pBack;
int i = 0;
if (p->next == NULL || p == NULL)
return;
while (NULL != p->next)
{
pBack = p->next;
if (p == pH->next)
{
p->next = NULL;
}
else
{
p->next = pH->next;
}
pH->next = p;
p = pBack;
}
top_insert(pH, p);
}
int main(int argc, char** argv)
{
int i;
L* header = create_node(0);
for (i = 1; i < 10; i++)
{
tail_insert(header, create_node(i));
}
Print_node(header);
detele_list_node(header, 5);
putchar('\n');
Print_node(header);
putchar('\n');
trave_list(header);
Print_node(header);
return 0;
}
二、单链表项目
#define _CRT_SECURE_NO_WARNINGS
#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 deleteNodeByAppoinNum(struct Node* headNode, int num)
{
struct Node* posNode = headNode->next;
struct Node* posNodeFront = headNode;
if (posNode == NULL)
{
printf("无法删除链表为空");
}
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("请输入学生的姓名 学号 成绩:");
scanf("%s%d%d", info.name, &info.num, &info.math);
insertNodeByHead(list, info);
printf("continue(Y/N)?\n");
int choice = 0;
scanf("%d", &choice);
if (choice == 'N' || choice == 'n');
{
break;
}
}
printf(list);
system("pause");
return 0;
}