I created a linkedlist structure and it creates the first structure just fine but the second one ist’t created and the functions for it seem as if they arn’t being ran.
`#include<stdio.h>
#include<stdlib.h>
typedef struct linkedList {
struct Node {
int data;
struct Node* next;
} Node;
struct Node* head; // global variable - pointer to head Node
struct Node* tail; // global variable - pointer to tail Node
};
struct Node* GetNewNode(int item) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = item;
newNode -> next = NULL;
return newNode;
}
void insertAtHead(struct linkedList* listPointer, int item) {
struct Node* newNode = GetNewNode(item);
if(NULL == listPointer->head) {
listPointer->head = newNode;
listPointer->tail = newNode;
return;
}
if(NULL == listPointer->head->next) {
listPointer->tail = listPointer->head;
}
newNode->next = listPointer->head;
listPointer->head = newNode;
}
void insertAtTail(struct linkedList* listPointer, int item) {
struct Node* newNode = GetNewNode(item);
if(NULL == listPointer->head) {
listPointer->head = newNode;
listPointer->tail = newNode;
return;
}
listPointer->Node.next = newNode;
listPointer->tail = newNode;
}
void print(struct linkedList* listPointer) {
struct Node* currentNode = listPointer->head;
while(NULL != currentNode) {
printf("%d ", currentNode->data);
currentNode = currentNode->next;
}
printf("n");
return;
}
int main() {
struct linkedList* myList;
insertAtHead(myList, 12);
insertAtHead(myList,122);
insertAtHead(myList,1222);
insertAtTail(myList,24);
insertAtTail(myList,244);
insertAtTail(myList,2444);
print(myList);
struct linkedList* mySecondList;
insertAtHead(mySecondList, 14);
insertAtHead(mySecondList, 144);
insertAtHead(mySecondList, 1444);
insertAtTail(mySecondList, 19);
insertAtTail(mySecondList, 199);
insertAtTail(mySecondList, 1999);
print(mySecondList);
return 0;
}`
I tried to make 2 of the same structure expecting both to behave the same way but only the first one did something, and the second one seemed like it never got created.
New contributor
Insert0Name0Here is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.