single linked list primary expression error
I get the error, "expected primary expression before struct" , in the switch case in the display function on the line display(struct node* ptr);
.
Can anyone explain why?
#include<stdio.h>
#include<malloc.h>
void insbeg();
void delmid();
void display(struct node* ptr);
struct node
{
int data;
struct node* next;
}* start=NULL;
int main()
{
int n;
printf("1-insbegn");
printf("2-delmidn");
printf("3-displayn");
scanf("%d",&n);
switch(n)
{
case 1:
insbeg();
break;
case 2:
delmid();
break;
case 3:
display(struct node* ptr);
break;
}
}
void insbeg()
{
struct node* new1;
int x;
new1 = (struct node*)malloc(sizeof (struct node));
printf("enter the data:");
scanf("%d",&x);
new1->data = x;
if(start == NULL)
{
new1->next = NULL;
start=new1;
} else {
new1->next = start;
start = new1;
}
}
void delmid()
{
struct node* prev,* ptr;
int pos, i;
printf("enter the position:");
scanf("%d",&pos);
ptr = start;
for(i = 1;i <= pos; i++){
prev = ptr;
ptr = ptr->next;
}
prev->next = ptr->next;
free(ptr);
}
void display(struct node* ptr)
{
ptr = start;
display(ptr->next);
printf("%d",ptr->data);
}
You need to pass a node pointer when you call display
in case 3 of the switch statement, not just write struct node* ptr
, which doesn't make a node or a node pointer.
The node pointer you want to pass is the one you have already defined, start
.
Original case 3:
-
case 3:
display(struct node* ptr);
break;
New case 3:
-
case 3:
display(start);
break;
That will solve the error you get, but there is one more problem.
The display
function is recursive, and all recursive functions need a base case , or a stopping point. You need to stop display
from calling itself, whenever ptr->next
is null
. Just add in an if statement to your current code.
Your current display
function also does ptr=start
, but if ptr
is set to start
every time the function is called, the function will forever call itself and pass in ptr->next
which is the same as start->next
, since you just assigned start
to ptr
. So the line ptr=start
is not necessary. To make the display function go through all nodes from the beginning to the end, just pass in start
the first time the function is called, in case 3 of the switch (which we just did above).
Original display
function:
void display(struct node* ptr)
{
ptr=start;
display(ptr->next);
printf("%d",ptr->data);
}
display
function with base case
void display(struct node* ptr)
{
if(ptr->next != NULL) {
display(ptr->next);
}
printf("%d",ptr->data);
}
Not completely related to your problem, I did notice that there were a few styling issues .
You need to make sure that your code follows a style guide, like one listed here. Some of the big things to look out for are indentations, spacing, and variable/class naming conventions. Most importantly, choose one style and use it consistently.
Some of the things I noticed that you might want to look at:
Assigning values to variables, you write the line like int x=1;
. Most style guides recommend using a space before and after the =
like so: int x = 1;
.
The above tip also applies to other pieces of code, like for loops. A line in your code is: for(i=1;i<=pos;i++){
but this could look much better as: for(i = 1; i <= pos; i++){
When you make declare more than one variable, they should be made on separate lines unless they are of the same type. On this line struct node* new1; int x;
struct node* new1; int x;
, you should put the struct node* new1;
and int x;
on separate lines.
Naming your variables is very important. The variable's names should describe or at least hint at the purpose of the variables. Names like new1
or x
aren't very descriptive, but names like currentNode
and nodeValue
are.
There should be enough whitespace in between groups of similar code, to make the code more readable:
Original segment:
printf("enter the position:");
scanf("%d",&pos);
ptr=start;
for(i=1;i<=pos;i++){
prev=ptr;
ptr=ptr->next;
}
prev->next=ptr->next;
free(ptr);
With spacing:
printf("enter the position:");
scanf("%d",&pos);
ptr = start;
for(i = 1; i <= pos; i++){
prev = ptr;
ptr = ptr->next;
}
prev->next = ptr->next;
free(ptr);
These are just a few things that I thought I should point out. There are more things that you should consider when styling your code. But remember the most important thing you can do, is to pick one style and stick with it!
Of course, these are just suggestions and tips that I like to use/follow. There is no perfect answer, therefore my answer is not perfect. You shouldn't take my advice as if it is the only way, rather use it to help form your opinions. See where you think I am wrong, and think about how you can improve my answer. Find your style.
Definitely check out this great post, these tips, and this article for more info on code style/readability.
When calling display
you must pass a pointer to node. The pointer must be defined before the call as you can't define a new variable inside a function call (which is what you currently are doing).
Most likely you want to display the whole list so you should pass start
.
So instead of
case 3:display(struct node* ptr);
break;
it should be
case 3:display(start); // Pass start
break;
Further your display
function is incorrect. Don't do the assign ptr=start
as that would cause an endless loop (which will crash the program when you run out of stack).
So instead of
void display(struct node* ptr)
{
ptr=start;
display(ptr->next);
printf("%d",ptr->data);
}
it should be
void display(struct node* ptr)
{
if (ptr == NULL) return; // End the recursive function when ptr is NULL
display(ptr->next); // Recursive call to print the list backwards
printf("%d",ptr->data);
}
链接地址: http://www.djcxy.com/p/84398.html
下一篇: 单链表主要表达式错误