[英]Insert an element into a linked list
If I have two structures defined as follow, how can I insert a new element in the list ?如果我有两个定义如下的结构,我如何在列表中插入一个新元素? I tried something, but I don't know where is the mistake.
我尝试了一些东西,但我不知道错误在哪里。 The function for inserting a new element is called insertFirst.
插入新元素的函数称为insertFirst。 In main I have
lst=insertFirst(lst,7); lst=insertFirst(lst,8);
在主要我有
lst=insertFirst(lst,7); lst=insertFirst(lst,8);
lst=insertFirst(lst,7); lst=insertFirst(lst,8);
Thank you!谢谢!
typedef int DATA;
struct element {
DATA cheie;
struct element *urm;
};
typedef struct element Element, *ELEMENT;
struct lista {
int nr; //nr elemente
ELEMENT inceput;
ELEMENT sfarsit;
};
typedef struct lista Lista, *LISTA;
LISTA insertFirst(LISTA l, DATA x)
{
LISTA w;
w = (LISTA)malloc(sizeof(Lista));
if (w == NULL)return NULL;
w->inceput=(LISTA)malloc(sizeof(Lista));
w->inceput->cheie = x;
w->inceput->urm = l;
LISTA p = l;
for (; p->inceput->urm != NULL; p = p->inceput->urm);
w->sfarsit->cheie = p->inceput->cheie;
w->sfarsit->urm = NULL;
return w;
}
It seems you mean the following.看来你的意思是以下。 That is you have a two-sided singly-linked list.
那就是你有一个双面单链表。
LISTA insertFirst(LISTA l, DATA x)
{
ELEMENT w;
w = (ELEMENT)malloc(sizeof(Element));
if ( w )
{
w->cheie = x;
w->urm = l->inceput;
l->inceput = w;
if ( !l->sfarsit ) l->sfarsit = w;
++l->nr;
}
return l;
}
Another way to define the function is the following定义函数的另一种方法如下
int insertFirst(LISTA l, DATA x)
{
ELEMENT w;
w = (ELEMENT)malloc(sizeof(Element));
int success = w != NULL;
if ( success )
{
w->cheie = x;
w->urm = l->inceput;
l->inceput = w;
if ( !l->sfarsit ) l->sfarsit = w;
++l->nr;
}
return success;
}
And in main you should define the list like在主要你应该定义列表
Lista l = { 0, NULL, NULL };
Here is a demonstrative program这是一个演示程序
#include <stdio.h>
#include <stdlib.h>
typedef int DATA;
struct element
{
DATA cheie;
struct element *urm;
};
typedef struct element Element, *ELEMENT;
struct lista
{
int nr; //nr elemente
ELEMENT inceput;
ELEMENT sfarsit;
};
typedef struct lista Lista, *LISTA;
int insertFirst(LISTA l, DATA x)
{
ELEMENT w;
w = (ELEMENT)malloc(sizeof(Element));
int success = w != NULL;
if ( success )
{
w->cheie = x;
w->urm = l->inceput;
l->inceput = w;
if ( !l->sfarsit ) l->sfarsit = w;
++l->nr;
}
return success;
}
void outputLista( LISTA l )
{
printf( "%d: ", l->nr );
for ( ELEMENT current = l->inceput; current; current = current->urm )
{
printf( "%d ", current->cheie );
}
}
int main(void)
{
Lista l = { 0, NULL, NULL };
insertFirst( &l, 7 );
insertFirst( &l, 8 );
outputLista( &l );
putchar( '\n' );
return 0;
}
Its output is它的输出是
2: 8 7
As for your function implementation then it does not make sense.至于你的功能实现,那就没有意义了。 You do not need to create a new list in the function.
您不需要在函数中创建新列表。 What you need is to create a new element and insert it before the current first element of the list.
您需要的是创建一个新元素并将其插入到列表的当前第一个元素之前。
The function that inserts a new element at the end of the list can look like在列表末尾插入新元素的函数看起来像
int insertLast(LISTA l, DATA x)
{
ELEMENT w;
w = (ELEMENT)malloc(sizeof(Element));
int success = w != NULL;
if ( success )
{
w->cheie = x;
w->urm = NULL;
if ( l->sfarsit )
{
l->sfarsit->urm = w;
}
else
{
l->inceput = w;
}
l->sfarsit = w;
++l->nr;
}
return success;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.