C language linked list

C language linked list

**Node:**Each space adopts dynamic data allocation, each data space saves one data, and the number of data spaces can be allocated according to requirements, and each data space is called a node. In each node structure, we define a member to store the first address of the next node, which is used to store the members of the next node. This is called a pointer field
** linked list: ** The pointer field of the first node stores The first address of the next node, and so on to the last node, and the pointer field of the last node points to Null.

A simple diagram of a linked list

Sogou screenshot 20180428155516

The 0th node is called the head node. There is no data, only the first address of the first node is stored.
Each node is the same data structure

struct Node{
	int data;
	struct student *next;
}Node,*PNode;

The basic operation of linked list:

  • creation of linked list
  • Judgment of linked list
  • Get the length of the linked list
  • Insertion of linked list
  • Deletion of linked list
  • traversal of linked list
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<stdbool.h>

#define LEN sizeof(struct Node)

typedef struct Node{
	int data;
	struct Node *pnext;
}Node, *PNode;

/*创建链表:pHead为头结点,pTail为尾结点,pNew为新创建的结点*/
PNode creatList(void){
	PNode pHead, pTail, pNew;
	int n, i;
	pHead = (PNode)malloc(LEN);
	pHead->pnext = NULL;
	pTail = pHead;
	printf("请输入链表的长度\n");
	scanf("%d",&n);
	for(i = 0; i < n; i++){
		pNew = (PNode)malloc(LEN);
		pNew->pnext = NULL;
		printf("请输入第%d个链表的值\n", i+1);
		scanf("%d", &pNew->data);
		pTail->pnext = pNew;
		pTail = pNew;		
	}
	return pHead;
}

/*链表的判断:判断链表是不是为空*/
bool isempty(PNode pHead){
	if(pHead->pnext == NULL)
		return true;
	else return false;
}

/*链表的长度:获取链表的长度*/
int list_num(PNode pHead){
	int num = 0;
	PNode p;
	p = pHead->pnext;
	while(p != NULL){
		num+=1;
		p = p->pnext;
	}
	return num;
}

/*链表的遍历:输出链表所有的数据*/
void traversal(PNode pHead){
	PNode p;
	p = pHead->pnext;
	while(p != NULL){
		printf("%d\n", p->data);
		p = p->pnext;
	}
}

/*链表的插入:插入的位置和数据需要提供*/
bool insert(PNode pHead, int val, int pos){
	PNode p, pNew;
	int i = 1;
	p = pHead->pnext;
	while(p != NULL){
		if(i == pos){
			pNew = (PNode)malloc(sizeof(LEN));
			pNew->data = val;
			pNew->pnext = p->pnext;
			p->pnext = pNew;
			printf("插入成功\n");
			return true;
		}
		p = p->pnext;
		i++;
	}
	printf("插入位置超过链表实际大小\n");
	return false;
}

/*链表的删除:*/
bool delete(PNode pHead, int pos){
	int i;
	PNode p, tmp;
	p = pHead;
	for(i=1; p!=NULL && i < pos; i++)
		p = p->pnext;
	if(p == NULL){
		printf("删除错误\n");
		return false;
	}
	tmp = p;
	p = p->pnext;
	tmp->pnext = p->pnext;
	free(p);
	return true;
}

int main(int argc, char const *argv[])
{
	PNode pHead;
	pHead = creatList();
	if(isempty(pHead))
		printf("链表为空\n");
	printf("链表的长度为:%d\n",list_num(pHead));
	traversal(pHead);
	//insert(pHead,55,1);
 	delete(pHead,2);
	traversal(pHead);
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325165499&siteId=291194637