#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *link;
};
void addcll(struct node **, struct node **, int);
void displaycll(struct node *);
void main()
{
struct node *front, *rear;
front = rear = NULL;
addcll(&front, &rear, 56);
addcll(&front, &rear, 12);
displaycll(front);
}
void addcll(struct node **f, struct node **r, int item) {
struct node *q;
q = malloc(sizeof(struct node));
q->data = item;
if (*f == NULL) {
*f = q;
}
else {
(*r)->link = q;
(*r) = q;
(*r)->link = *f;
}
}
void displaycll(struct node *f) {
struct node *q, *p;
q = f;
p = NULL;
while (q != p) {
printf("%d \n", q->data);
q = q->link;
p = f;
}
}
Unable to figure out exactly where is the error please help. Also I am beginner to data structures . Please give me some suggestion of book or online resources where I can learn and practice these concepts
Your addcll
is wrong. the rear pointer should always be reset on a queue back-push, and the link of the node pushed should always be set to the content of front
, even in the case when the first node is added, btw.
void addcll(struct node **f, struct node **r, int item)
{
struct node *q = malloc(sizeof(struct node));
q->data = item;
if (*f == NULL) {
*f = q;
}
else {
(*r)->link = q;
}
(*r) = q; // here
(*r)->link = *f; // and here.
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.