Question

I am new for c programming ,i have tried myself inserting node in singly linked list program but i didn't get a proper output and i dont have any idea to correct my program if anybody knows please help.

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
}*head;
int loc;

void addbegin(int num)
{
    struct node *temp;
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=num;
    if(head=NULL)
    {
        head=temp;
        head->next=NULL;
    }
    else
    {
        temp->next=head;
        head=temp;
    }
}

void addend(int num)
{
    struct node *temp1,*temp2;
    temp1=(struct node *)malloc(sizeof(struct node));
    temp1->data=num;
    temp2=head;
    if(head==NULL)
    {
        head=temp1;
        head->next=NULL;
    }
    else
    {
        while(temp2->next != NULL)
        temp2=temp2->next;
        temp1->next=NULL;
        temp2->next=temp1;
    }

}

void pos(int num,int loc)
{
    int length();
    struct node *temp,*cur_ptr,*prev_ptr;

    int i;
    cur_ptr=head;
    if(loc > (length()+1) || loc<= 0)
    {
        printf("it is illegal call:");
    }
    else 
    {
        if(loc == 1)
        {
            addbegin(num);
        }
        else
        {
            for(i=1;i<loc;i++)
            {
                prev_ptr=cur_ptr;
                cur_ptr=cur_ptr->next;
            }
            temp=(struct node*)malloc(sizeof(struct node));
            temp->data=num;
            prev_ptr->next=temp;
            temp->next=cur_ptr;
        }
    }
}

int length()
{
    struct node *cur_ptr;
    int count = 0;
    cur_ptr=head;
    while(cur_ptr!=NULL)
    {
        cur_ptr=cur_ptr->next;
        count++;
    }
    return(count);
}
void display()
{
    struct node *temp=NULL;
    if(temp==NULL)
    {
        printf("list is empty:");
    }
    while(temp!=NULL)
    {
        printf("%d",temp->data);
        temp=temp->next;
    }
}

int main()
{

    int num;
    head=NULL;
    int choice;
    while(1)
    {
    printf("\nList Operations\n");
    printf("===============\n");
    printf("1.Insert at begin\n");
    printf("2.insert at end\n");
    printf("3.insert at selected position\n");
    printf("4.Display\n");
    printf("5.Exit\n");
    printf("Enter your choice : ");
    if(scanf("%d",&choice)<=0)
    {
        printf("Enter only an Integer\n");

    } 

    printf("enter your choice:");
    scanf("%d",&choice);
    switch(choice)
    {
    case 1: printf("Enter the number to insert at begin : ");
            scanf("%d",&num);
            addbegin(num);
            break;
    case 2: printf("Enter the number to insert at end: ");
            scanf("%d",&num);
            addend(num);
            break;
    case 3: printf("Enter the number to insert at selected position: ");
            scanf("%d",&num);
            pos(num,loc);
            break;
    case 4: printf("display the values");
            display();
            break;
    case 5: printf("exit");

    display();
    }
    }
    return 0;
}

i think the error is in my main function but am not clear in that please help

Was it helpful?

Solution

In your addbeginmethod there's at least one obvious error:

if(head=NULL)

should be

if (head == NULL)

as you need to compare, not assign.

In your posmethod you have a function declaration: int length(); which shouldn't be there, but rather at the top, before main.

Another issue, this time in the display method:

void display()
{
    struct node *temp=NULL;
    if(temp==NULL) {
        printf("List is empty:");
    }
    while(temp!=NULL) {
        printf("%d",temp->data);
        temp=temp->next;
    }
}

Here temp will always be NULL, I guess you meant to assign headto the temppointer, otherwise it will never traverse the list.

And finally, in the insert at specific position choice you need to ask for a location value and pass that along too the function call, so add a declaration for int loc;in main, and change the third case to this:

case 3:
            printf("Enter the number to insert at selected position: ");
            scanf("%d",&num);
            printf("Enter the position: ");
            scanf("%d",&loc);
            pos(num,loc);
            break;

to the

Finally I'm going to quote from the C99 standard, section 5.1.2.2.1 Program startup:

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

So, please, change your declaration of mainand include a returnline at the end (possibly return 0;indicating successful program exit).

This became rather lengthy. After the suggested changes your program should look something like this:

#include<stdio.h>
#include<stdlib.h>

struct node {
    int data;
    struct node *next;
}*head;

void addbegin(int num)
{
    struct node *temp;
    temp = malloc(sizeof(struct node));
    temp->data=num;
    if(head==NULL) {
        head=temp;
        head->next=NULL;
    } else {
        temp->next=head;
        head=temp;
    }
}

void addend(int num)
{
    struct node *temp1, *temp2;
    temp1 = malloc(sizeof(struct node));
    temp1->data = num;
    temp2 = head;
    if(head == NULL) {
        head = temp1;
        head->next = NULL;
    } else {
        while(temp2->next != NULL)
            temp2=temp2->next;
        temp1->next=NULL;
        temp2->next=temp1;
    }
}

int length()
{
    struct node *cur_ptr;
    int count = 0;
    cur_ptr = head;
    while(cur_ptr != NULL) {
        cur_ptr = cur_ptr->next;
        count++;
    }
    return (count);
}

void pos(int num, int loc)
{
    struct node *temp, *cur_ptr, *prev_ptr;

    int i;
    cur_ptr=head;
    if(loc > (length()+1) || loc<= 0) {
        printf("it is illegal call:");
    } else {
        if(loc == 1) {
            addbegin(num);
        } else {
            for(i=1; i<loc; i++) {
                prev_ptr=cur_ptr;
                cur_ptr=cur_ptr->next;
            }
            temp = malloc(sizeof(struct node));
            temp->data=num;
            prev_ptr->next=temp;
            temp->next=cur_ptr;
        }
    }
}

void display()
{
    struct node *temp = head;
    if(temp == NULL) {
        printf("List is empty:");
    }

    printf("The list contains the following values:\n");
    while(temp!=NULL) {
        printf("%d\n",temp->data);
        temp=temp->next;
    }
}

int main()
{
    int choice, num, loc;
    head = NULL;

    while(1) {
        printf("\nList Operations\n");
        printf("===============\n");
        printf("1.Insert at begin\n");
        printf("2.insert at end\n");
        printf("3.Insert at selected position\n");
        printf("4.Display\n");
        printf("5.Exit\n");
        printf("Enter your choice : ");
        if(scanf("%d",&choice)<=0) {
            printf("Enter only an Integer\n");
        }

        switch(choice) {
        case 1:
            printf("Enter the number to insert at begin : ");
            scanf("%d",&num);
            addbegin(num);
            break;
        case 2:
            printf("Enter the number to insert at end: ");
            scanf("%d",&num);
            addend(num);
            break;
        case 3:
            printf("Enter the number to insert at selected position: ");
            scanf("%d",&num);
            printf("Enter the position: ");
            scanf("%d",&loc);
            pos(num,loc);
            break;
        case 4:
            printf("Display the values\n");
            display();
            break;
        case 5:
            printf("exit");
            exit(0); // maybe you should exit here.
            display();
        }
    }

    return 0;
}

OTHER TIPS

void addbegin(int num)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
  if(head==NULL)
  {
    head=temp;
    head->next=NULL;
  }
  else
  {
    temp->next=head;
    head=temp;
  }
}

and

void display()
{
struct node *temp=NULL;
temp = head;
  if(temp==NULL)
  {
    printf("list is empty:");
  }
  while(temp!=NULL)
  {
    printf("%d",temp->data);
    temp=temp->next;
  }
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
}*head;

int loc;

void addbegin(int num)
{
    struct node *temp;
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=num;
    if(head==NULL) //this not assign, you need == to compare
    {
        head=temp;
        head->next=NULL;
    }
    else
    {
        temp->next=head;
        head=temp;
    }
}

void addend(int num)
{
    struct node *temp1,*temp2;
    temp1=(struct node *)malloc(sizeof(struct node));
    temp1->data=num;
    temp2=head;
    if(head==NULL)
    {
        head=temp1;
        head->next=NULL;
    }
    else
    {
        while(temp2->next != NULL)
            temp2=temp2->next;
        temp1->next=NULL;
        temp2->next=temp1;
    }

}

int length()
{
    struct node *cur_ptr;
    int count = 0;
    cur_ptr=head;
    while(cur_ptr!=NULL)
    {
        cur_ptr=cur_ptr->next;
        count++;
    }
    return(count);
}

void pos(int num,int loc)
{
    struct node *temp,*cur_ptr,*prev_ptr;

    int i;
    cur_ptr=head;
    if(loc > (length()+1) || loc<= 0)
    {
        printf("it is illegal call:");
    }
    else 
    {
        if(loc == 1)
        {
            addbegin(num);
        }
        else
        {
            for(i=1;i<loc;i++)
            {
                prev_ptr=cur_ptr;
                cur_ptr=cur_ptr->next;
            }
            temp=(struct node*)malloc(sizeof(struct node));
            temp->data=num;
            prev_ptr->next=temp;
            temp->next=cur_ptr;
        }
    }
}


void display()
{
    struct node *temp=head;
    if(temp==NULL)
    {
        printf("list is empty:");
    }
    while(temp!=NULL)
    {
        printf("%d ",temp->data);
        temp=temp->next;
    }
    printf("\n");
}

int main()
{

    int num;

    int choice;
    while(1)
    {
        printf("\nList Operations\n");
        printf("===============\n");
        printf("1.Insert at begin\n");
        printf("2.insert at end\n");
        printf("3.insert at selected position\n");
        printf("4.Display\n");
        printf("5.Exit\n");
        printf("Enter your choice : ");
        if(scanf("%d",&choice)<=0)
        {
            printf("Enter only an Integer\n");

        } 

        switch(choice)
        {
        case 1: printf("Enter the number to insert at begin : ");
            scanf("%d",&num);
            addbegin(num);
            break;
        case 2: printf("Enter the number to insert at end: ");
            scanf("%d",&num);
            addend(num);
            break;
        case 3: printf("Enter the number to insert at selected position: ");
            scanf("%d",&num);
            pos(num,loc);
            break;
        case 4: printf("\ndisplay the values: ");
            display();
            break;
        case 5: printf("exit");
            display();
        }
    }
    return 0;
}

Try this

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
}*head;

int loc;

void addbegin(int num)
{
    struct node *temp;
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=num;
    if(head==NULL) //this not assign, you need == to compare
    {
        head=temp;
        head->next=NULL;
    }
    else
    {
        temp->next=head;
        head=temp;
    }
}

void addend(int num)
{
    struct node *temp1,*temp2;
    temp1=(struct node *)malloc(sizeof(struct node));
    temp1->data=num;
    temp2=head;
    if(head==NULL)
    {
        head=temp1;
        head->next=NULL;
    }
    else
    {
        while(temp2->next != NULL)
            temp2=temp2->next;
        temp1->next=NULL;
        temp2->next=temp1;
    }

}

int length()
{
    struct node *cur_ptr;
    int count = 0;
    cur_ptr=head;
    while(cur_ptr!=NULL)
    {
        cur_ptr=cur_ptr->next;
        count++;
    }
    return(count);
}

void pos(int num,int loc)
{
    struct node *temp,*cur_ptr,*prev_ptr;

    int i;
    cur_ptr=head;
    if(loc > (length()+1) || loc<= 0)
    {
        printf("it is illegal call:");
    }
    else 
    {
        if(loc == 1)
        {
            addbegin(num);
        }
        else
        {
            for(i=1;i<loc;i++)
            {
                prev_ptr=cur_ptr;
                cur_ptr=cur_ptr->next;
            }
            temp=(struct node*)malloc(sizeof(struct node));
            temp->data=num;
            prev_ptr->next=temp;
            temp->next=cur_ptr;
        }
    }
}


void display()
{
    struct node *temp=head;
    if(temp==NULL)
    {
        printf("list is empty:");
    }
    while(temp!=NULL)
    {
        printf("%d ",temp->data);
        temp=temp->next;
    }
    printf("\n");
}

int main()
{

    int num;

    int choice;
    while(1)
    {
        printf("\nList Operations\n");
        printf("===============\n");
        printf("1.Insert at begin\n");
        printf("2.insert at end\n");
        printf("3.insert at selected position\n");
        printf("4.Display\n");
        printf("5.Exit\n");
        printf("Enter your choice : ");
        if(scanf("%d",&choice)<=0)
        {
            printf("Enter only an Integer\n");

        } 

        switch(choice)
        {
        case 1: printf("Enter the number to insert at begin : ");
            scanf("%d",&num);
            addbegin(num);
            break;
        case 2: printf("Enter the number to insert at end: ");
            scanf("%d",&num);
            addend(num);
            break;
        case 3: printf("Enter the number to insert at selected position: ");
            scanf("%d",&num);
            printf("Enter the position to insert: 1 for insert at begin, so on: ");
            scanf("%d",&loc);
            pos(num,loc);
            break;
        case 4: printf("\ndisplay the values: ");
            display();
            break;
        case 5: 
            display();
        printf("\n exiting program \n");
        exit(0);
        }
    }
    return 0;
}

Try this:

#include<iostream>
using namespace std;

struct stu{
    int id;
    stu *next = NULL;   
};

stu *first = NULL;
stu *last = NULL;


int opt;

void insert_end();
void display();


int main(){
    
    do{
        
        cout<<"\n\n0.Exit";
        cout<<"\n1.Insert at end in linked list";
        cout<<"\n2.Display linked list";
        
        
        cout<<"\n\nEnter your choice: ";
        cin>>opt;
        
        switch(opt){
            case 1:{
                insert_end_list1();
                break;
            }
            case 2:{
                display_list1();
                break;
            }
        }
        
    }
    while(opt != 0);
    
    return 0;
}

void insert_end(){
    
    stu *current = new stu;
    
    cout<<"\n\nEnter the student id:";
    cin>>current->id;
    
    if(first == NULL){
        cout<<"\n\nEmpty linked list";
        first = last = current;
    }
    else{
        last->next = current;
        last = current;
    }
    
}

void display(){
    
    stu *p = first;
    
    while(p != NULL){
        cout<<p->id<<" ";
        p = p->next;
    }
    
}

This code uses structure for creation of a new node in singly linked list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top