gpt4 book ai didi

c - C语言中的简单链表程序

转载 作者:行者123 更新时间:2023-11-30 20:04:19 26 4
gpt4 key购买 nike

//我用c实现了简单的链表程序在这里我实现在第一个、最后一个、特定位置插入..执行时发生这些错误:“*”标记之前预期有“=”、“,”、“;”、“asm”或“属性
..任何人都可以澄清这些错误是什么

#include<stdio.h>
#include<malloc.h>
#define ISEMPTY printf("\n empty list");

struct node{
int value;
struct node *next;
}
startnode * create_node(int);
void insert_node_first();
void insert_node_last();
void insert_node_position();

typedef struct node start_node;

start_node *newnode,*ptr,*prev,*temp;
start_node *first=NULL,*last=NULL;


int main()
{
int ch;
char ans='Y';
while(ans == 'Y' || ans == 'y')
{
printf("\n Single linked list \n");
printf("\n1.INSERT NODE AT FIRST");
printf("\n2.INSERT NODE AT LAST");
printf("\n3.INSERT NODE AT POSITION");
printf("\n ENTER YOUR CHOICE");
scanf("%d",&ch);
switch(ans)
{
case 1:printf("INSERT NODE AT FIRST");insert_node_first();break;
case 2:printf("INSERT NODE AT LAST");insert_node_last();break;
case 3:printf("INSERT NODE AT POSITION");insert_node_position;break;
default:printf("U DIDN'T SELECT ANYTHING SO EXCEED");
}
printf("DO YOU WANT TO CONTINUE");
scanf("%c",&ans);
}
return 0;
}
start_node* create_node(int val)
{
newnode=(start_node *)malloc(sizeof(start_node));
if(newnode==NULL)
{
printf("\n Memory not allocated");
return 0;
}
else{
newnode->value=val;
newnode->next=NULL;
return newnode;
}
}
void insert_node_first()
{
int val;
printf("Enter the value for the node");
scanf("%d",&val);
newnode=create_node(val);

if(first == last && first == NULL)
{
first=last=newnode;
first->next=NULL;
last->next=NULL;
}

else{
temp=first;
first=newnode;
first->next=temp;
}
printf("\n inserted");
}
void insert_node_last()
{
int val;
printf("Enter the value insert at last");
scanf("%d",&val);
newnode=create_node(val);
if(first == last && first == NULL)
{
first=last=newnode;
first->next=NULL;
last->next=NULL;
}
else{
last->next=newnode;
last=newnode;
last->next=NULL;
}
}
void insert_node_position()
{
int val,position,count,i;
printf("Enter the value to insert");
scanf("%d",&val);
newnode=create_node(val);
printf("Enter the position you want to ");
scanf("%d",&position);
ptr=first;
while(ptr !=NULL)
{
ptr=ptr->next;
count++;
}
if(position == 1)
{
if(first == last && first == NULL)
{
first=last=newnode;
first->next=NULL;
last->next=NULL;
}
else{
temp=first;
first=newnode;
first->next=temp;
}
}
else if(position > 1 && position<=count)
{
ptr=first;
for(i=1;i<position;i++)
{
prev=ptr;
ptr=ptr->next;
}
prev->next=newnode;
newnode->next=ptr;
printf("inserted at position");
}else{
printf("position is out of range");
}
}

最佳答案

startnode 更改为 start_node 并移动

start_node * create_node(int);

之后

typedef struct node start_node;

否则编译器此时不知道start_node是什么;

此外,您忘记了结构声明中的 ;

struct node{ 
int value;
struct node *next;
};

关于c - C语言中的简单链表程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41931814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com