Queue implementation (two-way cyclic linked list C ++), queue
The queue is very simple, but it may be better to use arrays .. (In fact, I think it is more difficult to use arrays in multiple queues)
Then I wrote a two-way circular linked list for the first time. Too messy.
I established a head node and a tail Node Based on my own ideas, and then inserted them into two nodes in the queue order. When the output and pop-up queues are displayed, perform subsequent operations.
The code below:
//// Main. cpp // queue // Created by Alps on 14-7-28. // Copyright (c) 2014 chen. all rights reserved. // # include <iostream> # define ElementType intusing namespace std; struct Node; typedef Node * PtrToNode; typedef PtrToNode Queue; struct Node {ElementType X; PtrToNode Pre; PtrToNode Next ;}; queue createQueue (void) {Queue Q; Queue Q2; Q2 = (Queue) malloc (sizeof (Queue); Q-> X = 0; Q-> Next = Q2; Q-> Pre = Q2; Q2-> Next = Q; Q2-> Pre = Q; return Q ;} int isEmpty (Queue Q) {return Q-> Next = Q;} void Queue (Queue Q, ElementType element) {Queue tmp; Queue tmp1; tmp1 = (Queue) malloc (sizeof (Queue); // Queue switchTmp; tmp = (Queue) malloc (sizeof (Queue); tmp-> X = element; tmp-> Next = Q-> Next; Q-> Next-> Pre = tmp; Q-> Next = tmp; tmp-> Pre = Q ;} void outQueue (Queue Q) {Queue Tmp; tmp = Q-> Pre; Q-> Pre = tmp-> Pre; tmp-> Pre-> Next = Q-> Pre; free (tmp);} ElementType headQueue (Queue Q) {if (Q = NULL) {printf ("please create queue first! \ N "); return 0;} if (! IsEmpty (Q) {return Q-> Pre-> X;} else {printf ("The queue is empty! \ N "); return 0 ;}} int makeEmpty (Queue Q) {if (Q = NULL) {printf (" please create queue first! \ N "); return-1;} while (! IsEmpty (Q) {outQueue (Q);} return 0;} void Print (Queue Q) {if (! IsEmpty (Q) {Queue tmp = Q-> Pre; while (tmp! = Q) {printf ("% d", tmp-> X); tmp = tmp-> Pre;} printf ("\ n ");}} int main (int argc, const char * argv []) {Queue Q = createQueue (); if (isEmpty (Q) {printf ("The queue is empty! \ N ");} else {printf (" The queue is not empty! \ N ");} queue Queue (Q, 2); queue Queue (Q, 4); queue Queue (Q, 6); Print (Q); outQueue (Q ); print (Q); makeEmpty (Q); Print (Q); // printf ("% d \ n", headQueue (Q); return 0 ;}
This code is messy. In fact, a single-chain table can also be used, but that operation is not O (1), so a double-chain table is used.
How do I define a two-way linked list in C? One-way linked list? Queue?
Do you know what a two-way linked list, one-way linked list, and queue are?
If you don't know, you don't know me even if I write it to you.
Write a program in C language, create a two-way circular linked list, and implement its insert and delete operations
Hope smile ~
// CreateList_L.cpp
// To create a LinkList
# Include <stdlib. h>
# Include <iostream. h>
# Include <conio. h>
# Define TRUE 1
# Define FALSE 0
# Define OK 1
# Define ERROR 0
# Define INFEASIBLE-1
# Define OVERFLOW-2
Typedef struct DuLNode
{Int data;
Struct DuLNode * prior;
Struct DuLNode * next;
} DuLNode, * DuLinkList;
// Initial condition: L already exists. Operation Result: returns the number of data elements in L.
Int ListLength (DuLinkList L)
{
Int I = 0;
DuLinkList p = L-> next; // p points to the first node
While (p! = L) // p does not reach the header.
{
I ++;
P = p-> next;
}
Return I;
}
// Starting from the header node of the double-stranded loop linear table L, each data element is output in the forward order.
Void ListTraverse (DuLinkList L)
{
DuLinkList p = L-> next;
While (p! = L)
{
Cout <p-> data <"\ t ";
P = p-> next;
}
Cout <endl;
}
// Starts from the header node of the double-chain cyclic linear table L and outputs each data element in reverse order.
Void ListTraverseBack (DuLinkList L)
{
DuLinkList p = L-> prior;
While (p! = L)
{
Cout <p-> data <"\ t ";
P = p-> prior;
}
Cout <endl;
}
// To Creatre a DuLinkList L with HeadNode
Void CreateList_DuL (DuLinkList & L)
{
Int n;
Int I;
DuLNode * p;
L = (DuLinkList) malloc (sizeof (DuLNode ));
L-> next = L-> prior = L;
Cout <"CreateList_L" <endl <"==================" <endl;
Cout <"Please input the Init DuLinkNode Number: <eg. 5> ";
Cin> n;
Cout <"Please input th... the remaining full text>