队列

早上写了下队列,越发觉得ddd的强大,但linux上面没有明显的高亮,而且代码提示补全功能没windows那么强大。。。

[code]
#include <stdio.h>
#include <stdlib.h>

typedef int ElemType;
typedef int Stauts;

typedef struct QNode{
ElemType data;
struct QNode *next;
}QNode,*QNodePtr;

typedef struct QueueLink{
QNodePtr front;
QNodePtr rear;
}QLink,*QLinkPtr;

void prinf();
void DisplayQueue(QLinkPtr Q);//Print Queue
Stauts InitQNode(QNodePtr *Node);//Initial Node of Queue
Stauts InitQLink(QLinkPtr Q);//Create a empty Quue
Stauts InsertQLink(QLinkPtr Q,ElemType e);//insert into Queue
Stauts DeleteQLink(QLinkPtr Q,ElemType *e);//out Queue
void DestoryLink(QLinkPtr Q); //clear Queue

int main()
{
int iChoice,i;
QLink Q;
if(!InitQLink(&Q)) //initial Queue
return 0;
for(i = 1; i < 10;++i)//for debug,Insert Queue from 1 to 100;
InsertQLink(&Q,i);
DisplayQueue(&Q); //print Queue

while(1)
{
prinf();
scanf("%d",&iChoice); //choose
switch(iChoice)
{
case 1:
{
int data;
printf("Iuput data:\n");
scanf("%d",&data);
if(InsertQLink(&Q,data)) //insert node to queue
{
DisplayQueue(&Q);
}
break;
}
case 2:
{
ElemType e;
if(DeleteQLink(&Q,&e)) //delete node from queue
{
DisplayQueue(&Q);
}
break;
}
case 3:
printf( "Node of The Stack is %d\n" , Q.front->data); //length of queue
break;
case 4:
DestoryLink(&Q); //clear queue
return 0 ;
default :
break;
}
}
return 0;
}
Stauts InitQNode(QNodePtr *Node) //Initial Node of Queue
{
*Node = (QNodePtr )calloc(1,sizeof(QNode));
if(!*Node)
{
printf("Allocate Memory Error");
return 0;
}
(*Node)->data = 0;
(*Node)->next = NULL;
return 1;
}

Stauts InitQLink(QLinkPtr Q) //create an empty queue
{
Q->front = (QNodePtr)calloc(1,sizeof(QNode));
if(!Q->front)
{
printf("Allocate Memory Error");
return 0;
}
Q->rear = Q->front ;
return 1;
}

Stauts InsertQLink(QLinkPtr Q,ElemType e) //insert into Queue
{
QNodePtr QNode_New = NULL;
if(!InitQNode(&QNode_New)) //initial new node
return -1;
QNode_New->data = e;
Q->rear->next = QNode_New;
Q->rear = QNode_New;
Q->front->data++;
return 1;
}

Stauts DeleteQLink(QLinkPtr Q,ElemType *e ) //out Queue
{
QNodePtr p= NULL;
if(Q->rear == Q->front)
{
printf("Empry Queue\n");
return 0;
}
p = Q->front->next;
Q->front->next = p->next;
Q->front->data--;
if(p == Q->rear)
{
Q->front = Q->rear;
return 1;
}
p->next = NULL;
free(p);
p = NULL;
return 1;
}

void DestoryLink(QLinkPtr Q) //clear queue
{
while(Q->front)
{
Q->rear = Q->front->next;
free(Q->front);
Q->front = Q->rear;
}
printf("Empty Queue\n" );
return ;
}

void DisplayQueue(QLinkPtr Q)
{
if(Q->rear == Q->front)
{
printf("Empty Queue\n" );
return ;
}
int i = 0;
QNodePtr pTemp = Q->front->next;
while(pTemp)
{
++i;
printf("No.%d Node : %d \n",i,pTemp->data);
pTemp = pTemp->next;
}
}

void prinf()
{
printf ("1:Insert Node\n");
printf ("2:delete Node\n");
printf ("3:Length of Queue\n");
printf ("4:Destory Queue");
}

[/code]

标签:DataStructure