Printing a linked list of integers in C - c

I'm trying to get an integer of any size to a linked list in C. But when I print the list, a zero is printed always after the the integer.
Please note that I'm adding each digit of the integer to the head.(head has the 0th place of the integer)
#include <stdio.h>
#include <stdlib.h>
struct node
{
int digit;
struct node* next;
};
void get_number(struct node** head);
int create_node(int digit, struct node** head);
void printlist(struct node* head);
int main()
{
struct node* head1 = malloc(sizeof(struct node*));
get_number(&head1);
printlist(head1);
return 0;
}
int create_node(int digit, struct node** head)
{
struct node* tmp = malloc(sizeof(struct node*));
tmp -> digit = digit;
tmp -> next = *head;
*head = tmp;
}
void printlist(struct node* head)
{
struct node* curr = head;
if(!head)
return;
while(curr != NULL )
{
printf("%d",curr -> digit);
curr = curr -> next;
}
}
void get_number(struct node** head)
{
int k;
char c;
c = getchar();
while(c != '\n' && c != ' ')
{
k = c - '0';
create_node(k, head);
c = getchar();
}
}
when I input 123456, the output is 1234560.
I tried to find solution, but couldn't. Please help

You are one more node than necessary when you allocate to head1. You simply need to call the function get_number() as:
struct node* head1 = 0;
get_number(&head1);
which would set the next of the last element (i.e. the first allocate node) to 0 and rest of the logic would be fine.
You also need to correctly call malloc() and change the type of c to int (to handle EOF) as noted in the comments. My preferred way is to allocate memory is:
TYPE *p = malloc(sizeof *p);

Related

How to change sinlgy linked list to doubly linked list?

I have a program that I am supposed to change it from a singly linked list to a doubly linked list. This means that I use pointer that points to the next node and a pointer that points to previous node.
How do I do this while recycling my previous code. Is there a way to do this with minimum steps involved?
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable:4996)
//declaring structure
typedef struct node
{
char songName[20];
int songLength;
int copyright;
struct node * next;
}node;
//prototypes
node *create(int n);
void display_recursive(node *n);
int main()
{
int n = 0;
node *head = NULL;
printf("How many entries?\n");
scanf("%d", &n);
//call to create list
head = create(n);
printf("\nThe linked list in order is:\n");
display_recursive(head);
return 0;
}
node *create(int n)
{
node *head = NULL;
node *temp = NULL;
node *p = NULL;
for (int i = 0; i < n; i++)
{
temp = (node*)malloc(sizeof(node));
printf("What is the name of song %d\n", i + 1);
scanf("%s", &temp->songName);
printf("What is the length of song %d (in seconds)?\n", i + 1);
scanf("%d", &temp->songLength);
printf("Is song %d copyrighted?(1 = YES, 0 = NO)\n", i + 1);
scanf("%d", &temp->copyright);
temp->next = NULL;
if (head == NULL)
{
head = temp;
}
else
{
// if not empty, attach new node at the end
p = head;
while (p->next != NULL)
{
p = p->next;
}
p->next = temp;
}
}
return head;
}
void display_recursive(node *n) {
if (!n) {
return;
}
display_recursive(n->next);
printf("Song: %s, ", n->songName);
printf("%d minutes, ",n->songLength);
if (n->copyright == 1)
{
printf("Copyrights\n");
}
else if (n->copyright == 0)
{
printf("No copyrights\n");
}
}
I don't really know how the code should look or what I have to add to achieve a doubly linked list.
You just need a pointer point to previous node
typedef struct node
{
char songName[20];
int songLength;
int copyright;
struct node * next;
struct node* prev;
}node;
just like #T1412 said, you need to add a new member to the structure.
typedef struct node
{
char songName[20];
int songLength;
int copyright;
struct node * next;
struct node* prev;
}node
now you need to modify the create() function so that each node's prev pointer is pointing to previous node, and the HEAD node's prev points to NULL.
Similarly, you need to modify all the linked list related functions to incorporate the prev pointer.
1) strongly suggest changing:
typedef struct node
{
char songName[20];
int songLength;
int copyright;
struct node * next;
}node;
to:
struct NODE
{
char songName[20];
int songLength;
int copyright;
struct NODE * prev;
struct NODE * next;
};
typedef struct NODE node;
Then wherever in the code that it is linking in a new node, add the necessary statement to set the 'prior' field. Remember that the firs node will contain NULL in the 'prior' field.

Traverse linked list with recursive method

I need to go through a list linked by the middle of a function with a parameter that is a triple pointer, by means of a recursive void function.
The program is as follows
typedef struct node
{
int data;
struct node* next;
}Node;
void insert(Node** first,int d){
Node* new= createNode(d);
new->next= *first;
*first=new;
}
Node* createNode(int d){
Node* new= (Node*)malloc(sizeof(Node));
new->data= d;
new->next=NULL;
return new;
}
void printList(Node***p)
{
Node**temp = *p;
if(temp == NULL)
return;
else
{
printf("\nValue: %d", (*temp)->data);
*temp = (*temp)->next;
printList(&temp);
}
}
int main()
{
Node *first = NULL;
int n =10;
while(n>0){
insert(&first,n);
n=n-1;
}
Nodo **ptr_first= &first;
printList(&ptr_first);
return 0;
}
The function prints all the values, but the program hangs and returns a negative value. What is wrong with this implementation?
PD: The use of the triple pointer is only for teaching purposes
Your recursion termination condition is wrong.
You have tho change if(temp == NULL) to if(*temp == NULL), since *temp is pointing to the element and not temp.
I also think that it is not good for teaching if you use triple pointers since they are not necessary here.

Linked string list in c

I'm trying this simple code which asks a user for string input. As it receives an input it then tries to copy each element of the string into different location in a linked list. Everything works just fine (I think) but when i print the linked list, the screen doesnt show any out put. Any idea why is this happening?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node {
char data;
struct node* next;
};
struct node* head = NULL;
void insert(char);
void print();
void main() {
char str1[20];
int i;
printf("Enter the string\n");
fgets(str1,20,stdin);
int len = strlen(str1);
printf("%d\n",len);
for(i=0;i<len;i++) {
insert(str1[i]);
}
print();
}
void insert(char str) {
struct node* temp = (struct node*)malloc(sizeof(struct node));
struct node* temp1 = head;
while(temp1!=NULL) {
temp1 = temp1->next;
}
temp->data = str;
temp1 = temp;
}
void print() {
struct node *temp;
temp = head;
while(temp!=NULL) {
printf("%c ",temp->data);
temp = temp->next;
}
}
You never set head to anything, it will always be NULL. So, you're not creating a list, but a group of unlinked floating nodes.
On another note, don't cast the result of malloc
On yet another note, no need to go through the entire list for every insert - you can keep a tail pointer along with the head, so adding to the end is done without a loop.
void insert(char str) {
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = str;
temp->next = NULL;
if(head){//head != NULL
struct node* temp1 = head;
while(temp1->next != NULL) {//search last element
temp1 = temp1->next;
}
temp1->next = temp;//insert new node
} else {
head = temp;//if head == NULL then replace with new node
}
}

Why does my link list have the wrong data?

When printing out my linked list, what is displayed is not what I assumed it to be. How do I get the right output?
struct node{
int data;
struct node *next;
};
struct node *newNode(int data){
struct node *new_node=(struct node *) malloc(sizeof(struct node));
new_node->data=data;
new_node->next=NULL;
return new_node;
}
void push(struct node*** head, int data){
struct node* new_node=newNode(data);
new_node->next=(**head);
(**head)=new_node;
}
void create(struct node **number, char num[]){
int x=0;
while(x<strlen(num)){
int d=(int)(num[x]);
push(&number, d);
x++;
}
}
void printList(struct node *number){
while(number!=NULL){
printf("%d", number->data);
number=number->next;
}
printf("\n");
}
int main (void){
struct node *first;
char num1[10];
scanf("%s", num1);
create(&first, num1);
printList(first);
return 0;
}
Examples
Input : 1
Expected Output: 1
Actual Output : 49
Input : 12345
Expected Output: 12345
Actual Output : 5352515049
I think it is printing where the value is stored, not the value itself.
Correct me on that if that's wrong. Anyways how do I get the expected output I want.
The problem is that you are reading ascii values, and then trying to print integers, and since you need to store integers and not the ascii values all you need is a simple mathematical operation, i.e. subtract the ascii value of the digit '0', so to convert the ascii value of a digit to it's integer value all you need is
integer = ascii - '0';
here I fixed your code, because you were appending the value to the head of the list instead of it's tail
#include <stdlib.h>
#include <stdio.h>
struct node{
int data;
struct node *next;
};
struct node *newNode(int data)
{
struct node *new_node;
new_node = malloc(sizeof(struct node));
if (new_node == NULL) /* always check that malloc succeeded */
return NULL;
new_node->data = data;
new_node->next = NULL;
return new_node;
}
struct node *push(struct node *tail, int data)
{
struct node *new_node;
new_node = newNode(data);
if (tail != NULL)
return tail->next = new_node;
return new_node;
}
struct node *create(char *numbers)
{
size_t i;
struct node *head;
struct node *tail;
i = 0;
head = NULL;
tail = NULL;
/* since strings are 'nul' terminated, you just need to loop,
* until you find the 'nul' byte, strlen() expects that byte
* anyway.
*/
while (numbers[i] != '\0')
{
tail = push(tail, numbers[i++] - '0');
if (head == NULL)
head = tail;
}
return head;
}
void printList(struct node *number)
{
while (number != NULL)
{
printf("%d", number->data);
number = number->next;
}
printf("\n");
}
void freeList(struct node *number)
{
while (number != NULL)
{
struct node *last;
last = number;
number = number->next;
free(last);
}
}
int main(void)
{
struct node *first;
char numbers[10];
/* '%9s' prevents buffer overflow */
if (scanf("%9s", numbers) != 1)
return -1;
first = create(numbers);
printList(first);
freeList(first);
return 0;
}
You have a linked list which ends up in reverse order, and this is why your second example comes out in reverse order. If you print the linked list like this:
printf("%d ", number->data);
with a space, it would be clearer what is happening, output is ASCII values
53 52 51 50 49
But you are also confusing character values with numeric values. If you correct this,
printf("%c ", number->data);
you will get your original input characters in reverse order.
5 4 3 2 1

What is wrong with my code for singly linked list?

I'm working on a singly linked list in C. This is what I've written so far.
C program
#include<stdio.h>
#include<stdlib.h>
struct Node{
int value;
struct Node *next;
};
struct Node* init()
{
struct Node* head=NULL;
head=malloc(sizeof(struct Node));
head->value=-1;
return head;
}
int length(struct Node* head)
{
struct Node* current=head;
int length=0;
while(current!=NULL)
{
length++;
current=current->next;
}
return length;
}
void print(struct Node* head)
{
int i=0;
int len=length(head);
for(i=0;i<len;i++)
{
printf("%d%d",i,head[i].value);
printf("\n");
}
}
struct Node* insert(int data,struct Node* head)
{
struct Node* current=NULL;
if(length(head) > 0)
{
int val=head->value;
if (val==-1)
{
head->value=data;
head->next=NULL;
}
else
{
current=malloc(sizeof(struct Node));
current->value=data;
current->next=head;
head=current;
}
}
else
{
printf("List is empty");
}
return head;
}
int main()
{
/* printf("Hello"); */
struct Node *head=init();
head=insert(20,head);
head=insert(30,head);
head=insert(40,head);
print(head);
printf("%d",length(head));
return 0;
}
The output values I get are:
Index Value
0 40
1 0
2 0
and for length is 3. I'm not able to grasp what I'm doing wrong here in pointer manipulation.
One obvious problem is not setting next to NULL on init - that would fail when checking length on the empty list
But your real problem is the print function
You can't use:
head[i].value
That notation is only valid for arrays, you need to use next to find each member
The Init function should set Next to NULL
struct Node* init()
{
struct Node* head=NULL;
head=malloc(sizeof(struct Node));
head->value=-1;
head->next=NULL;
return head;
}
otherwise the first call to length return an undefined result ( or GPF ).
Here:
for (i = 0; i < len; i++)
{
printf("%d%d", i, head[i].value);
printf("\n");
}
You need to advance from one node to another with head = head->next in the same manner as you do it in length(). head[i] won't do it.
It's unclear why your init() and insert() are so unnecessarily complicated and I don't even want to try to guess why. I want to suggest a better insert() and no init():
struct Node* insert(int data, struct Node* head)
{
struct Node* current;
current = malloc(sizeof(struct Node));
current->value = data;
current->next = head;
return current;
}
And then you do this:
int main(void)
{
struct Node *head = NULL;
head = insert(20, head);
head = insert(30, head);
head = insert(40, head);
print(head);
printf("%d", length(head));
return 0;
}
The notation head[i].value is only valid for arrays but not for linked lists. Arrays and linked lists are completely different, allocation of memory towards arrays is premeditated where as for linked lists it's dynamic. That is the reason why we use pointers for linked lists.
In init() you didn't assign null which causes the loop to run infinite times when you call length() for first time.
I am posting the modified code of print function:
void print(struct Node* head)
{
int i=0;
int len=0;
struct Node* current=head;
for(i=0;i<len;i++)
{
printf("%d %d",i,current->value);
print("\n");
current=current->next;
}
}

Resources