C: create, insert, traverse, find, delete, sort, destroy

Posted by BEFOR on Fri, 01 Nov 2019 04:43:38 +0100

C: create, insert, traverse, find, delete, sort, destroy

I. significance of the existence of two-way circular list

Array structure provides continuous memory access and use. Linked list is an effective organization and use of memory fragmentary space. Bidirectional circular linked list increases the freedom of access.

II. Definition of nodes

typedef struct node
{
	int data;
	struct node *pre;
	struct node *next;
}Node;

Three, implementation

Once the one-way list is mastered, it is easy to learn the two-way circular list.

1. Create (i.e. create an empty linked list)
Node * createList()
{
	Node * head = (Node *)malloc(sizeof(Node));
	if (NULL == head)
		exit(-1);
	head->pre = head;
	head->next = head;
	return head;
}

2. Insertion (head insertion)
void insertList(Node* head,int input)
{
	Node * ptr = (Node *)malloc(sizeof(Node));
	if (NULL == ptr)
		exit(-1);
	ptr->data = input;
	ptr->next = head->next;
	ptr->pre = head;
	head->next = ptr;
	ptr->next->pre = ptr;
}

3. traversal

Both forward and reverse are OK

void traverseNList(Node* head)
{
	Node* tmp = head->next;
	while (tmp != head)
	{
		printf("%-3d", tmp->data);
		tmp = tmp->next;
	}
}
void traversePList(Node* head)
{
	Node* tmp = head->pre;
	while (tmp != head)
	{
		printf("%-3d", tmp->data);
		tmp = tmp->pre;
	}
}
4. asking for long
int lengthList(Node* head)
{
	int len = 0;
	Node* tmp = head->next;
	while (tmp != head)
	{
		len++;
		tmp = tmp->next;
	}
	return len;
}
5. search
Node * findDataList(Node* head, int findData)
{
	Node* fnext=head->next, *fpre = head->pre;
	while (fpre != fnext->pre)
	{
		if (findData == fnext->data)
			return fnext;
		if (findData == fpre->data)
			return fpre;
		if (fnext == fpre)
			break;
		fnext = fnext->next;
		fpre = fpre->pre;
	}
	return NULL;
}

The following analysis shows that the end lookup cannot be found:
(1) the number of linked list nodes searched is even:

(2) the number of linked list nodes searched is odd:

6. delete
void deleteDataList(Node *pfind)
{
	pfind->pre->next = pfind->next;
	pfind->next->pre = pfind->pre;
	free(pfind);
}

7. ranking

Bubble sort

void sortList(Node *head)
{
	int N=lengthList(head);
	Node *p,*q;
	for (int i = 0; i < N - 1; i++)
	{
		p = head->next;
		q = p->next;
		for (int j = 0; j < N - i - 1; j++)
		{
			if (p->data > q->data)
			{
				p->pre->next = q;
				q->pre = p->pre;
				p->next = q->next;
				p->pre = q;
				q->next = p;
				p->next->pre = p;
				q = p->next;
				continue;
			}
			p = p->next;
			q = q->next;
		}
	}
}

When an exchange is required:

8. destruction

Direct head - > pre - > next = null; destroy the linked list structure and make it a one-way linked list.

void destroyList(Node * head)
{
	head->pre->next=NULL;
	Node *tmp;
	while (head)
	{
		tmp = head;
		head = head->next;
		free(tmp);
	}
}

Four. Synthesis

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct node
{
	int data;
	struct node *pre;
	struct node *next;
}Node;

Node * createList();
void insertList(Node* head,int input);
void traverseNList(Node* head);
void traversePList(Node* head);
int lengthList(Node* head);
Node * findDataList(Node* head,int findData);
void deleteDataList(Node *pfind);
void sortList(Node *head);
void destroyList(Node * head);

int main()
{
	srand(time(NULL));
	Node * head = createList();
	for (int i = 0; i < 10; i++)
	{
		insertList(head,rand()%100);
	}
	printf("create and insert the list:\n");
	traverseNList(head);
	//traversePList(head);
	int listLen = lengthList(head);
	printf("\nlength of the list:%d\n",listLen);
	Node *flist = findDataList(head,0);
	if (flist)
	{
		printf("find!delete data 0\n");
		deleteDataList(flist);
		traverseNList(head);
	}
	else
		printf("find none!\n");
	printf("\nsort the list:\n");
	sortList(head);
	traverseNList(head);
	destroyList(head);
	
	return 0;
}
Node * createList()
{
	Node * head = (Node *)malloc(sizeof(Node));
	if (NULL == head)
		exit(-1);
	head->pre = head;
	head->next = head;
	return head;
}
void insertList(Node* head,int input)
{
	Node * ptr = (Node *)malloc(sizeof(Node));
	if (NULL == ptr)
		exit(-1);
	ptr->data = input;
	ptr->next = head->next;
	ptr->pre = head;
	head->next = ptr;
	ptr->next->pre = ptr;
}
void traverseNList(Node* head)
{
	Node* tmp = head->next;
	while (tmp != head)
	{
		printf("%-3d", tmp->data);
		tmp = tmp->next;
	}
}
void traversePList(Node* head)
{
	Node* tmp = head->pre;
	while (tmp != head)
	{
		printf("%-3d", tmp->data);
		tmp = tmp->pre;
	}
}
int lengthList(Node* head)
{
	int len = 0;
	Node* tmp = head->next;
	while (tmp != head)
	{
		len++;
		tmp = tmp->next;
	}
	return len;
}
Node * findDataList(Node* head, int findData)
{
	Node* fnext=head->next, *fpre = head->pre;
	while (fpre != fnext->pre)
	{
		if (findData == fnext->data)
			return fnext;
		if (findData == fpre->data)
			return fpre;
		if (fnext == fpre)
			break;
		fnext = fnext->next;
		fpre = fpre->pre;
	}
	return NULL;
}
void deleteDataList(Node *pfind)
{
	pfind->pre->next = pfind->next;
	pfind->next->pre = pfind->pre;
	free(pfind);
}
void sortList(Node *head)
{
	int N=lengthList(head);
	Node *p,*q;
	for (int i = 0; i < N - 1; i++)
	{
		p = head->next;
		q = p->next;
		for (int j = 0; j < N - i - 1; j++)
		{
			if (p->data > q->data)
			{
				p->pre->next = q;
				q->pre = p->pre;
				p->next = q->next;
				p->pre = q;
				q->next = p;
				p->next->pre = p;
				q = p->next;
				continue;
			}
			p = p->next;
			q = q->next;
		}
	}
}
void destroyList(Node * head)
{
	head->pre->next=NULL;
	Node *tmp;
	while (head)
	{
		tmp = head;
		head = head->next;
		free(tmp);
	}
}

Topics: Programming