Creating Linked-list of characters and printing it - c

I'm trying to implement a Linked list using this code.This code complies successfully but results in Segmentation fault (core dumped) error.How can I resolve this?
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node *head=(struct node *)malloc(sizeof(struct node));
struct node *p1=NULL;
void addnode(char ch) {
if(head==NULL) {
head->ch=ch;
head->next=NULL;
}
else {
struct node *New=(struct node *) malloc (sizeof(struct node));
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
}
}
void main() {
char ch,frm,to;
printf("\nEnter the string");
while((ch=getchar())!='\0')
addnode(ch);
for(p1=head;p1!=NULL;p1=p1->next)
printf("\n%c",p1->ch);
}

Simple Mistakes first: When you allocate memory in global you initiate a function call(malloc is also a function). Function calls can only be made inside main or other funcitons. So just declare head don't use malloc in global.
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node *head=NULL;
struct node *p1=NULL;
void addnode(char ch) {
if(head==NULL) {
struct node *New=(struct node *) malloc (sizeof(struct node));
head=New;
New->ch=ch;
New->next=NULL;
}
else {
struct node *New=(struct node *) malloc (sizeof(struct node));
New->ch=ch;
New->next=NULL;
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
}
}
void main() {
char ch,frm,to;
printf("\nEnter the string");
while((ch=getchar())!='\n')
addnode(ch);
for(p1=head;p1!=NULL;p1=p1->next)
printf("\n%c",p1->ch);
}
Second mistake: Inside your addnode function when you chekk if head is null or not allocate some memory and assign it to head.
Third mistake: in your getchar() check until you find a new line not null character.
Fourth mistake: Assign ch to New and set New->next=null. You almost completely forgot that.

This worked better and I over came the errors :). And i missed pointers clarity there, which were rectified here..
#include<stdio.h>
#include<stdlib.h>
struct Node{
char ch;
struct Node *next;
};
struct Node head={'\0',NULL};
struct Node *p1=NULL;
void add(char ch){
if(head.ch=='\0')
head.ch=ch;
else{
struct Node *new=(struct node *)malloc(sizeof(struct Node));
new->ch=ch;
for(p1=&head;p1->next!=NULL;p1=p1->next);
p1->next=new;
}
}
void main(){
char c;
while((c=getchar())!='\n')
add(c);
for(p1=&head;p1!=NULL;p1=p1->next)
printf("%c\n",p1->ch);
}
But i still receive a warning saying,
initialization from incompatible pointer type [enabled by default]
struct Node *new=(struct node *)malloc(sizeof(struct Node));
^

I am not sure this is c way..but you have to think your code over how to release your allocated pointer...like free list function maybe..
Here is my way.
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node * addnode(struct node *head, struct node *p1, char ch) {
if(head==NULL) {
printf("......return 2... \r\n");
head=(struct node *)malloc(sizeof(struct node));
head->ch=ch;
head->next=NULL;
return head;
}
else {
struct node *New=NULL;
printf("......return ... \r\n");
New=(struct node *) malloc (sizeof(struct node));
New->ch = ch;
New->next=NULL;
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
return head;
}
}
void main() {
char ch,frm,to;
struct node *head=NULL, *p1=NULL;
printf("\nEnter the string \n");
while((ch=getchar())!='q')
head = addnode(head, p1, ch);
for(p1=head;p1!=NULL;p1=p1->next)
{
printf("\n%c",p1->ch);
}
}
Another one.
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
char ch;
struct node *next;
} *pNODE, NODE;
pNODE addnode2(pNODE head, pNODE p1, char ch) {
if(head==NULL) {
printf("......return 2... \r\n");
head=(pNODE)malloc(sizeof(NODE));
head->ch=ch;
head->next=NULL;
return head;
}
else {
struct node *new=NULL;
printf("......return ... \r\n");
new=(pNODE) malloc (sizeof(NODE));
new->ch = ch;
new->next=NULL;
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=new;
return head;
}
}
void main() {
char ch,frm,to;
pNODE head=NULL;
pNODE p1=NULL;
printf("\nEnter the string \n");
while((ch=getchar())!='q')
head = addnode2(head, p1, ch);
for(p1=head;p1!=NULL;p1=p1->next)
{
printf("\n%c",p1->ch);
}
}

Related

How to return pointer adress for structure variable

I was working on data structures and while I was writing some code, I needed to return the address of the pointer that was defined in the structure. So here's my code but when I compile and run it, it doesn't work and give an error message as " assignment makes pointer from integer without a cast ". How should I rewrite it?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node{
int x;
struct Node *next;
};
void main(){
int i;
struct Node *head;
head = (struct Node*) malloc(sizeof(struct Node));
head->next=NULL;
/*Ekle(5,head);
Ekle(10,head);
Ekle(15,head);
printf("Enter the value of 'ara' element");
scanf("%d",&i);
Print(head);
ArayaEkle(i,20,head);
Print(head);*/
head = siraliEkle(10,head);
head = siraliEkle(5,head);
Print(head);
}
void Print(struct Node *root){
while(root->next!=NULL){
root = root->next;
printf("%d\n",root->x);
}
}
struct Node *siraliEkle(int sayi, struct Node *root){
if(root==NULL){
root = (struct Node*) malloc(sizeof(struct Node));
root->x = sayi;
root->next = NULL;
return root;
}
else if(root->next==NULL){
if(root->x>sayi){
struct Node *temp = (struct Node*) malloc(sizeof(struct Node));
temp->x = sayi;
temp->next = root;
root = temp;
return root;
}
}
}
You must declare (or define) functions before using them.
Without declaration nor definition, types of function arguments are assumed to int and it will cause trouble when actual types are not int.
struct Node{
int x;
struct Node *next;
};
/* add these declarations */
void Print(struct Node *root);
struct Node *siraliEkle(int sayi, struct Node *root);
int main(){ /* also return type should be standard int */
Also don't forget to return something from siraliEkle even if root != NULL && (root->next != NULL || root->x <= sayi).
If you want to return the address for the struct pointer use the declaration
struct Node *siraliEkle(int sayi, struct Node *root);
As below and return:
return &root;

Consistent segmentation fault on recursive appending and new node creation in a Linked List C

I got a struct with 2 fields (int and char*) and I should create a list containing nodes of this type, problem is recursive algorithms seem correct but every time I compile it gives me a SIGSEGV error due to the assignment new_node->data->number = stud->number, more specifically it results data = {struct fields* | 0x0} NULL and next = {struct node* | 0x0} NULL. Any Help?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct fields{
int number;
char *name;
};
struct Node {
struct fields *data;
struct Node* next;
};
struct Node *newNode(struct fields* data);
struct Node* insertEnd(struct Node* head, struct fields* data);
struct Node* insertEnd(struct Node* head, struct fields* data);
void traverse(struct Node* head);
void init_fields (struct fields* stud);
// Allocates a new node with given data
struct Node *newNode(struct fields* data) {
struct Node *new_node = malloc (1000*sizeof(*new_node));
new_node->data->number = data->number;
new_node->data->name = data->name;
new_node->next = NULL;
return new_node;
}
struct Node* insertEnd(struct Node* head, struct fields* data){
if (head == NULL)
return newNode(data);
else
head->next = insertEnd(head->next, data);
return head;
}
void traverse(struct Node* head){
if (head == NULL)
return;
printf("\n%d %s\n", head->data->number, head->data->name);
traverse(head->next);
}
int main(){
struct Node* head = NULL;
struct fields* stud = malloc(1000* sizeof(*stud));
init_fields (stud);
if (stud == NULL){
exit(EXIT_FAILURE);
}
printf("\nInsert number: ");
scanf("%d", &stud->number);
fflush(stdin);
printf("\nInsert name: \n");
scanf(" %[^\n]s", stud->name);
head = insertEnd(head, stud);
printf("\nInsert number: ");
scanf("%d", &stud->number);
fflush(stdin);
printf("\nInsert name: \n");
scanf(" %[^\n]s", stud->name);
head = insertEnd(head, stud);
traverse(head);
}
void init_fields (struct fields* stud){
stud->number = 0;
stud->name = malloc(50*sizeof(char));
}
You need to allocate data
struct Node *newNode(struct fields* data) {
struct Node *new_node = malloc (1000*sizeof(*new_node));
new_node->data = malloc(sizeof(*new_node->data));
new_node->data->number = data->number;
new_node->data->name = data->name;
new_node->next = NULL;
return new_node;
}
In the future, you can find where exactly the problem was using gdb or lldb. It will point you to the exact line, and the problem is obvious from there.
Here:
struct Node *new_node = malloc (1000*sizeof(*new_node));
you are only allocating memory for new_node. What about this field?
struct fields *data;
It must be initialized somehow.

Program doesn't display Linked list but creates a linked list in c

I am weak in linked list so please help me:
My LinkedList is not displaying on screen(display function isn't working correctly) even the linked list is creating successfully and if i use node *head as a global pointer to structure and if i replaced list->head with head (which is the address of node *head) and LinkedList *list (in argument of function) with node *head, then it is printing the whole list but if i use list->head or list in place of head (which is the address of node *head) and declared LinkedList *list in term of node *head then it input the value in linked list but don't display the linked list.
The code which causing problem is following:
#include<stdio.h>
#include<malloc.h>
typedef struct node{
int data;
struct node *next;
}node;
typedef struct LinkedList_
{
node *head;
} LinkedList;
void create( LinkedList *list ){
char choice='y';
do{
node *newnode,*temp;
newnode=(node*)malloc(sizeof(node*));
printf("\nenter the data: ");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(list==NULL){
list->head=newnode;
temp=newnode;
}
else{
temp=list->head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newnode;
}
printf("\ndo you want to continue(y or no)? ");
choice=getche();
}while(choice=='y');
}
void display( LinkedList *list ){
printf("\n[");
while(list->head!=NULL){
printf("%d,",list->head->data);
list->head=list->head->next;
}
printf("]");
}
void main(){
LinkedList *head=NULL;
create(head);
display(head);
}
fix like this
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct node{
int data;
struct node *next;
}node;
typedef struct LinkedList_ {
node *head;
} LinkedList;
void create( LinkedList *list ){
char choice='y';
if(!list || list->head){//guard
fprintf(stderr, "Invalid call %s.\n", __func__);
return;
}
node *temp;
do{
node *newnode;
newnode = malloc(sizeof(*newnode));//or malloc(sizeof(node));
printf("\nenter the data: ");
scanf("%d", &newnode->data);
newnode->next = NULL;
if(list->head == NULL){
list->head = newnode;
temp = newnode;
} else {
temp = temp->next = newnode;//There is no need to follow the link
}
printf("\ndo you want to continue(y or no)? ");
choice=getche();
}while(choice=='y');
}
void display( LinkedList *list ){
node *curr = list->head;//head should not be changed
printf("\n[");
while(curr != NULL){
printf("%d,", curr->data);
curr = curr->next;
}
printf("]\n");
}
int main(void){
LinkedList head = { NULL };//requires entity
create(&head);
display(&head);
}

How to declare struct variable with in the main function in c?

I am new for c programming, my code is working but my question is, if I declare struct node *a,*b; in main function ,how to passing a and b to void create() .And why it's not working, can someone please help me understand it?
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int d;
struct node *next;
}*start=NULL;struct node *a,*b; //move this part to main function -> struct node *a,*b; but its not working
void create()
{
a=(struct node *)malloc(sizeof(struct node));
printf("Enter the data : ");
scanf("%d",&a->d);
a->next=NULL;
if(start==NULL)
{
start=a;
b=a;
}
else
{
b->next=a;
b=a;
}
}
void display()
{
struct node *a;
printf("\nThe Linked List : ");
a=start;
while(a!=NULL)
{
printf("%d--->",a->d);
a=a->next;
}
printf("NULL\n");
}
void main()
{
char ch;
do
{
create();
printf("Do you want to create another : ");
ch=getche();
}
while(ch!='n');
display();
}
void freenodes()
{
struct node *a;
a = start;
while(a != NULL)
{
struct node *freenode = a ;
a = a->next;
free(freenode) ;
}
}
void main()
{
struct node name_of_varialbe;//this is struct variable declaration
...
}
Just change the prototype of void create() to void create(struct node *,struct node *) and call this way:
char ch;
do
{
struct node *a_main=(struct node *)malloc(sizeof(struct node));
struct node *b_main=(struct node *)malloc(sizeof(struct node));
create(a_main,b_main);
printf("Do you want to create another : ");
ch=getche();
}while(ch!='n');
void create(struct node *a,struct node *b)
{
//do your stuff
}

I want to swap the linkedlist terms pairwise, my code is giving me Segmentation Fault

I want to swap the linkedlist terms pairwise.
Here is my code. It is giving me Segmentation Fault-core dumped
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void insert(struct node *n)
{
int num;
printf("Enter a number : ");
scanf("%d",&num);
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if(n==NULL)
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
int main()
{
struct node *n;
head=NULL;
n=head;
int i;
for(i=0;i<6;i++)
{
insert(n);
n=head;
}
display(n);
pairswap(n);
display(n);
}
void display(struct node *n)
{
struct node *temp;
temp=n;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
void pairswap(struct node *n)
{
struct node *temp,*temp1,*temp2;
temp=n;
temp1=temp->next;
while(temp!=NULL)
{
int tempnum;
tempnum=temp->data;
temp->data=temp1->data;
temp1->data=tempnum;
if(temp==n)
{
head=temp;
head->next=temp1;
}
else
{
temp2->next=temp;
temp2->next->next=temp1;
}
temp2=temp1;
temp=(temp->next)->next;
temp1=temp->next;
}
n=head;
}
Please learn about the debuggers.
Somewhere at the end of the list the value of
(temp->next)->next
is NULL, which you are putting it in variable temp.
Before making this assignment temp1=temp->next, you need to check if temp is NULL and take proper action.
The problem is pairswap function. In the while loop you are using the condition:
while(temp!=NULL)
and inside the loop you are assigning:
temp=(temp->next)->next
which will get crashed on the second last node since you are trying to dereference NULL pointer.
Use the following condition:
while((temp->next)->next!=NULL)
or
while (temp1->next != NULL)

Resources