I am trying to populate my linked list in C, but id does not word how
I want to. I want to keep a pointer "p" and keep adding to the list with the same pointer. However when I try to print, it prints only the data of the head!
#include<stdio.h>
#include <stdlib.h>
typedef struct{
int data;
struct node *next;
}node;
int main(){
node *head = NULL;
head = malloc(sizeof(node));
if(head==NULL){
printf("ta foirer quelque chose frero!");
return 1;
}
(*head).data=3;
(*head).next=NULL;
node *p = NULL;
p = (node*) head->next;
p = malloc(sizeof(node));
p->data = 5;
p->next = NULL;
p= (node *)p->next;
int i=0;
while(i<5){
p = malloc(sizeof(node));
i++;
p->data = i;
p->next=NULL;
p= (node *)p->next;
}
p = head;
while(p){
printf("\n%d",p->data);
p =(node*) p->next;
}
return 0;
}
I am getting as output
3
and I am expecting to get
3
5
0
1
2
3
4
#include<stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
typedef struct Node node;
void insert(node* h, int v) {
node* tmp = h;
while(tmp->next)
tmp = tmp->next;
node* newnode = malloc(sizeof(node));
newnode->data = v;
newnode->next = NULL;
tmp->next = newnode;
}
int main(){
node *head = NULL;
head = malloc(sizeof(node));
if(head==NULL){
printf("ta foirer quelque chose frero!");
return 1;
}
head->data=3;
head->next = NULL;
node *p = NULL;
insert(head, 5);
int i=0;
while(i<5){
insert(head, i++);
}
p = head;
while(p){
printf("%d\n",p->data);
p = p->next;
}
return 0;
}
If you notice, I changed the layout of your code a bit, and made it cleaner. What you needed was to traverse to find the node that appears before the place to add the new node, which in this case is the end. Usually this is a separate pointer in a different struct that contains the head and this is called the tail of a linked list. You were simply not keeping track of where to really add the node. This insert function above does that.
Related
I'm trying to figure out how to duplicate a linked list, and after debugging on Vs code I'm getting a segmentation fault on cuurent->data = temp->data;
and I'm not sure why this is happening.
and this is the code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* head;
struct node* head2;
struct node* Insert(struct node* head, int x)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = x;
temp->next = head;
return temp;
}
void Print(struct node* head)
{
struct node* tmp1 = head;
printf("List is:");
while (tmp1 != NULL) {
printf(" %d", tmp1->data);
tmp1 = tmp1->next;
}
printf("\n");
}
struct node* dupe(struct node* head, struct node* head2)
{
if (head == NULL)
return NULL;
struct node* temp = head;
struct node* prev = NULL;
struct node* cuurent = (struct node*)malloc(sizeof(struct node));
cuurent->data = temp->data;
if (head2 == NULL) {
cuurent->next = head2;
head2 = cuurent;
}
while (temp != NULL) {
temp = temp->next;
cuurent = (struct node*)malloc(sizeof(struct node));
cuurent->data = temp->data;
cuurent->next = prev;
prev = cuurent;
}
return head2;
}
int main(void)
{
head = NULL;
head2 = NULL;
head = Insert(head, 4);
head = Insert(head, 2);
head = Insert(head, 3);
head = Insert(head, 5);
head2 = dupe(head, head2);
Print(head);
Print(head2);
}
As pointed out in the comments,
while (temp != NULL) {
temp = temp->next;
changes the value of temp immediately after guarding against a null pointer value.
This eventually means
cuurent->data = temp->data;
will cause Undefined Behaviour by dereferencing temp when it is the null pointer value.
You can apply the exact same looping principles shown in Print to copy the list. The only difference being you must save a pointer to the first node.
The same principle can be used again when it comes time to free the memory.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *insert(struct node *next, int x)
{
struct node *n = malloc(sizeof *n);
n->data = x;
n->next = next;
return n;
}
void print_list(struct node *head)
{
printf("List is: ");
while (head) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
void free_list(struct node *head)
{
struct node *t;
while (head) {
t = head->next;
free(head);
head = t;
}
}
struct node *copy_list(struct node *head)
{
struct node *root = NULL;
for (struct node *current; head; head = head->next) {
struct node *new = insert(NULL, head->data);
if (!root)
root = new;
else
current->next = new;
current = new;
}
return root;
}
int main(void)
{
struct node *head = NULL;
head = insert(head, 4);
head = insert(head, 2);
head = insert(head, 3);
head = insert(head, 5);
struct node *head2 = copy_list(head);
print_list(head);
print_list(head2);
free_list(head);
free_list(head2);
}
Output:
List is: 5 3 2 4
List is: 5 3 2 4
#include <stdio.h>
#include <stdlib.h>
struct node{
int coff;
int pow;
struct node* next;
};
struct node* head1 = NULL;
struct node* head2 = NULL;
void insert(int c, int p, struct node* head){
struct node* newnode = (struct node*)malloc(sizeof(struct node));
struct node* temp = head;
newnode->coff = c;
newnode->pow = p;
newnode->next = NULL;
if(head == NULL){
head = newnode;
}else{
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newnode;
}
}
void display(struct node* h){
struct node* temp;
temp = h;
while(temp != NULL){
printf("%dx^%d ", temp->coff, temp->pow);
temp = temp->next;
}
printf("\n");
}
int main(){
insert(3,2,head1);
insert(5,1,head1);
insert(4,0,head1);
insert(9,2,head2);
insert(6,1,head2);
insert(2,0,head2);
display(head1);
display(head2);
return 0;
}
I have created node struct for my linked list. Then i have created two functions for insert and display polynomials. I have created head1 and head2 for store address of two different polynominals. And I want use that head1 and head2 as arguments in insert and display functions. then finally I wanna print both polynomials. But there is a bottleneck. When I execute my program it exits without giving any output.
My expected output is :
3x^2 5x^1 2x^0
9x^2 6x^1 4x^0
How can i solve this problem? Is there anything wrong with (struct node* head) arguments in the functions? Why this program exits without giving any output?
Thank you so much!
Your line head = newnode; isn't actually changing head1 and head2 because the pointers themselves were passed by value. This results in head1 and head2 remaining NULL, and thus resulting in no output. You can fix this issue by passing a pointer to a pointer to the head node to insert. A quick modification to your code to implement this:
#include <stdio.h>
#include <stdlib.h>
struct node{
int coff;
int pow;
struct node* next;
};
struct node* head1 = NULL;
struct node* head2 = NULL;
void insert(int c, int p, struct node **head){
struct node* newnode = (struct node*)malloc(sizeof(struct node));
struct node* temp = *head;
newnode->coff = c;
newnode->pow = p;
newnode->next = NULL;
if(*head == NULL){
*head = newnode;
}else{
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newnode;
}
}
void display(struct node* h){
struct node* temp;
temp = h;
while(temp != NULL){
printf("%dx^%d ", temp->coff, temp->pow);
temp = temp->next;
}
printf("\n");
}
int main(){
insert(3, 2, &head1);
insert(5, 1, &head1);
insert(4, 0, &head1);
insert(9, 2, &head2);
insert(6, 1, &head2);
insert(2, 0, &head2);
display(head1);
display(head2);
return 0;
}
Take a look at
void insert(int c, int p, struct node* head)
Because 'head' is passed by value (and all C parameters do so),
caller of this function has no chance to get the 'inside modified' head.
So, in your case, in main(), head1 is always NULL.
I modified a bit from your code, it seems works:)
But don't forget to free all allocated nodes.
include <stdio.h>
#include <stdlib.h>
struct node{
int coff;
int pow;
struct node* next;
};
struct node* head1 = NULL;
struct node* head2 = NULL;
struct node* insert(int c, int p, struct node* head){
struct node* newnode = (struct node*)malloc(sizeof(struct node));
struct node* temp = head;
newnode->coff = c;
newnode->pow = p;
newnode->next = NULL;
if(head == NULL){
head = newnode;
}else{
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newnode;
}
return head;
}
void display(struct node* h){
struct node* temp;
temp = h;
while(temp != NULL){
printf("%dx^%d ", temp->coff, temp->pow);
temp = temp->next;
}
printf("\n");
}
int main(){
head1 = insert(3,2,head1);
insert(5,1,head1);
insert(4,0,head1);
head2 = insert(9,2,head2);
insert(6,1,head2);
insert(2,0,head2);
display(head1);
display(head2);
return 0;
}
I am trying to create a simple linked list using C, I think I was able to construct the linked list itself, but when I try and print it, it prints the value of the last node instead of all the values in the list.
#include <stdio.h>
#include <alloca.h>
typedef int DATA;
struct Node
{
DATA d;
struct Node *next;
};
void printList(struct Node **head)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *temp;
temp = *head;
while(temp!=NULL)
{
printf("%d \n", temp->d);
temp = temp->next;
}
}
int main()
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *head = newNode;
struct Node *temp = newNode;
head->d = 1;
int i = 0;
printf("Enter 3 numbers");
for( i = 0; i< 3; i++)
{
scanf("%d", &temp->d);
temp->next = newNode;
temp = temp->next;
}
temp->next = NULL;
printf("%d \n", temp->d);
return 0;
}
Any help/tips would be greatly appreciated.
There are multiple problems in your code:
You are not creating the list properly. You are allocating memory for only one node and playing around with pointers improperly causing the linked list to be pointed to the same memory
You are not calling printList function at all
It is not clear why you are allocating memory again in printList whereas it should be printing the already created list
Also, no need to pass double pointer for printList as you are not modifying the list.
Though you should be understanding and doing the changes yourself, I am providing a below modified program which I hope will help you understand the mistakes in your code.
Please see the below modified version of the code
#include<stdio.h>
#include <alloca.h>
typedef int DATA;
struct Node
{
DATA d;
struct Node *next;
};
void printList(struct Node *head)
{
struct Node *temp = head;
while(temp!=NULL)
{
printf("%d \n", temp->d);
temp = temp->next;
}
}
struct Node *createNode()
{
struct Node *newNode;
newNode = malloc(sizeof(struct Node));
if (NULL == newNode)
return NULL;
memset(newNode, 0, sizeof(struct Node));
return newNode;
}
int main()
{
struct Node *newNode = NULL;
struct Node *headNode = NULL;
struct Node *temp = NULL;
int i = 0;
int data = 0;
printf("Enter 3 numbers\n");
for( i = 0; i< 3; i++)
{
scanf("%d", &data);
newNode = createNode();
if (NULL == newNode)
break;
newNode->d = data;
if (headNode == NULL)
{
headNode = newNode;
temp = newNode;
}
else
{
temp->next = newNode;
temp = temp->next;
}
}
printList(headNode);
return 0;
}
Replace your code with the following code :-
#include<stdio.h>
#include <alloca.h>
typedef int DATA;
struct Node
{
DATA d;
struct Node *next;
};
void printList(struct Node **head)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *temp;
temp = *head;
while(temp->next!=NULL)
{
printf("%d \n", temp->d);
temp = temp->next;
}
}
int main()
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *head = newNode;
struct Node *temp = newNode;
head->d = 1;
int i = 0;
printf("Enter 3 numbers");
for( i = 0; i< 3; i++)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
scanf("%d", &temp->d);
temp->next = newNode;
temp = temp->next;
}
printList(head);
return 0;
}
Here you need to declare the newNode in the loop also while taking the input. As, the current value is over-writted, the old value is lossed and only the value of last node is printed.
Also while printing, check for temp->next!=Null instead of temp!=NULL
I want to insert node one by one in createDDCL() method and print all elements in printDDClinkList() method,but there is some strange error in createDDCL() method:
//BiDirection link list
typedef struct DDCL{
int data;
struct DDCL *priv;
struct DDCL *next;
}*BiDirectionLink;
BiDirectionLink createDDCL(){//带头结点的双向循环链表
int n = 0,i=0;
BiDirectionLink head = (BiDirectionLink)malloc(sizeof(BiDirectionLink));
BiDirectionLink newNode,lastNodw;
head->next = head;
head->priv = head;
lastNodw = head;
printf("how many node do you want?\n:");
scanf("%d",&n);
for (; i<n; i++) {//append new node to the last
newNode = (BiDirectionLink)malloc(sizeof(BiDirectionLink));
newNode->priv = lastNodw;
newNode->next = head;//new node's next always head
head->priv = newNode;//th privious one of the head always the last
lastNodw->next = newNode;
newNode->data = i;
lastNodw = newNode;
}
return head;
}
detail!!!!!!!!!!!
void printDDClinkList(BiDirectionLink head){
BiDirectionLink node = head->next;
while (node->next != head) {
printf("%d ",node->data);
node = node->next;
}
}
void testDDCLinkList(){
printDDClinkList(createDDCL());
}
int main(int argc, const char * argv[])
{
testDDCLinkList();
return 0;
}
This is the problem: You are allocating only enough memory for a pointer, not the whole struct.
This is what you did:
newNode = (BiDirectionLink)malloc(sizeof(BiDirectionLink));
Which is the same as this:
newNode = (struct DDCL *)malloc(sizeof(struct DDCL *));
But, that's what you wanted to do:
newNode = (struct DDCL *)malloc(sizeof(struct DDCL));
My recommendation? Never typedef to pointers.
How will I free the nodes allocated in another function?
struct node {
int data;
struct node* next;
};
struct node* buildList()
{
struct node* head = NULL;
struct node* second = NULL;
struct node* third = NULL;
head = malloc(sizeof(struct node));
second = malloc(sizeof(struct node));
third = malloc(sizeof(struct node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
return head;
}
I call the buildList function in the main()
int main()
{
struct node* h = buildList();
printf("The second element is %d\n", h->next->data);
return 0;
}
I want to free head, second and third variables.
Thanks.
Update:
int main()
{
struct node* h = buildList();
printf("The element is %d\n", h->next->data); //prints 2
//free(h->next->next);
//free(h->next);
free(h);
// struct node* h1 = buildList();
printf("The element is %d\n", h->next->data); //print 2 ?? why?
return 0;
}
Both prints 2. Shouldn't calling free(h) remove h. If so why is that h->next->data available, if h is free. Ofcourse the 'second' node is not freed. But since head is removed, it should be able to reference the next element. What's the mistake here?
An iterative function to free your list:
void freeList(struct node* head)
{
struct node* tmp;
while (head != NULL)
{
tmp = head;
head = head->next;
free(tmp);
}
}
What the function is doing is the follow:
check if head is NULL, if yes the list is empty and we just return
Save the head in a tmp variable, and make head point to the next node on your list (this is done in head = head->next
Now we can safely free(tmp) variable, and head just points to the rest of the list, go back to step 1
Simply by iterating over the list:
struct node *n = head;
while(n){
struct node *n1 = n;
n = n->next;
free(n1);
}
One function can do the job,
void free_list(node *pHead)
{
node *pNode = pHead, *pNext;
while (NULL != pNode)
{
pNext = pNode->next;
free(pNode);
pNode = pNext;
}
}
struct node{
int position;
char name[30];
struct node * next;
};
void free_list(node * list){
node* next_node;
printf("\n\n Freeing List: \n");
while(list != NULL)
{
next_node = list->next;
printf("clear mem for: %s",list->name);
free(list);
list = next_node;
printf("->");
}
}
You could always do it recursively like so:
void freeList(struct node* currentNode)
{
if(currentNode->next) freeList(currentNode->next);
free(currentNode);
}