在c语言中插入链表
我想遍历链表并打印链表的所有元素,直到结束。 我执行以下操作在c语言中插入链表,c,linked-list,C,Linked List,我想遍历链表并打印链表的所有元素,直到结束。 我执行以下操作 #include<stdio.h> #include<stdlib.h> #include<malloc.h> int main() { struct linkedList //making user defined linked list { int num; struct linkedList *ptr; }; int choi
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
int main()
{
struct linkedList //making user defined linked list
{
int num;
struct linkedList *ptr;
};
int choice=1,last=0;
typedef struct linkedList node;
node *temp,*lasst,*head; //initialization of pointers.
while(choice==1)
{
temp=(node *)malloc(sizeof(node)); //allocation of memory to temp
printf("enter num");
scanf("%d",&temp->num);
if(last==0)
{
lasst=head=temp;
}
else
{
lasst->ptr=temp;
lasst=temp;
}
printf("do u want to enter more data? type 1");
scanf("%d",&choice);
}
lasst->ptr=0;
temp=head;
while(temp!=0)
{
printf("%d =>",temp->num);
temp=temp->ptr;
}
}
#包括
#包括
#包括
int main()
{
struct linkedList//创建用户定义的链表
{
int-num;
结构链接列表*ptr;
};
int choice=1,last=0;
typedef结构linkedList节点;
node*temp、*lasst、*head;//指针的初始化。
while(选项==1)
{
temp=(node*)malloc(sizeof(node));//为temp分配内存
printf(“输入num”);
scanf(“%d”,&temp->num);
如果(最后==0)
{
lasst=水头=温度;
}
其他的
{
lasst->ptr=温度;
lasst=温度;
}
printf(“是否要输入更多数据?类型1”);
scanf(“%d”,选择(&C);
}
lasst->ptr=0;
温度=水头;
while(温度!=0)
{
printf(“%d=>”,temp->num);
温度=温度->ptr;
}
}
我想打印链表中的所有元素,但我的代码只打印链表的最后一个元素,我该怎么办?这是链表中插入节点的完整代码。它可以在节点的开始、结束、之后和之前插入节点
#include<stdio.h>
//#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
}*new1,*first=NULL,*save,*pred;
struct node *insert_begin(int x,struct node *first);
struct node *insert_end(int x,struct node *first);
struct node *insert_after(int x,struct node *first);
struct node *insert_before(int x,struct node *first);
void display(struct node *first);
void main()
{
int x,ch,c=0;
//clrscr();
do
{
printf("\n_____________________________________________\n");
printf("\n1 - Insert Node At the Begining of the Node");
printf("\n2 - Insert Node At the Ending of the Node");
printf("\n3 - Insert Node At the After Particular Node");
printf("\n4 - Insert Node At the Before Particular Node");
printf("\n_____________________________________________\n");
printf("\n\tEnter Your Choice Here -->");
scanf("%d",&ch);
printf("\nEnter Value To Insert In The Node -->");
scanf("%d",&x);
switch(ch)
{
case 1:
first=insert_begin(x,first);
break;
case 2:
first=insert_end(x,first);
break;
case 3:
first=insert_after(x,first);
break;
case 4:
first=insert_before(x,first);
break;
default:
printf("\nPlease Reenter Choice....");
break;
}
display(first);
printf("\nDo You Want To Continue.. Press 1 For Continue -->");
scanf("%d",&c);
}while(c==1);
//getch();
}
void display(struct node *first)
{
printf("\n**********************************************\n");
printf("\n-----NODES ARE BELOW-----\n");
while(first!=NULL)
{
printf(" [ %d ] ",first->info);
first=first->link;
}
printf("\n**********************************************\n");
}
struct node *insert_begin(int x,struct node *first)
{
new1=(struct node*)malloc(sizeof(struct node));
new1->info=x;
new1->link=NULL;
if(first==NULL)
{
first=new1;
}
else
{
new1->link=first;
first=new1;
}
return first;
}
struct node *insert_end(int x,struct node *first)
{
new1=(struct node*)malloc(sizeof(struct node));
new1->info=x;
new1->link=NULL;
save=first;
if(first==NULL)
{
first=new1;
// save=first;
}
else
{
while(save->link!=NULL)
{
save=save->link;
}
save->link=new1;
}
// save=first;
return first;
}
struct node *insert_after(int x,struct node *first)
{
int v;
new1=(struct node*)malloc(sizeof(struct node));
new1->info=x;
new1->link=NULL;
save=first;
printf("\nEnter Node Value Which Insert After Above Node =");
scanf("%d",&v);
if(first==NULL)
{
first=new1;
save=first;
}
else
{
if(first->link==NULL)
{
new1->link=first;
first=new1;
}
else
{
while(save->link!=NULL && save->info!=v)
{
save=save->link;
}
new1->link=save->link;
save->link=new1;
}
}
save=first;
return first;
}
struct node *insert_before(int x,struct node *first)
{
int v;
new1=(struct node*)malloc(sizeof(struct node));
new1->info=x;
new1->link=NULL;
save=first;
printf("\nEnter Value of node Which Insert Before Above Node =");
scanf("%d",&v);
if(first==NULL)
{
first=new1;
}
else
{
if(first->link==NULL)
{
new1->link=first;
first=new1;
}
else
{
while(save->link!=NULL && save->info!=v)
{
pred=save;
save=save->link;
}
pred->link=new1;
new1->link=save;
}
}
return first;
}
#包括
//#包括
#包括
结构节点
{
国际信息;
结构节点*链接;
}*new1,*first=NULL,*save,*pred;
结构节点*插入\开始(int x,结构节点*第一);
结构节点*插入\结束(int x,结构节点*第一);
结构节点*在后面插入_(int x,结构节点*第一);
结构节点*在前面插入_(int x,结构节点*第一);
无效显示(结构节点*第一);
void main()
{
int x,ch,c=0;
//clrsc();
做
{
printf(“\n\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu\n”);
printf(“\n1-在节点的开头插入节点”);
printf(“\n2-在节点末尾插入节点”);
printf(“\n3-在特定节点后插入节点”);
printf(“\n4-在特定节点之前插入节点”);
printf(“\n\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu\n”);
printf(“\n\t在此处输入您的选择-->”;
scanf(“%d”和“ch”);
printf(“\n要插入到节点-->”中的输入值”);
scanf(“%d”和&x);
开关(ch)
{
案例1:
first=插入\开始(x,第一);
打破
案例2:
first=插入_端(x,第一);
打破
案例3:
first=在(x,first)之后插入_;
打破
案例4:
first=在(x,first)之前插入_;
打破
违约:
printf(“\n请重新输入选项…”);
打破
}
显示(第一);
printf(“\n是否继续..按1继续-->”;
scanf(“%d”、&c);
}而(c==1);
//getch();
}
无效显示(结构节点*第一个)
{
printf(“\n******************************************************\n”);
printf(“\n------节点在------\n”下面);
while(第一个!=NULL)
{
printf(“[%d]”,第一个->信息);
第一个=第一个->链接;
}
printf(“\n******************************************************\n”);
}
结构节点*插入\开始(int x,结构节点*第一个)
{
new1=(结构节点*)malloc(sizeof(结构节点));
new1->info=x;
new1->link=NULL;
if(first==NULL)
{
first=new1;
}
其他的
{
new1->link=first;
first=new1;
}
先返回;
}
结构节点*插入\结束(int x,结构节点*第一个)
{
new1=(结构节点*)malloc(sizeof(结构节点));
new1->info=x;
new1->link=NULL;
保存=第一;
if(first==NULL)
{
first=new1;
//保存=第一;
}
其他的
{
while(保存->链接!=NULL)
{
保存=保存->链接;
}
保存->链接=new1;
}
//保存=第一;
先返回;
}
结构节点*在后面插入(int x,结构节点*第一个)
{
INTV;
new1=(结构节点*)malloc(sizeof(结构节点));
new1->info=x;
new1->link=NULL;
保存=第一;
printf(“\n输入在上述节点“”之后插入的节点值”);
scanf(“%d”和“&v”);
if(first==NULL)
{
first=new1;
保存=第一;
}
其他的
{
如果(第一->链接==NULL)
{
new1->link=first;
first=new1;
}
其他的
{
while(保存->链接!=NULL&&save->info!=v)
{
保存=保存->链接;
}
新建1->link=保存->链接;
保存->链接=new1;
}
}
保存=第一;
先返回;
}
结构节点*在前面插入(int x,结构节点*第一个)
{
INTV;
new1=(结构节点*)malloc(sizeof(结构节点));
new1->info=x;
new1->link=NULL;
保存=第一;
printf(“\n在上述节点之前插入的节点的输入值=”);
scanf(“%d”和“&v”);
if(first==NULL)
{
first=new1;
}
其他的
{
如果(第一->链接==NULL)
{
new1->link=first;
first=new1;
}
其他的
{
while(保存->链接!=NULL&&save->info!=v)
{
pred=保存;
保存=保存->链接;
}
pred->link=new1;
新建1->link=保存;
}
}
先返回;
}
“我应该怎么做?”-使用调试器并逐步检查代码以了解发生了什么。调试是o的一个组成部分