gpt4 book ai didi

c - 如何在单链表的开始、结束和选定位置插入节点?

转载 作者:行者123 更新时间:2023-11-30 15:36:11 25 4
gpt4 key购买 nike

我是 C 编程新手,我尝试过在单链表程序中插入节点,但我没有得到正确的输出,而且我不知道如何纠正我的程序,如果有人知道请帮忙。

#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;
}

我认为错误出在我的主要函数中,但不清楚,请帮忙

最佳答案

在您的 addbegin 中方法至少有一个明显的错误:

if(head=NULL)

应该是

if (head == NULL)

因为您需要比较,而不是分配。

在您的 pos 中方法你有一个函数声明:int length();它不应该在那里,而应该在顶部,在 main 之前。

另一个问题,这次是在显示方法中:

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

这里 temp 将始终为 NULL,我猜你的意思是分配 headtemp指针,否则永远不会遍历列表。

最后,在 insert at specific position 中选择您需要询问位置值并将其传递给函数调用,因此添加 int loc; 的声明在 main 中,将第三种情况更改为:

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;

最后我要引用 C99 标准的5.1.2.2.1 程序启动部分:

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[]) { /* ... */ }

所以,请更改您的 main 声明并包括 return末尾的行(可能 return 0; 表示程序成功退出)。

这变得相当冗长。经过建议的更改后,您的程序应如下所示:

#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;
}

关于c - 如何在单链表的开始、结束和选定位置插入节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22626391/

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