我试图创建出列结构来插入/删除第一个节点,插入/删除最后一个节点。我已经在一个.c文件中写了所有内容并且它工作得很好,但是当我尝试从中创建一个项目文件时(3个文件:main,header,functions),它只是不起作用。制作项目后遇到的第一个问题:打印无限次出列(函数void printDeque(deque *d)
)。
这是工作的单文件代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct dequeNode *link;
struct dequeNode{
int data;
link next;
};
typedef struct{
link front;
link rear;
}deque;
void initDeque(deque *d){ //Create new deque
d->front=NULL;
d->rear=NULL;
}
int isEmpty(deque *d){ //Function to find, if deque is empty
if((d->front)==NULL){
return 0;
}else{
return 1;
}
}
void putFront(deque *d,int x){ //Function to insert data in front of deque
link node=(link)malloc(sizeof(struct dequeNode));
node->data=x;
node->next=NULL;
if(isEmpty(d)){
node->next=d->front;
d->front=node;
}else{
d->front=node;
d->rear=node;
}
}
void putRear(deque *d,int x){ //Function to insert data in rear of deque
link node=(link)malloc(sizeof(struct dequeNode));
node->data=x;
node->next=NULL;
if(isEmpty(d)){
d->rear->next=node;
d->rear=node;
}else{
d->front=node;
d->rear=node;
}
}
int getFront(deque *d){ //Delete first node of deque
link node=d->front;
int x=node->data;
if((d->front)==(d->rear)){
d->front=NULL;
d->rear=NULL;
}else{
d->front=node->next;
}
free(node);
return x;
}
int getRear(deque *d){ //Delete last node of deque
link node=d->front;
link tr=d->rear;
int x=tr->data;
if(node==tr){
d->front=NULL;
d->rear=NULL;
}else{
while((node->next)!=tr){
node=node->next;
}
d->rear=node;
node->next=NULL;
}
free(tr);
return x;
}
void printDeque(deque *d){ //Print out deque to console
link node=d->front;
while(node!=NULL){
printf("%d ",node->data);
node=node->next;
}
}
void main(){
int x;
deque *d;
d=(deque*)malloc(sizeof(deque));
initDeque(d);
putFront(d,10);
putFront(d,20);
putRear(d,30);
putFront(d,40);
putRear(d,50);
putFront(d,60);
printf("\n");
printDeque(d);
if(isEmpty(d))
x=getFront(d);
printf("\n");
printDeque(d);
if(isEmpty(d))
x=getRear(d);
printf("\n");
printDeque(d);
if(isEmpty(d))
x=getFront(d);
printf("\n");
printDeque(d);
}
这里有三个文件来制作一个项目:
main.c
#include<stdio.h>
#include<stdlib.h>
#include"deque.h"
void main(){
int x;
deque *d;
d=(deque*)malloc(sizeof(deque));
initDeque(d);
putFront(d,10);
putFront(d,20);
printDeque(d);
putRear(d,30);
putFront(d,40);
putRear(d,50);
putFront(d,60);
printf("\n");
printDeque(d);
if(isEmpty(d))
x=getFront(d);
printf("\n");
printDeque(d);
if(isEmpty(d))
x=getRear(d);
printf("\n");
printDeque(d);
if(isEmpty(d))
x=getFront(d);
printf("\n");
printDeque(d);
}
头文件(deque.h):
#ifndef deque_H
#define deque_H
typedef struct dequeNode *link;
struct dequeNode{
int data;
link next;
};
typedef struct{
link front;
link rear;
}deque;
void initDeque(deque*);
int isEmpty(deque*);
void putFront(deque*,int);
void putRear(deque*,int);
int getFront(deque*);
int getRear(deque*);
void printDeque(deque*);
#endif
和函数文件(functions.c):
#include<stdio.h>
#include<stdlib.h>
#include"deque.h"
void initDeque(deque *d){ //Create new deque
d->front=NULL;
d->rear=NULL;
}
int isEmpty(deque *d){ //Function to find, if deque is empty
if((d->front)==NULL){
return 0;
}else{
return 1;
}
}
void putFront(deque *d,int x){ //Function to insert data in front of deque
link node=(link)malloc(sizeof(struct dequeNode));
node->data=x;
node->next=NULL;
if(isEmpty(d)){
node->next=d->front;
d->front=node;
}else{
d->front=node;
d->rear=node;
}
}
void putRear(deque *d,int x){ //Function to insert data in rear of deque
link node=(link)malloc(sizeof(struct dequeNode));
node->data=x;
node->next=NULL;
if(isEmpty(d)){
d->rear->next=node;
d->rear=node;
}else{
d->front=node;
d->rear=node;
}
}
int getFront(deque *d){ //Delete first node of deque
link node=d->front;
int x=node->data;
if((d->front)==(d->rear)){
d->front=NULL;
d->rear=NULL;
}else{
d->front=node->next;
}
free(node);
return x;
}
int getRear(deque *d){ //Delete last node of deque
link node=d->front;
link tr=d->rear;
int x=tr->data;
if(node==tr){
d->front=NULL;
d->rear=NULL;
}else{
while((node->next)!=tr){
node=node->next;
}
d->rear=node;
node->next=NULL;
}
free(tr);
return x;
}
void printDeque(deque *d){ //Print out deque to console
link node=d->front;
while(node!=NULL){
printf("%d ",node->data);
node=node->next;
}
}
第二个问题:我在deque *d; d=(deque*)malloc(sizeof(deque));
中创建了第一个节点int main
,但是有可能创建它,比如说void initDeque()
吗?
答案 0 :(得分:0)
你遇到的第一个问题 - isEmpty()函数出错。如果为NULL,则应返回true,如果不为NULL - 则应返回false。
UPD。为了保持其余代码不变,您可以将isEmpty重命名为isNotEmpty(假设为isedev)。此外,值得添加检查此函数中“d”指针是否等于NULL:
//Function to find, if deque is not empty
int isNotEmpty(deque *d)
{
if (!d || !d->front)
return 0;
return 1;
}