C 审查这个链表计划
这是链表上的一个程序,用于打印给定的输入。但是,是否可以不使用C 审查这个链表计划,c,pointers,linked-list,C,Pointers,Linked List,这是链表上的一个程序,用于打印给定的输入。但是,是否可以不使用if(start==NULL)…else..语句来执行此操作 #include <stdio.h> #include <malloc.h> struct node { int data; struct node* next; }* start = NULL; void main() { struct node* tmp; struct node *ptr, *tmp1;
if(start==NULL)…else..
语句来执行此操作
#include <stdio.h>
#include <malloc.h>
struct node
{
int data;
struct node* next;
}* start = NULL;
void main()
{
struct node* tmp;
struct node *ptr, *tmp1;
int n = 0;
int choice = 2;
while (1)
{
printf("Enter a number less than 3 to continue");
scanf("%d", &choice);
if (choice >= 3)
break;
printf("Enter the input");
scanf("%d", &n);
// n=n+10;
if (start == NULL)
{
tmp = (struct node*)malloc(sizeof(struct node));
start = tmp;
start->data = n;
ptr = start;
}
else
{
tmp = (struct node*)malloc(sizeof(struct node));
ptr->next = tmp;
tmp->data = n;
ptr = tmp;
}
}
tmp->next = NULL;
printf("\nThe output of the linked list is\n");
n = 0;
ptr = start;
while (ptr != NULL)
{
printf("\n 1 ptr = %d", ptr->data);
ptr = ptr->next;
}
}
#包括
#包括
结构节点
{
int数据;
结构节点*下一步;
}*start=NULL;
void main()
{
结构节点*tmp;
结构节点*ptr,*tmp1;
int n=0;
int-choice=2;
而(1)
{
printf(“输入一个小于3的数字以继续”);
scanf(“%d”,选择(&C);
如果(选项>=3)
打破
printf(“输入”);
scanf(“%d”和“&n”);
//n=n+10;
if(start==NULL)
{
tmp=(结构节点*)malloc(sizeof(结构节点));
启动=tmp;
开始->数据=n;
ptr=启动;
}
其他的
{
tmp=(结构节点*)malloc(sizeof(结构节点));
ptr->next=tmp;
tmp->data=n;
ptr=tmp;
}
}
tmp->next=NULL;
printf(“\n链接列表的输出为\n”);
n=0;
ptr=启动;
while(ptr!=NULL)
{
printf(“\n 1 ptr=%d”,ptr->data);
ptr=ptr->next;
}
}
嗯,很多事情都是不必要的
您只需将节点定义为
struct node
{
int data;
node * next; // Will hold the value of next node's address
};
现在,您可以简单地使用方法创建一个数据结构类
class LinkedList {
private:
node *head, *current, *temp; // 3 Main pointers
public:
LinkedList() {
head = temp = current = NULL; // Set them to NULL
}
void insert(int data) { // to add a new node
if(head == NULL) { // if there is no node in the memory
head = new node; // use head to make a new node
head->data = data; // set the data of the node to data
head->next = NULL; // set the next to NULL
temp = current = head; // 3 pointers sitting at the head node
}
else { // If the node is not the first one in the memory
current = new node; // use current pointer to make new node
temp->next = current; // temp pointer one step behind the current node
current->next = NULL; // make the next of new node NULL
temp = current; // move the pointer to current
current->data = data;
}
}
};
此函数只需在每次调用时添加节点
要显示列表,
只需将临时指针移到head并开始迭代
void display()
{
temp = head; // move to head
while(temp != NULL) // while temp doesn't reach the end
{
cout<<temp->data<< " "; // print the node's data
temp = temp->next; // move the pointer to next node
}
}
void display()
{
temp=head;//移动到head
while(temp!=NULL)//while temp未到达末尾
{
cout变量start
保存链表的标题
条件if(start==NULL)
检查空列表
如果列表为空,则需要创建列表的标题。从第二个元素开始,需要将下一个元素链接到列表的最后一个元素。这由else
条件负责
从另一个角度来看,如果您没有if
条件,那么您就有了行
ptr->next = tmp;
由于此时未初始化ptr
,因此您将有未定义的行为,很可能出现分段错误
不使用if(start==NULL)…else..语句是否可以执行此操作
#include <stdio.h>
#include <malloc.h>
struct node
{
int data;
struct node* next;
}* start = NULL;
void main()
{
struct node* tmp;
struct node *ptr, *tmp1;
int n = 0;
int choice = 2;
while (1)
{
printf("Enter a number less than 3 to continue");
scanf("%d", &choice);
if (choice >= 3)
break;
printf("Enter the input");
scanf("%d", &n);
// n=n+10;
if (start == NULL)
{
tmp = (struct node*)malloc(sizeof(struct node));
start = tmp;
start->data = n;
ptr = start;
}
else
{
tmp = (struct node*)malloc(sizeof(struct node));
ptr->next = tmp;
tmp->data = n;
ptr = tmp;
}
}
tmp->next = NULL;
printf("\nThe output of the linked list is\n");
n = 0;
ptr = start;
while (ptr != NULL)
{
printf("\n 1 ptr = %d", ptr->data);
ptr = ptr->next;
}
}
对
创建头部节点并仅使用其.next
成员
#include <stdio.h>
#include <malloc.h>
struct node {
int data;
struct node* next;
};
int main(void) {
struct node head = {.next = NULL}; // only need .next
struct node *ptr = &head;
while (1) {
// ...
int n;
printf("Enter the input ");
fflush(stdout);
if (scanf("%d", &n) != 1) {
break;
}
ptr->next = malloc(sizeof *ptr->next);
if (ptr->next == NULL) {
break;
}
ptr = ptr->next;
ptr->data = n;
ptr->next = NULL;
}
printf("\nThe output of the linked list is\n");
ptr = head.next; // The start of the list is here
while (ptr) {
printf(" 1 ptr = %d\n", ptr->data);
ptr = ptr->next;
}
}
#包括
#包括
结构节点{
int数据;
结构节点*下一步;
};
内部主(空){
结构节点头={.next=NULL};//只需要.next
结构节点*ptr=&head;
而(1){
// ...
int n;
printf(“输入”);
fflush(stdout);
如果(scanf(“%d”,&n)!=1){
打破
}
ptr->next=malloc(sizeof*ptr->next);
如果(ptr->next==NULL){
打破
}
ptr=ptr->next;
ptr->data=n;
ptr->next=NULL;
}
printf(“\n链接列表的输出为\n”);
ptr=head.next;//列表的开头在这里
while(ptr){
printf(“1 ptr=%d\n”,ptr->data);
ptr=ptr->next;
}
}
你想让代码做什么?它做什么呢?@Broman我只想在节点中插入一些值并打印它们。这是有效的。但我怀疑如果不使用这个if(start==NULL)…else..语句,它是可能的。事实上,有必要区分start
为NULL
的情况与相反(这里不是NULL
)。但是,在then-and-else分支中有很多代码重复。这是可以消除的。@Scheff你能在重复后给我代码吗。如果(ptr==NULL){start=ptr=tmp;}else{ptr->next=tmp;ptr=tmp;},我将非常感谢你
,您可以从if
/else
中计算出节点的创建/初始化(这是基于start
和ptr
最初为NULL
)注释开始于<代码> //< /COD>,而不是''\'……这个问题被标记为“…”代码>类链接目录> /COD>,<代码>头=新节点;< /C> >等,可能会在C编译器中造成语法错误。这是C++而不是。C@Scheff几乎所有东西都会在C编译器中生成编译器错误。:)这就是为什么他们引入了标签:对于C相关的问题,对于C++相关的问题,对于Python相关的问题,以及关于算法的语言不可知的问题。Btw.,你在C.Touch中提到了python?op-标签和暴露代码。使用虚拟节点作为列表头,有必要区分start
为NULL
的情况