using namespace std;
typedef struct node
{
int data;
struct node *next;
}node;
void creatlist(node*&head,int arr[],int n)
{
head=(node*)malloc(sizeof(node));
head->next=NULL;
node* p=head;
for(int i=0;i<n;i++)
{
nodex=(node)malloc(sizeof(node));
p->next=x;
x->next=NULL;
x->data=arr[i];
p=x;
}
}
void del(node*&head,int a[])
{
nodep;
nodex =head->next;
while(x->next!=NULL)
{
if(x->data==x->next->data)
{
p=x->next;
x->next=p->next;
free§;
}
else
{
x=x->next;
}
}
}
int main()
{
int arr[]={1,1,2,2,3,3,4,5,6,7,7};
node* x;
int i=11;
creatlist(x,arr,i);
del(x,arr);
node*y=x->next;
while(y!=NULL)
{
std::cout << y->data << std::endl;
y=y->next;
}
}
这段代码展示了如何使用C++创建一个链表,并实现一个功能来删除链表中相邻的重复元素。`creatlist`函数用于初始化链表,`del`函数则根据给定数组删除重复项。在`main`函数中,创建了一个包含重复数字的链表并调用`del`函数进行处理,然后遍历输出处理后的链表节点数据。
5万+

被折叠的 条评论
为什么被折叠?



