(C language Edition) list (d)--realize bidirectional cyclic list creation, insert, delete, free memory, etc. simple Operation __c language

Source: Internet
Author: User

Two-way circular chain list is based on the two-way linked list of the implementation, and the two-way linked list of operations, the only difference is that it is a circular list, through each node of the two pointers to the knot together to form a ring. So, each node has a precursor node and a successor node (both the head node and the tail node) which is different from the two-way list. We look at the map of the two-way circular chain (I found a picture on the internet, my painting is really ugly, there is time to really learn how to paint, and then can write a better blog):

In the programming aspect of the program, the two-way circular chain table a bit to the previous knowledge of the combination of two-way cycle linked list in the "two-way" aspect and the two-way linked list, in the "circular" and one-way circular chain table. Before the knowledge of digestion, this two-way cycle linked list is also simple. The above image shows the meaning of the bidirectional cyclic chain list: Simply put, a pointer to the predecessor node, one pointer to the successor node, each node repeats, and a loop is formed.

DbCcLinkList.h header file--contains the definition of the node structure and the declaration of the related operation function of the linked list

#ifndef double_circular_linked_list_h
#define DOUBLE_CIRCULAR_LINKED_LIST_H

typedef struct NODE
{
	int data;
	struct Node *pnext;
	struct Node *ppre;
} NODE, *pnode;

Create bidirectional cyclic chain list
pnode createdbcclinklist (void);

Print List
void Traversedbcclinklist (Pnode phead);

Determine if the linked list is an empty
int isemptydbcclinklist (Pnode phead);

Calculate the length of the linked list
int getlengthdbcclinklist (Pnode phead);

Inserts a node
int inserteledbcclinklist (pnode phead, int pos, int data) into the list;

Deletes the node
int deleteeledbcclinklist (pnode phead, int pos) from the linked list;

Deletes the entire list, freeing the memory
void Freememory (Pnode *pphead);

#endif


The source file of DbCcLinkList.cpp bidirectional cyclic chain List--contains the definition of related operation function of linked list

(1) This part is used to create a linked list, two-way cyclic linked list control 4 pointers for each node inserted, first, the previous node of the insertion position has a pointer to the insertion node, and second, the inserted node has two pointers, one pointing to the previous node, one pointing to the next node, and the third, The next node at the insertion point has a pointer to the insertion node. The key to writing a program is to control the four pointers, do not make a mistake, or there will be strange results (program out of the results, wireless loop, etc.)

#include <stdio.h>
#include <stdlib.h>
#include "DbCcLinkList.h"

//Create bidirectional cyclic link list
pnode Createdbcclinklist (void)
{
	int i, length = 0, data = 0;
	Pnode p_new = null, Ptail = NULL;
	Pnode Phead = (pnode) malloc (sizeof (NODE));

	if (NULL = = Phead)
	{
		printf ("Memory allocation failed.") \ n ");
		Exit (exit_failure);
	}
	Phead->data = 0;
	Phead->pnext = Phead;
	Phead->ppre = Phead;
	Ptail = Phead;

	printf ("Please enter the length of the list you want to create:");
	scanf ("%d", &length);

	For (I=1 i<length+1; i++)
	{
		p_new = (pnode) malloc (sizeof (NODE));

		if (NULL = = p_new)
		{
			printf ("Memory allocation failed.") \ n ");
			Exit (exit_failure);
		}

		printf ("Please enter the%d node element value:", i);
		scanf ("%d", &data);

		P_new->data = data;
		P_new->ppre = Ptail;
		P_new->pnext = Phead;
		Ptail->pnext = p_new;
		Phead->ppre = p_new;
		Ptail = p_new;
	}
	return phead;
}
(2) This part is to obtain the information of the two-way linked list, and the one-way circular chain table, the limit of the end of the linked list is to determine whether equal to the head node. It means that the next node in the head node begins.

Print List
void Traversedbcclinklist (Pnode phead)
{
	Pnode pt = phead->pnext;

	printf ("List printing as:");
	while (PT!= phead)
	{
		printf ("%d", pt->data);
		PT = pt->pnext;
	}
	Putchar (' \ n ');
}

Determine if the list is empty
int isemptydbcclinklist (pnode phead)
{
	Pnode pt = phead->pnext;

	if (pt = = Phead) return
		1;
	else return
		0;
}

Calculates the length of the list
int getlengthdbcclinklist (pnode phead)
{
	int length = 0;
	Pnode pt = phead->pnext;

	while (PT!= phead)
	{
		length++;
		PT = pt->pnext;
	}
	return length;
}

(3) This part is the bidirectional linked list inserts, deletes the node operation, but it does not need to be the same as a two-way list to determine whether the last node is empty (because at this point the pointer to the node), the two-way circular chain list does not exist, because it is not only two-way, but also circular, so each node can not be empty.

Inserts a node
int inserteledbcclinklist (pnode phead, int pos, int data) to the list
{
	Pnode p_new = null, pt = NULL;
	if (pos > 0 && pos < getlengthdbcclinklist (phead) + 2)
	{
		p_new = (pnode) malloc (sizeof (NODE));

		if (NULL = = p_new)
		{
			printf ("Memory allocation failed.") \ n ");
			Exit (exit_failure);
		}

		while (1)
		{
			pos--;
			if (0 = = pos) break
				;
			Phead = phead->pnext;
		}
		
		P_new->data = data;
		PT = phead->pnext;
		P_new->pnext = pt;
		P_new->ppre = Phead;
		Phead->pnext = p_new;
		Pt->ppre = p_new;
		
		return 1;
	}
	else return
		0;
}

Deletes the node
int	deleteeledbcclinklist (pnode phead, int pos) from the linked list
{
	Pnode pt = NULL;
	if (pos > 0 && pos < getlengthdbcclinklist (phead) + 1)
	{while
		(1)
		{
			pos--;
			if (0 = = pos) break
				;
			Phead = phead->pnext;
		}
		PT = phead->pnext->pnext;
		Free (phead->pnext);
		Phead->pnext = pt;
		Pt->ppre = Phead;

		return 1;
	}
	else return
		0;	
}

(4) This is the release of memory operations, as mentioned above, do not need to determine whether the last node is empty, each time the memory of the release of a node it is still a ring structure, so no node is empty.

Delete entire list, free memory space
void Freememory (Pnode *pphead)
{
	Pnode pt = NULL;
	while (*pphead!= NULL)
	{
		pt = (*pphead)->pnext->pnext;


		if ((*pphead)->pnext = = *pphead)
		{free
			(*pphead);
			*pphead = NULL;
		}
		else
		{free
			((*pphead)->pnext);
			(*pphead)->pnext = pt;
			Pt->ppre = *pphead;
		}
	}

Maincpp test Program--the function is judged correctly by the simple interactive interface

#include <stdio.h> #include <stdlib.h> #include "DbCcLinkList.h" int main (void) {int flag = 0, length = 0;
	int position = 0, value = 0;

	Pnode head = NULL;
	
	Head = Createdbcclinklist ();
	Flag = Isemptydbcclinklist (head); if (flag) printf ("Two-way circular chain list is empty.")
	\ n ");
		else {length = Getlengthdbcclinklist (head);
		printf ("The length of the two-way cyclic list is:%d\n" and "long");
	Traversedbcclinklist (head);
	printf ("Enter the location of the node to be inserted and the value of the element (two digits separated by a space):");
	scanf ("%d%d", &position, &value);
	Flag = Inserteledbcclinklist (head, position, value); if (flag) {printf ("Insert node succeeded.")
		\ n ");
	Traversedbcclinklist (head); else printf ("Insert node failed.")
	
	\ n ");
	Flag = Isemptydbcclinklist (head); if (flag) printf ("The two-way circular list is empty, the delete operation cannot be done.")
	\ n ");
		else {printf ("Enter the location where you want to delete the node:");
		scanf ("%d", &position);
		Flag = Deleteeledbcclinklist (head, position); if (flag) {printf ("Delete node succeeded.)
			\ n ");
		Traversedbcclinklist (head); else printf (delete node failed.)
	\ n ");
	} freememory (&head); if (NULL = head) printf ("successfully deletedBidirectional loop linked list, free memory complete.
	\ n "); Else printf ("Failed to delete the bidirectional cyclic list, freeing memory is not complete.")

	\ n ");
return 0; }

PS: So far, the knowledge of the list is written here, the beginning of the queue and stack. In fact, the knowledge of the linear table is similar, as long as the principle is clear, its form of change can be more than a variety of, of course, my level is limited, I hope and fellow people to continue in-depth discussion and common progress.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.