Question:

Correct my c prog-linked list ...plz......?

by  |  earlier

0 LIKES UnLike

include<stdio.h>

#include<conio.h>

#include<alloc.h>

struct node

{

int data;

struct node* next;

};

struct node* temp,*p,*head=NULL,*t,*n;

int no,position,i;

void create();

void infirst();

void inmid();

void inlast();

void delfirst();

void delmid();

void dellast();

void display();

void create()

{

for (i=0;i<4;i++)

{

if (head==NULL)

{

{

temp=malloc(sizeof(struct node));

printf("enter the no");

scanf("%d",&no);

temp->data=no;

temp->next=NULL;

head=temp;

}

else

{

p=malloc(sizeof (struct node));

printf("enter the no");

scanf("%d",&no);

p->data=no;

p->next=NULL;

p->next=head;

head=p;

}

}

}

void inmid()

{

n=malloc(sizeof (struct node));

printf("enter the no");

scanf("%d",&no);

n->data=no;

n->next=NULL;

printf("enter the position");

scanf("%d",&position);

temp=head;

i=1;

while(i<position)

{

p=temp;

temp=p->next;

i++;

}

p->next=n;

n->next=temp;

}

void infirst()

{

t=malloc(sizeof (struct node));

printf("enter the no");

scanf("%d",&no);

t->data =no;

t->next=head;

p=head;

head=t;

}

void inlast()

{

t=malloc (sizeof (struct node));

printf("enter the no");

scanf("%d",&no);

t->data=no;

t->next=NULL;

temp=head;

while(temp->next!=NULL)

{

temp=temp->next;

}

temp->next=t;

}

void delfirst()

{

t=head;

head=t->next;

free(t);

}

void delmid()

{

temp=head;

i=1;

while(i<position)

{

p=temp;

temp=p->next;

i++;

}

p->next=temp->next;

t=temp;

free(t);

}

void dellast()

{

temp=head;

while(temp->next!NULL)

{

p=temp;

temp=p->next;

}

p->next=NULL;

free(temp);

}

void display()

{

for(temp=head;temp->next!=NULL;temp=te...

{

printf("%d",temp->data);

}

printf("\n****");

}

void main()

{

int choice;

do

{

printf("\n **MENU**");

printf("\n1.creation \n2.insert first \n3.insert middle \n4.insert last \n5.delete first \n6.delete middle \n7.last \n8.display");

printf(" \n enter the choice");

scanf("%d",& choice);

switch(choice)

{

case 1:

create();

display();

break;

case 2:

infirst();

display();

break;

case 3:

inmid();

display();

break;

case 4:

inlast();

display();

break;

case 5:

delfirst();

display();

break;

case 6:

delmid();

display();

break;

case 7:

dellast();

display();

break;

default:

printf("invalid choice");

}

}

while (choice!=8)

getch();

}

ERROR: line 26: cannot covert 'void *' to 'node *'

line 33:misplaced else

line 35: cannot covert 'void *' to 'node *'

line 46: declaration syntax error

line 174:(last line) : declaration missing ;

line 174: compound statement missing }

 Tags:

   Report

3 ANSWERS


  1. Missing semicolon after void(create).


  2. E-mail me at bbi5291@gmail.com with the properly indented source code attached. I will point out - and correct - all of your errors. Because there are more than what you just listed. For example, on the line &quot;while(temp-&gt;next!NULL)&quot;, you are missing the equal sign from &quot;!=&quot;.

  3. malloc returns a void*.  temp is a node*.  You need to cast to node*:

    temp=(node*)malloc(sizeof(struct node));

    in all the malloc calls

    if (head==NULL)

    {

    {

    too many braces, that&#039;s the &quot;misplaced else &quot; error

    In dellast while(temp-&gt;next!NULL)

    shoud be while(temp-&gt;next != NULL)

    In main, need a semicolon after while stmt

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.