#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node*next;
};
struct Node*createNode(int data)
{
struct Node*newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data=data;
newNode->next=NULL;
return newNode;
}
struct List
{
struct Node* frontNode; /*表头*/
struct Node* tailNode;/*表尾*/
int size; /*链表的大小*/
}
struct List*createList()
{
struct List *list = (struct List*)malloc(sizeof(struct List));
list->frontNode = list->frontNode=NULL;
list->size = 0; /*一开始链表大小为空*/
return list;
}
/*以插入头部的方式插入*/
void insertNodeByHead(struct List, int data)
{
struct Node* newNode =createNode(data);
if(list->size ==0)/*大小为空,即是表尾又是表头*/
list->tailNode = newNode;
else
newNode->next=list->frontNode;
list->frontNode = newNode;
list->size++;/*插入了一个节点,大小加一*/
}
/*表尾法插入*/
void insertNodeByTail(struct List*list,int data)
{
struct Node*newNode = createNode(data);
if(list->size==0)
{
list->frontNode = newNode;
list->tailNode=newNode;
}
else
{
list->tailNode->next = newNode;
/*原表尾的下一个节点为newNode,新表尾为newNode*/
list->tailNode = newNode;
}
list->size++;
}
void printList(struct List*list)
{
struct Node*pMove = list->frontNode;
while(pMove)
{
printf("%d",pMove->data);
pMove=pMove->next;
}
printf("\n");
}
int main()
{
/*打印和前面的一样*/
system("pause");
return 0;
}
无头链表结构体封装
最新推荐文章于 2022-05-17 18:50:38 发布