Cyclic linked lists-C_ data structure

Source: Internet
Author: User
#define NULL ((void *) 0) #define MALLOC (DataType, N) MALLOC (sizeof (dataType) *n) #define SCANF (PVal, Val) SCANF ( "%d", &val) #define FREE (point) #define EXIT (Val) exit (val) typedef struct node{int da
	Ta 
struct Node *pnext;

}node_t, *pnode_t;
pnode_t create_loop_list (void);
void Traverse_loop_list (pnode_t phead);
BOOL Insert_loop_list (pnode_t phead, int pos, int val);
BOOL Delete_loop_list (pnode_t phead, int pos, int *val);
void Sort_loop_list (pnode_t phead, int length);


int Loop_list_length (pnode_t phead);
	int main (void) {int val;
	pnode_t Phead;
	
  Phead = Create_loop_list ();
	Insert_loop_list (Phead, 1, 22);
	Insert_loop_list (Phead, 1, 44);
	Insert_loop_list (Phead, 1, 55);
	Insert_loop_list (Phead, 1, 66);
	Insert_loop_list (Phead, 1, 33);
	Insert_loop_list (Phead, 1, 77);
	Insert_loop_list (Phead, 1, 88);

  Insert_loop_list (Phead, 1, 99);
	Traverse_loop_list (Phead);
	
	Sort_loop_list (Phead, Loop_list_length (Phead)); Traverse_loop_liSt (Phead);
	if (Delete_loop_list (Phead, 1, &val)) {printf ("Delete Success, val =%d\n", Val);
	}else{printf ("Delete fail!\n");
	} if (Delete_loop_list (Phead, 6, &val)) {printf ("Delete Success, val =%d\n", Val);
	}else{printf ("Delete fail!\n");
	} if (Delete_loop_list (Phead, 2, &val)) {printf ("Delete Success, val =%d\n", Val);
	}else{printf ("Delete fail!\n");
	
	} traverse_loop_list (Phead);
return 0;
	
	} pnode_t create_loop_list (void) {pnode_t phead = (pnode_t) MALLOC (node_t, 1);
		if (NULL = = Phead) {printf ("Allocation fail!\n");
	EXIT (-1);
		}else{pnode_t ptail = (pnode_t) MALLOC (node_t, 1);
			if (NULL = = Phead) {printf ("Allocation fail!\n");
		EXIT (-1);
			}else{ptail->data = 0;
			Ptail->pnext = Phead;
		Phead->pnext = Ptail;
	return phead;
	} void Traverse_loop_list (pnode_t phead) {pnode_t p = phead->pnext;
	printf ("Traverse result:\n");
		while (P!= phead) {printf ("%xh:%d\n", p,p->data);
	p = p->pnext;
	}Return
	int Loop_list_length (pnode_t phead) {int cnt = 0;
	
	pnode_t p = phead->pnext;
		while (P!= phead) {p = p->pnext;
	cnt++;

} return CNT;
	Insert a new node in front of the node in the//pos position, POS greater than 0 bool Insert_loop_list (pnode_t phead, int pos, int val) {int i = 0;

	pnode_t p = phead;
		while ((P->pnext!= phead) && (i < pos-1)) {p = p->pnext;
	i++;
	}//printf ("-Insert_loop_list-%xh, i:%d, pos:%d\n", P,i,pos); if ((P->pnext = = Phead) | | (i > Pos-1))
	
	return false;
	pnode_t pnew = (pnode_t) MALLOC (node_t, 1);
	Pnew->data = val;

	Pnew->pnext = p->pnext;

	P->pnext = pnew;
return true;
	//delete the node at POS position in the cyclic list, assign the data field value of the node to (*val) bool Delete_loop_list (pnode_t phead, int pos, int *val) {int i = 0;

	pnode_t p = phead;
		while ((P->pnext!= phead) && (i < pos-1)) {p = p->pnext;
	i++; } if ((P->pnext = = Phead) | | (i > Pos-1))

	return false;
	
	pnode_t Pbuff = p->pnext;
	(*val) = p->pnext->data; P->pnext = P-&GT;PNEXT-&GT;pnext;

	Free (pbuff);
return true;
	BOOL Is_empty (pnode_t phead) {if (NULL = = Phead->pnext) return true;
else return false;
	} void Sort_loop_list (pnode_t phead, int length) {if (Is_empty (Phead)) {printf ("List is empty!\n");
		}else{int I, J, T;
		pnode_t p, q; For (i=0,p=phead->pnext; i<length-1; I++,p=p->pnext) {for (j=i+1,q=p->pnext; j<length; j++,q=q->pNe
					XT) {if (P->data > Q->data) {t = p->data;
					P->data = q->data;
				Q->data = t;
}}} return;
 }

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.