C语言实现链表的创建,链表的遍历,链表的节点删除,链表的节点插入。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct User{
char name[20];
int age;
int id;
struct User *next;
};
int n = 0;
//指针
void logIntA(int *);
void exchangeA(int *,int *);
void exchangeB(int *,int *);
//函数指针
int min(int , int);
int max(int , int);
//指针函数
int *fun(int a , int b);
//单向链表
struct User * CreateSinglyLinkedList();
void logLinkedList(struct User *head);
void deleteNode(struct User *head, int id);
struct User *insertNode(struct User *head , int id , struct User *node);
struct User* flipLikedList(struct User *head);
int main(int argc, const char * argv[]) {
// int a = 11;
// int b = 22;
// printf("a:%p --- b:%p\n",&a ,&b);
// exchange(&a, &b);
// printf("a:%d \t b:%d\n",a,b);
// printf("a:%p --- b:%p\n",&a ,&b);
// int *p = NULL;
// int (*f)(int a,int b);//函数指针
// f = min;
// *p = (*f)(a , b);
// printf("p:%p \t *p:%d\n",p,*p);
// f=max;
// *p = (*f)(a , b);
// printf("p:%p \t *p:%d\n",p,*p);
// p = fun(a, b);
// printf("p:%p \t *p:%d\n",p,*p);
int id = -1;
struct User *head = CreateSinglyLinkedList();
printf("*************\n");
logLinkedList(head);
// printf("delete User id:\n");
// scanf("%d",&id);
// deleteNode(head, id);
// logLinkedList(head);
// while (1) {
// struct User *node = malloc(sizeof(struct User));
// node->id = ++n;
// printf("please input name age:\n");
// scanf("%s %d",node->name , &node->age);
// printf("please input insertNode where id:\n");
// scanf("%d",&id);
// head = insertNode(head, id, node);
// logLinkedList(head);
// }
head = flipLikedList(head);
printf("*************\n");
logLinkedList(head);
return 0;
}
//指针
void exchangeA(int *p1,int *p2){
int a = *p1;
*p1 = *p2;
*p2 = a;
}
void exchangeB(int *p1,int *p2){
int *p = p1;
printf("p1:%p --- p2:%p\n",p1 ,p2);
p1 = p2;
p2 = p;
printf("p1:%p --- p2:%p\n",p1 ,p2);
}
void logIntA(int *p){
printf("p:%p --- *p:%d\n",p , *p);
*p = *p + 10;
}
// 指针函数
int *fun(int a , int b){
int *p = (int *)malloc(sizeof(int));
*p = a + b;
return p;
}
int min(int a,int b){
return a<b ? a : b;
}
int max(int a, int b){
return a>b ? a : b;
}
/**
创建链表
@return 链表
*/
struct User * CreateSinglyLinkedList(){
struct User *newP;
struct User *lastP = NULL;
struct User *head = NULL;
//
newP = malloc(sizeof(struct User));
if (newP == NULL) {
printf("create error");
return NULL;
}else{
printf("please input name age:\n");
scanf("%s %d",newP->name , &newP->age);
}
while (newP->age != 0) {
n++;
if (head == NULL) {
head = newP;
}else{
lastP->next = newP;
}
newP->id = n;
lastP = newP;
newP = malloc(sizeof(struct User));
printf("please input name age:\n");
scanf("%s %d",newP->name , &newP->age);
}
free(newP);
if (head != NULL) {
lastP->next = NULL;
}
newP = NULL;
return head;
}
/**
打印链表
@param head 链表
*/
void logLinkedList(struct User *head){
struct User *p;
p = head;
while (p != NULL) {
printf("p:%p \t name:%s \t age:%d \t id:%d\n",p,p->name,p->age,p->id);
p = p->next;
}
}
/**
删除节点
@param head 链表
@param id 节点值
*/
void deleteNode(struct User *head, int id){
if (head == NULL) {
printf("not find");
return;
}
struct User *p = head;
struct User *lastP = NULL;
while (p->id != id && p->next != NULL) {
lastP = p;
p = p->next;
}
if (p->next == NULL) {
printf("not find\n");
return;
}else{
if (p == head) {
*head = *p->next;
}else{
lastP->next = p->next;
}
}
free(p);
p = NULL;
}
/**
插入节点
@param head 链表
@param id 节点值 位置
@param node 节点
@return 新链表
*/
struct User *insertNode(struct User *head , int id , struct User *node){
struct User *p = head;
if (head == NULL) {
head = node;
node->next = NULL;
return head;
}
while (p->id != id && p->next != NULL) {
p = p->next;
}
if (p->id != id) {
printf("not find\n");
return head;
}else{
//不可颠倒
node->next = p->next;//插入节点的next指向p下一个节点,也就是p后面的node
p->next = node;//p的next指向node
}
return head;
}
/**
翻转链表
@param head 链表
@return 翻转后的链表
*/
struct User* flipLikedList(struct User *head){
if (head == NULL) {
return head;
}
struct User *cur_node;
struct User *new_node;
struct User *tmp_node;
new_node = NULL;
cur_node = head;
while (cur_node != NULL) {
tmp_node = cur_node->next;//记录下一个node的指针
cur_node->next = new_node;//把当前节点的next指向新的节点
new_node = cur_node;//把当前节点赋值给新节点
cur_node = tmp_node;//当前节点移动到下一个节点
}
head = new_node;
return head;
}