Queue implementing by linked list in c - c

I am implementing queue with a linked list but I am facing problem in the insertion() function. I am able to insert only one data, whenever I insert another data then again previous data insert whatever did I insert at first time.
#include <stdio.h>
#include <stdlib.h>
struct Queue
{
int data;
struct Queue *next;
};
struct Queue *rear = NULL;
struct Queue *front = NULL;
void insertion(int data)
{
struct Queue *n;
n = (struct Queue *)malloc(sizeof(struct Queue));
n->data = data;
n->next = NULL;
if (rear == NULL)
{
front = n;
rear = n;
}
else
{
rear->next = n;
rear = n;
}
}
void deletion()
{
if (front == NULL)
printf("\n Underflow");
else if (front == rear)
{
front = NULL;
rear = NULL;
}
else
front = front->next;
}
void viewList()
{
struct Queue *t = front;
if (t == NULL)
printf("\n there is no item for view...............");
else
{
while (t != NULL)
{
printf(" %d", front->data);
t = t->next;
}
}
}
int main()
{
struct Queue *q = NULL;
insertion(5);
insertion(10);
// deletion();
viewList();
printf("\n");
viewList();
return 0;
}

The function deletion produces a memory leak because it does not free the memory of the deleted node. The function can look at least like
void deletion() {
if(front == NULL)
printf("\n Underflow");
else
{
stuct Queue *n = front;
front = front->next;
if ( front == NULL ) rear = NULL;
free( n );
}
}
Within the function viewList you are always outputting the value stored in the node pointed to by the pointer front
printf(" %d", front->data);
You have to write
printf(" %d", t->data);
This declaration in main
struct Queue *q = NULL;
is redundant and does not make a sense.
Pay attention to that it is a bad idea when functions depend on global variables. You could define the queue the following way
struct Node
{
int data;
struct Node *next;
};
struct Queue
{
struct Node *front;
struct Node *rear;
};
And in main you could define an object of the queue the following way
struct Queue queue = { .front = NULL, .rear = NULL };
And the functions should be rewritten such a way that they would accept a pointer to the queue. For example
int insertion( struct Queue *queue, int data );
The function can be defined like
int insertion( struct Queue *queue, int data )
{
struct Node *new_node = malloc( sizeof( struct Node ) );
int success = new_node != NULL;
if ( success )
{
new_node->data = data;
new_node->next = NULL;
if ( queue->front == NULL )
{
queue->front = new_node;
}
else
{
queue->rear->next = new_node;
}
queue->rear = new_node;
}
return success;
}
And the function will be called like
int main( void )
{
struct Queue queue = { .front = NULL, .rear = NULL };
insertion( &queue, 5 );
insertion( &queue, 10 );
//...
you can check the returned value of the function to determine whether the insertion was successful as for example.
if ( !insertion( &queue, 5 ) ) puts( "Error. Not enough memory." );
Here is a demonstrative program.
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Queue
{
struct Node *front;
struct Node *rear;
};
int insertion( struct Queue *queue, int data )
{
struct Node *new_node = malloc( sizeof( struct Node ) );
int success = new_node != NULL;
if ( success )
{
new_node->data = data;
new_node->next = NULL;
if ( queue->front == NULL )
{
queue->front = new_node;
}
else
{
queue->rear->next = new_node;
}
queue->rear = new_node;
}
return success;
}
int empty( const struct Queue *queue )
{
return queue->front == NULL;
}
void viewList( const struct Queue *queue )
{
if ( empty( queue ) )
{
printf("\n there is no item for view...............");
}
else
{
for ( struct Node *current = queue->front; current != NULL; current = current->next )
{
printf( " %d", current->data );
}
}
}
int main(void)
{
struct Queue queue = { .front = NULL, .rear = NULL };
insertion( &queue, 5 );
insertion( &queue, 10 );
viewList( &queue );
return 0;
}
The program output is
5 10

Related

Sorting a singly linked list with multiple elements in C [duplicate]

In C I have to write a bubble sort function that swaps the nodes and not the values of a LinkedList, but I'm unable to accomplish it. Here is the code (As you can see the order is not correct):
#include <stdio.h>
#include <malloc.h> // malloc, free
#include <stddef.h> // NULL
// defining 'int' as data_t
typedef int data_t;
typedef struct node_s {
data_t data;
struct node_s* next;
} node_t;
node_t** list_new() {
node_t** list = malloc(sizeof **list);
if (!list) return NULL;
*list = NULL;
return list;
}
void list_delete(node_t** list) {
node_t *node, *next;
for (node = *list; node; node = next) {
next = node->next;
free(node);
}
}
node_t* list_push_front(node_t** list, data_t data) {
node_t* node = malloc(sizeof(node_t));
if (!node) return NULL;
node->data = data;
node->next = *list;
return *list = node;
}
// IS EASY TO SWAP THE VALUES
/*
void swap(data_t* a, data_t* b) {
data_t c = *a;
*a = *b;
*b = c;
}
void simple_bubble_sort(node_t** list) {
for(node_t* i = *list; i; i = i->next)
for(node_t* j = *list; j->next; j = j->next)
if(j->data > j->next->data)
swap(&(j->data), &(j->next->data));
}
*/
// MUCH LESS EASY TO SWAP THE NODES
void swap_node(node_t** prev_node_A, node_t** node_A, node_t** node_B) {
node_t *last_node = (*node_B)->next;
node_t *first_node = *node_A;
node_t *second_node = *node_B;
if (prev_node_A) {
(*prev_node_A)->next = second_node;
(*prev_node_A)->next->next = first_node;
(*prev_node_A)->next->next->next = last_node;
} else {
(*node_A) = second_node;
(*node_A)->next = first_node;
(*node_A)->next->next = last_node;
}
}
void simple_bubble_sort(node_t** list) {
for(node_t* i = *list; i->next; i = i->next)
for (node_t *j = *list; j->next->next; j = j->next) {
if (j == *list) {
if (j->data > j->next->data) {
*list = j->next;
swap_node(NULL, &j, &(j->next));
}
}
else {
if (j->next->data > j->next->next->data)
swap_node(&j, &(j->next), &(j->next->next));
}
//printf("%i,%i | %i, %i, %i, %i \n", i->data, j->data, (*list)->data, (*list)->next->data, (*list)->next->next->data, (*list)->next->next->next->data);
//system("pause");
}
}
int main() {
// Create List
node_t** list = list_new();
if (!list)
goto memory_allocation_failure;
// Add values to List
for(int i=0; i<10; i++)
if (!list_push_front(list, i))
goto memory_allocation_failure;
// Print List
for (node_t* node = *list; node != NULL; node = node->next)
printf("%i\n", node->data);
// Swap Test
//swap_node(NULL, &(*list), &((*list)->next));
//swap_node(&(*list), &((*list)->next), &((*list)->next->next));
// Sort List
printf("-- Bubble Sort --\n");
simple_bubble_sort(list);
// Print List
for (node_t* node = *list; node != NULL; node = node->next)
printf("%i\n", node->data);
// Delete List
list_delete(list);
return 0;
// Error Handler
memory_allocation_failure:
printf("Memory Allocation Failure");
return 1;
}
Here is the function swap.
void swap( node_t **current )
{
node_t *tmp = ( *current )->next->next;
( *current )->next->next = *current;
*current = ( *current )->next;
( *current )->next->next = tmp;
}
And here is the function simple_bubble_sort
void simple_bubble_sort( node_t **head )
{
if ( *head )
{
for ( node_t **first = head, *sorted = NULL, *last = sorted;
( *first )->next != last;
last = sorted )
{
sorted = ( *first )->next;
for ( node_t **current = first; ( *current )->next != last; current = &( *current )->next )
{
if ( ( *current )->next->data < ( *current )->data )
{
swap( current );
sorted = ( *current )->next;
}
}
}
}
}
Investigate them.
Pay attention to that the header <malloc.h> is not a standard C header. Instead use the header <stdlib.h>.
You need to revise your current code. For example this function
node_t** list_new() {
node_t** list = malloc(sizeof **list);
if (!list) return NULL;
*list = NULL;
return list;
}
does not make sense it should be removed.
What you need is just to define in main a pointer like
node_t *head = NULL;
And pass it to functions as for example to the function simple_bubble_sort like
simple_bubble_sort( &head );
Or the function list_push_front should be defined like
int list_push_front(node_t** list, data_t data)
{
node_t* node = malloc(sizeof(node_t));
int success = node != NULL;
if ( success )
{
node->data = data;
node->next = *list;
*list = node;
}
return success;;
}

Delete specific element linked list

Can you help me to understand why this function doesn't delete a specific element in the linked list? What am I doing wrong?
typedef struct str_node{
int data;
struct str_node *next;
}node;
...
node *head;
head = malloc(sizeof(node));
...
void delete_spec(node *a){
int num;
node *tmp;
tmp = a;
printf("Insert number to eliminate: ");
scanf("%d",&num);
while(tmp!=NULL){
if(tmp->data == num){
tmp = tmp->next->next;
}
tmp = tmp->next;
}
}
For starters it is unclear what the allocation is doing here
node *head;
head = malloc(sizeof(node));
The pointer head shall be initially set to NULL
node *head = NULL;
and new nodes shall be inserted in the list by the function that inserts new values in the list.
The function that deletes nodes from the list shall not issue any prompt. It is the caller of the function that will ask the user to specify the value that will be deleted from the list and then call the function passing the specified value. So the function should have two parameters: pointer to the pointer to the head node and the integer value that shall be deleted from the list.
The function can be defined the following way
void delete_spec( node **head, int data )
{
while ( *head != NULL )
{
if ( ( *head )->data == data )
{
node *tmp = *head;
*head = ( *head )->next;
free( tmp );
}
else
{
head = &( *head )->next;
}
}
}
Here is a demonstrative program.
#include <stdio.h>
#include <stdlib.h>
typedef struct str_node
{
int data;
struct str_node *next;
} node;
int push_front( node **head, int data )
{
node *new_node = malloc( sizeof( node ) );
int success = new_node != NULL;
if ( success )
{
new_node->data = data;
new_node->next = *head;
*head = new_node;
}
return success;
}
void delete_spec( node **head, int data )
{
while ( *head != NULL )
{
if ( ( *head )->data == data )
{
node *tmp = *head;
*head = ( *head )->next;
free( tmp );
}
else
{
head = &( *head )->next;
}
}
}
void display( node *head )
{
for ( ; head != NULL; head = head->next )
{
printf( "%d -> ", head->data );
}
puts( "null" );
}
int main(void)
{
node *head = NULL;
int a[] = { 1, 3, 5, 7, 1, 2, 3, 1 };
const size_t N = sizeof( a ) / sizeof( *a );
for ( size_t i = 0; i < N; i++ )
{
push_front( &head, a[i] );
}
display( head );
delete_spec( &head, 1 );
display( head );
return 0;
}
Its output is
1 -> 3 -> 2 -> 1 -> 7 -> 5 -> 3 -> 1 -> null
3 -> 2 -> 7 -> 5 -> 3 -> null
public class LL5 {
Node head;
int size;
LL5(){
this.size = 0;
}
class Node{
String data;
Node next;
Node(String data){
this.data = data;
this.next = null;
size++;
}
}
public void push(String data) {
Node newNode = new Node(data);
if(head == null) {
head = newNode;
return;
}
newNode.next = head;
head = newNode;
}
public void deleteelement(String data) {
Node cur = head;
Node x = cur.next;
Node y = x.next;
if(data == head.data) {
head = head.next;
}
else if(x.data == data) {
head.next = y;
}
else{
while(y.data != data) {
x = x.next;
y = y.next;
}
x.next = y.next;
}
}
public void printList() {
if(head == null) {
System.out.println("list is empty");
return;
}
Node cn = head;
while(cn != null) {
System.out.print(cn.data + " -> ");
cn = cn.next;
}
System.out.print("null");
}
public static void main(String[] args) {
LL5 obj = new LL5();
obj.push("1");
obj.push("2");
obj.push("3");
obj.push("4");
obj.push("5");
obj.push("6");
obj.push("7");
obj.push("8");
obj.push("9");
obj.push("10");
System.out.println("before delete");
obj.printList();
System.out.println();
System.out.println("After delete");
obj.deleteelement("10");
obj.printList();
}
}
delete_spec does not modify in anyway the input list. also: it does not free any memory.
in order to actually delete a node you must:
1. free its memory.
2. modify the list so the "next" pointers are updated. in order to update the list, you must provide the delete function the address of the head so it can modify also head.
something like this:
void delete_spec(node **a){
int num;
node *tmp;
if (a == NULL || *a == NULL) return;
tmp = *a;
printf("Insert number to eliminate: ");
scanf("%d",&num);
if (tmp->data == num)
{
*a = (*a)->next;
free(tmp);
return;
}
while(tmp->next!=NULL){
if(tmp->next->data == num){
node* tmp2 = tmp->next;
tmp->next = tmp->next->next;
free(tmp2);
}
tmp = tmp->next;
}
}

How do I swap the nodes of a linked list in a bubble-sort algorithm?

In C I have to write a bubble sort function that swaps the nodes and not the values of a LinkedList, but I'm unable to accomplish it. Here is the code (As you can see the order is not correct):
#include <stdio.h>
#include <malloc.h> // malloc, free
#include <stddef.h> // NULL
// defining 'int' as data_t
typedef int data_t;
typedef struct node_s {
data_t data;
struct node_s* next;
} node_t;
node_t** list_new() {
node_t** list = malloc(sizeof **list);
if (!list) return NULL;
*list = NULL;
return list;
}
void list_delete(node_t** list) {
node_t *node, *next;
for (node = *list; node; node = next) {
next = node->next;
free(node);
}
}
node_t* list_push_front(node_t** list, data_t data) {
node_t* node = malloc(sizeof(node_t));
if (!node) return NULL;
node->data = data;
node->next = *list;
return *list = node;
}
// IS EASY TO SWAP THE VALUES
/*
void swap(data_t* a, data_t* b) {
data_t c = *a;
*a = *b;
*b = c;
}
void simple_bubble_sort(node_t** list) {
for(node_t* i = *list; i; i = i->next)
for(node_t* j = *list; j->next; j = j->next)
if(j->data > j->next->data)
swap(&(j->data), &(j->next->data));
}
*/
// MUCH LESS EASY TO SWAP THE NODES
void swap_node(node_t** prev_node_A, node_t** node_A, node_t** node_B) {
node_t *last_node = (*node_B)->next;
node_t *first_node = *node_A;
node_t *second_node = *node_B;
if (prev_node_A) {
(*prev_node_A)->next = second_node;
(*prev_node_A)->next->next = first_node;
(*prev_node_A)->next->next->next = last_node;
} else {
(*node_A) = second_node;
(*node_A)->next = first_node;
(*node_A)->next->next = last_node;
}
}
void simple_bubble_sort(node_t** list) {
for(node_t* i = *list; i->next; i = i->next)
for (node_t *j = *list; j->next->next; j = j->next) {
if (j == *list) {
if (j->data > j->next->data) {
*list = j->next;
swap_node(NULL, &j, &(j->next));
}
}
else {
if (j->next->data > j->next->next->data)
swap_node(&j, &(j->next), &(j->next->next));
}
//printf("%i,%i | %i, %i, %i, %i \n", i->data, j->data, (*list)->data, (*list)->next->data, (*list)->next->next->data, (*list)->next->next->next->data);
//system("pause");
}
}
int main() {
// Create List
node_t** list = list_new();
if (!list)
goto memory_allocation_failure;
// Add values to List
for(int i=0; i<10; i++)
if (!list_push_front(list, i))
goto memory_allocation_failure;
// Print List
for (node_t* node = *list; node != NULL; node = node->next)
printf("%i\n", node->data);
// Swap Test
//swap_node(NULL, &(*list), &((*list)->next));
//swap_node(&(*list), &((*list)->next), &((*list)->next->next));
// Sort List
printf("-- Bubble Sort --\n");
simple_bubble_sort(list);
// Print List
for (node_t* node = *list; node != NULL; node = node->next)
printf("%i\n", node->data);
// Delete List
list_delete(list);
return 0;
// Error Handler
memory_allocation_failure:
printf("Memory Allocation Failure");
return 1;
}
Here is the function swap.
void swap( node_t **current )
{
node_t *tmp = ( *current )->next->next;
( *current )->next->next = *current;
*current = ( *current )->next;
( *current )->next->next = tmp;
}
And here is the function simple_bubble_sort
void simple_bubble_sort( node_t **head )
{
if ( *head )
{
for ( node_t **first = head, *sorted = NULL, *last = sorted;
( *first )->next != last;
last = sorted )
{
sorted = ( *first )->next;
for ( node_t **current = first; ( *current )->next != last; current = &( *current )->next )
{
if ( ( *current )->next->data < ( *current )->data )
{
swap( current );
sorted = ( *current )->next;
}
}
}
}
}
Investigate them.
Pay attention to that the header <malloc.h> is not a standard C header. Instead use the header <stdlib.h>.
You need to revise your current code. For example this function
node_t** list_new() {
node_t** list = malloc(sizeof **list);
if (!list) return NULL;
*list = NULL;
return list;
}
does not make sense it should be removed.
What you need is just to define in main a pointer like
node_t *head = NULL;
And pass it to functions as for example to the function simple_bubble_sort like
simple_bubble_sort( &head );
Or the function list_push_front should be defined like
int list_push_front(node_t** list, data_t data)
{
node_t* node = malloc(sizeof(node_t));
int success = node != NULL;
if ( success )
{
node->data = data;
node->next = *list;
*list = node;
}
return success;;
}

Remove value from a List

I'm writing a program which creates lists, print it and remove from the list (3 functions).
Print and pushBack are fine, they work nice but I can't figure out how to pick up a number to remove from the list in removeFromList() function.
Don't pay attention on names (like client, socket), it's for my client-server application to save active sockets (that's why I need to remove them from a list when client has disconnected).
Here I have 2 structures: listElement and clientList (which contains a pointer to head element of listElement)
struct listElement
{
SOCKET socket;
struct listElement* next;
};
struct clientList
{
listElement * head;
};
My pushBack function:
int pushBackŠ”lient(struct clientList* list, int socket)
{
struct listElement* newClient = (struct listElement*)malloc(sizeof(struct listElement));
struct listElement* currentElement = list->head;
newClient->socket = socket;
newClient->next = 0;
do
{
// IT'S PUSHBACK
if (list->head == 0)
{
list->head = newClient;
break;
}
while (currentElement->next != 0)
{
currentElement = currentElement->next;
}
currentElement->next = newClient;
} while (false);
return 0;
}
My print:
void print(struct clientList* list)
{
struct listElement* currentElement = list->head;
while (currentElement != 0)
{
printf("%d\n", currentElement->socket);
currentElement = currentElement->next;
}
}
And the function I have a problem with (I made debug messages to see if a "socket" was added correctly). I suppose I don't need the first 3 lines but not sure.
Updated13/05/2017
void removeFromList(struct clientList* list, int socket)
{
struct listElement* currentElement = list->head;
do
{
if (list->head == 0)
{
return;
}
while (currentElement != 0 && currentElement->next != 0)
{
if (currentElement->socket == socket)
{
printf("currentElement == %d\n", currentElement);
currentElement = currentElement->next;
printf("currentElement == %d\n", currentElement);
free(currentElement);
//break; // if I only want to remove the first socket?
}
currentElement = currentElement->next;
}
} while (false);
}
Thank you.
The function removeFromList is wrong at least because this condition of the while statement can be equal to false when the list contains only one element. In this case even this one element contains the target vakue it will not be removed.
while (currentElement != 0 && currentElement->next != 0)
The functions can look as it is shown in the demonstrative program.
#include <stdio.h>
#include <stdlib.h>
typedef int SOCKET;
struct listElement
{
SOCKET socket;
struct listElement *next;
};
struct clientList
{
struct listElement *head;
};
int pushBackClient( struct clientList *list, SOCKET socket )
{
struct listElement *newClient = malloc( sizeof( struct listElement ) );
int success = newClient!= NULL;
if ( success )
{
newClient->socket = socket;
newClient->next = NULL;
struct listElement **current = &list->head;
while ( *current != NULL ) current = &( *current )->next;
*current = newClient;
}
return success;
}
int removeFromList( struct clientList *list, SOCKET socket )
{
int success;
struct listElement **current = &list->head;
while ( *current != NULL && ( *current )->socket != socket )
{
current = &( *current )->next;
}
if ( ( success = *current != NULL ) )
{
struct listElement *tmp = *current;
*current = ( *current )->next;
free( tmp );
}
return success;
}
void print(struct clientList *list)
{
for ( struct listElement *current = list->head;
current != NULL;
current = current->next )
{
printf( "%d ", current->socket );
}
}
int main(void)
{
const int N = 10;
struct clientList list = { NULL };
for ( int i = 0; i < N; i++ ) pushBackClient( &list, i );
print( &list );
putchar( '\n' );
for ( int i = 0; i < N; i++ )
{
if ( i % 2 == 0 ) removeFromList( &list, i );
}
print( &list );
putchar( '\n' );
for ( int i = 0; i < N; i++ )
{
if ( i % 2 == 1 ) removeFromList( &list, i );
}
print( &list );
putchar( '\n' );
return 0;
}
The program output is
0 1 2 3 4 5 6 7 8 9
1 3 5 7 9
You need at least to add a function that will free all elements of the list.
For your remove function I suggest something like this:
void removeFromList(struct clientList* list, int socket)
{
struct listElement* aux, prev;
if(list->head == 0)
return;
aux = list->head;
prev = aux;
while(aux != 0){
if(aux->socket == socket) {
prev->next = aux->next;
free(aux);
break; // if you only want to remove the first socket
}
prev = aux;
aux = aux->next;
}
}
As for your list structure, I suggest using a structure of structs, like follows:
struct list
{
int numberOfElements;
NODE * first;
} LIST;
struct node
{
ELEMENT * info;
NODE * prev; // If you want to have a double connection between the nodes
NODE * next;
} NODE;
struct element
{
int id;
/* Other Properties */
} ELEMENT;
It should give you a better control of your list.

void Pointer to a structure causes error 'dereferencing 'void *' pointer'

I try to initialize a queueADT pointer called initAmigo. Apparently I never create one if the structure is not making the pointers for the (void *data)
Reasons why I can't put any data in void *data in node structure:
Asuumption 1: Take away void *data and say User_struct (from .h file)
Assumption 2: RegisteredData is not supposed to be a pointer because the parameter in que_Insert asks for a pointer)
Assumption 3: In AddUser function, que_insert( initAmigo , registeredData); The registeredData to be inserted in the void *data node was actually a pointer to a pointer to a structure
Having a queue reference a void * creates the following error:
amigonet.c: In function 'findUser':
amigonet.c:247:21: warning: dereferencing 'void *' pointer [enabled by default]
if (currNode->data->name == name) { //If front is the name being searched
^
amigonet.c:247:21: error: request for member 'name' in something not a structure or union
amigonet.c:253:22: warning: dereferencing 'void *' pointer [enabled by default]
if (currNode->data->name == name ) {
.c file just has to create a structure of a queue of Users (QueueADT initAmigo)
typedef struct node {
//name of data userStruct (just for referencing)
void* data;
struct node *next;
}node;
struct queueStruct {
struct node *front; /* pointer to front of queue */
struct node *rear; /* pointer to rear of queue */
int (*cmprFunc)(const void*a,const void*b); /* The compare function used for insert */
};
typedef struct queueStruct *QueueADT; //typedef inserted for pointers,
//name is QueueADT
#define _QUEUE_IMPL_
#include "queueADT.h"
/// create a queue that is either sorted by cmp or FIFO
//function with two void
QueueADT que_create( int (*cmp)(const void*a,const void*b) ) {
QueueADT new;
new = (QueueADT)malloc(sizeof(struct queueStruct)); //NOTE: NO POINTERS HERE
//use pointers in Single lines
if (cmp == NULL) {
new->front = NULL;
new->rear = NULL;
new->cmprFunc = NULL;
} else {
new->cmprFunc = cmp;
new->front = NULL;
new->rear = NULL;
}
return ( new );
}
//free the queue once the nodes have been cleared from the queue
void que_destroy( QueueADT queue ) {
if ( queue->front == NULL ) {
free(queue);
} else {
que_clear(queue);
free(queue);
}
}
//passes a real pointer for dynamic allocation
//set a temp to the front to be deleted, then front becomes the next and delete the temp
void que_clear( QueueADT queue ) {
while (queue->front->next != NULL) {
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
temp = queue->front;
queue->front= queue->front->next;
free(temp);
}
}
//if the cmpr function returns a positive then put in in before the b node in cmp(curr, temp)
void que_insert( QueueADT queue, void *data ) {
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->data= data;
temp->next = NULL;
node *currNode; //simply a pointer
currNode = queue->front;
if ( queue->front != NULL ) { //+1 or more in Q
if ( queue->cmprFunc == NULL ) { //if the cmp_func is FIFO
queue->rear->next = temp;
queue->rear= temp;
queue->rear->next=NULL;
if ( queue->front == queue->rear ) {
currNode->next = temp;
queue->rear = temp;
temp->next= NULL;
}
} else {
while ( currNode->next != NULL ){ //2 or more
if (( (*(queue->cmprFunc))(currNode->data, temp->data) >= 0
&& ( currNode == queue->front)) ) {
temp->next = currNode;
queue->front=temp;
break;
}
if (( (*(queue->cmprFunc))(currNode->next->data, temp->data) >= 0 ) ) {
temp->next = currNode->next;
currNode->next = temp;
break;
} else {
currNode = currNode->next; //move past front and possibly rear
if (currNode->next == NULL ) { //currNode is rear of queue
currNode->next = temp;
queue->rear = temp;
temp->next = NULL;
break;
}
//exit_failure
}
}
if ( queue->front == queue->rear ) { //exactly 1 node in queue
if (( (*(queue->cmprFunc))(currNode->data, temp->data) >= 0 ) ) {
temp->next = queue->front;
queue->front = temp;
} else {
queue->front->next = temp;
queue->rear = temp;
}
}
}
} else {
queue->front = temp;
queue->rear= queue->front;
queue->front->next= queue->rear->next = NULL;
}
}
//removes the front
void *que_remove( QueueADT queue ) {
if ( queue->front != NULL ) { //if the size of queue is greater than 1
node *currNode; // dont make new node, pointer
currNode = queue->front;
void *data = currNode->data;
if ( queue->front == queue->rear ) { //if size of queue is 1
free(currNode);
queue->front = queue->rear = NULL; //set both to NULL
} else {
queue->front= currNode->next;
free(currNode);
}
return data;
}
return NULL;
}
bool que_empty( QueueADT queue ) {
if ( queue->front == NULL ) {
return true;
} else {
return false;
}
}
QueueADT initAmigo;
//struct queueStruct *initAmigo
//QueueADT initAmigo;
//make a Queue with front and rear
//compare function with A-Z
struct Friends_struct {
struct queueStruct *amigos_Queue;
};
void create_amigonet() {
//makes the same thing as que_Create
//(Friends)malloc(sizeof(struct Friend_struct));
initAmigo = que_create(NULL);
printf("%lu Founded size of an.c\n\n\n\n\n\n\n\n\n\n\n", sizeof(initAmigo));
}
//The add user will find a new user and make the name of the user to a amigonet
//add Usernode to queue initAmigo
//make sure set to first and display queue
void addUser( const char *name ) {
//create Usernode and void *data (user w/ friends)
//check to see if User already exists
if ( findUser(name) != NULL) {
return;
}
//create data for node, by initializing memorry for a userStruct
struct User_struct *registeredData;
registeredData = ( struct User_struct* )malloc(sizeof(struct User_struct)); //first user Data
registeredData->name = name;
//create a Friends
//put this in file create F
struct Friends_struct *initAmigos;
initAmigos = (struct Friends_struct*)malloc(sizeof(struct Friends_struct)); //NOTE: NO POINTERS HERE
//set Friends list to Null
initAmigos->amigos_Queue = que_create( NULL );
//put User with empty Friends struct
registeredData->amigos = initAmigos;
//void que_insert( QueueADT queue, void *data )
que_insert( initAmigo , registeredData);
printf("%s User was inerted \n", name);
}
//Find a user in the init Amgio
//start at front as temp, go to next until name found
User *findUser( const char *name ) {
struct node *currNode;
currNode = initAmigo->front;
printf("%lu Founded size of an.c\n\n\n\n\n\n\n\n\n\n\n", sizeof(initAmigo));
if ( initAmigo->front == NULL ) {
return NULL;
} else {
if (currNode->data->name == name) { //If front is the name being searched
return currNode->data;
}
//Loop after front
while ( currNode->next != NULL ) {
currNode = currNode->next;
if (currNode->data->name == name ) {
return currNode->data;
}
}
node *rear = currNode;
if (rear->data->name == name ) {
return rear->data;
}
}
//User Not Founded
return;
}
#if 0
void addAmigo( User *user, User *amigo ) {
//check to see if node exits within friends
//go to use friends list
//traverse the node, check data for amigo->name == data->name
if ( user->amigos->amigos_Queue->front != NULL ) {
node *currNode = user->amigos->amigos_Queue->front;
if ( currNode->data->name == amigo->name ) {
return ;
} else {
//loop to determine if User friend queue front to rear has amigo already
while ( currNode->next != NULL ) {
currNode = currNode->next;
if (currNode ->data->name == amigo->name) {
return;
}
}
}
}
que_insert( user->amigos->amigos_Queue, amigo);
}
void removeAmigo( User *user, User *ex_amigo) {
//Pre Condition: Amgio exists in user
//go to user
//set user as currNode
//either create a function in queueADT remove
// use a prev and curr and next and traverse, remove, connect
//preconditions: front is not examigo
// rear is not examigo
// amigo is friend of user
}
size_t separation( const User *user1, const User *user2 ) {
return;
//queue a DFS
// queue a BFS
//
}
//search using a queue BFS
//search using a DFS
//number of users and their names
void dump_data() {
if (initAmigo->front != NULL) {
node *currNode = initAmigo->front;
printf("%s: Amigos!\n", currNode->data->name); //prints the name of the first node
while ( currNode->next != NULL ) {
currNode = currNode->next;
printf("%s: Amigos!\n", currNode->data->name);
}
//while loop of each user set to currNodw
//set a variable for currNodeAmigo and print the names of them inside ^^^
}
}
void destroy_amigonet(){
//clear Friend Queue and destroy for each User Friend
//destroy initAmigo queue
}
The problem looks like this:
typedef struct node {
void* data;
struct node *next;
}node;
...
struct node *currNode;
if (currNode->data->name == name) { //If front is the name being searched
return currNode->data;
}
The type of currNode->data is void *. This is an incomplete type, which means that it cannot be dereferenced. The compiler has no idea what to make of whatever it points at. You need to convert the void pointer, into a pointer to a meaningful type.
You have not defined the User type in the code you posted, but I'm guessing that you want something like this:
User *user = currNode->data;
if (user->name == name) {
return user;
}
You will also have to make similar changes elsewhere in the same function.

Resources