创建和遍历
头文件.h
在头文件中申明文件类型和函数
typedef struct Node *PtrToNode;
typedef PtrToNode Position;
typedef PtrToNode List;
typedef int NodeElement;
struct Node {
NodeElement element;
Position Next;
};
extern Position CreateListCell(List ahead, NodeElement element);
extern List CreateHead();
extern void OverViewList(List head);
creat_overview.c
这个文件中的函数通过在.h文件中添加extern申明,可以在别的文件中进行调用。
#include<stdio.h>
#include<string.h>
#include<sys/time.h>
#include<stdlib.h>
#include<errno.h>
#include"list.h"
Position CreateListCell(List ahead, NodeElement element){
Position position = NULL;
position = (Position)malloc(sizeof(struct Node));
if(position==NULL){
fprintf(stderr, "there is no space\n");
return NULL;
}
position->element = element;
ahead->Next = position;
position->Next = NULL;
return position;
}
List CreateHead()
{
List head = NULL;
Position position = NULL;
head = (Position)malloc(sizeof(struct Node));
if(head==NULL){
fprintf(stderr, "there is no space\n");
return NULL;
}
head->element = 0;
head->Next = NULL;
return head;
}
void OverViewList(List head)
{
Position position = head;
while(position!=NULL){
printf("element is %d\n", position->element);
position = position->Next;
}
}
在main函数中调用此处的函数
#include<stdio.h>
#include<string.h>
#include<sys/time.h>
#include<stdlib.h>
#include<errno.h>
#include"list.h"
static Position CreateList1()
{
List head = CreateHead();
Position position = NULL;
position = CreateListCell(head, 1);
position = CreateListCell(position, 2);
position = CreateListCell(position, 3);
position = CreateListCell(position, 4);
position = CreateListCell(position, 5);
return head;
}
static Position CreateList2()
{
List head = CreateHead();
Position position = NULL;
position = CreateListCell(head, 3);
position = CreateListCell(position, 4);
position = CreateListCell(position, 6);
position = CreateListCell(position, 8);
position = CreateListCell(position, 9);
return head;
}
int main()
{
List head1 = CreateList1();
List head2 = CreateList2();
OverViewList(head1);
printf("\n");
OverViewList(head2);
printf("\n");
return 0;
}
element is 0
element is 1
element is 2
element is 3
element is 4
element is 5
element is 0
element is 3
element is 4
element is 6
element is 8
element is 9
运行的结果如下
工作中在解析带宽log的时候用到了单链表
https://github.com/feifeiyuan/features/tree/master/C/PTM%E7%9A%84%E5%B8%A6%E5%AE%BDlog%E8%A7%A3%E6%9E%90
使用二级指针不再单独创建头结点
Position CreateListCell(List *ahead, NodeElement element){
if(*ahead==NULL){
*ahead = (Position)malloc(sizeof(struct Node));
if(*ahead==NULL){
fprintf(stderr, "there is no space\n");
return NULL;
}
(*ahead)->element = 0;
(*ahead)->Next = NULL;
printf("enter here \n");
}
Position position = NULL;
position = (Position)malloc(sizeof(struct Node));
if(position==NULL){
fprintf(stderr, "there is no space\n");
return NULL;
}
position->element = element;
(*ahead)->Next = position;
position->Next = NULL;
return position;
}
添加元素到尾结点
static int AddToTail(List *head, int value)
{
List new_node = NULL;
new_node = (List)malloc(sizeof(*new_node));
if(new_node==NULL){
fprintf(stderr, "there is no space\n");
return ERROR;
}
new_node->element = value;
new_node->Next = NULL;
if(*head==NULL){
*head = new_node;
return 0;
}
List ptr = *head;
//printf("ptr->element is %d\n", (ptr->Next)->element);
while(ptr!=NULL){
if(ptr->Next==NULL){
ptr->Next = new_node;
break;
}
ptr = ptr->Next;
printf("ptr->element is %d\n", ptr->element);
}
return 0;
}
删除链表元素
这次成熟很多,至少考虑了链表中包含多个需要删除的元素,以及需要删除的是head节点的问题
static int RmNode(List *head, int value)
{
if(*head==NULL){
return ERROR;
}
if((*head)->element==value){
*head = (*head)->Next;
}
List node = *head;
while(node!=NULL&&node->Next!=NULL){
if(node->Next->element==value){
List del_node = node->Next;
node->Next = node->Next->Next;
free(del_node);
del_node = NULL;
}
node = node->Next;
}
return 0;
}
从尾到头开始打印数组
static int FromTailToHead(List head)
{
if(head!=NULL){
FromTailToHead(head->Next);
printf("%s: %d\n", __func__, head->element);
}
return 0;
}
但是递归实际上面临一个问题就是当元素很多的时候会出现调用函数溢出的情况。
既然是先进后出,就想到了数据结构栈,使用C++的数据结构
static void PrintTail(List head)
{
std::stack<List> node;
Position position = head;
while(position!=NULL){
node.push(position);
position = position->Next;
}
printf("yuanhui\n");
while(!node.empty()){
position = node.top();
printf("%s:%d\n", __func__, position->element);
node.pop();
}
return;
}
这样会感觉更好。