删除链接列表C中其他节点之间的一个节点(或多个节点)



嗨,我正在为学校创建一个程序,我必须:

  1. 创建结构
  2. 创建函数以打印我的链接列表
  3. 创建函数以将元素插入列表底部
  4. 删除重复的元素

我在最后一步创建了所有零件(删除函数(。你能帮我知道什么是正确的方法吗?这是我的代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*STRUCTURE*/
typedef struct punto {
int x;
int y;
} PUNTO;
typedef struct quadrato {
PUNTO v;
int lato;
} QUADRATO;
typedef struct nodo {
QUADRATO q;
struct nodo *next;
} NODO;
/*PRINT LIST*/
void stampaQuadrato(QUADRATO q) {
printf("The square has the side equal to %d and coordinates of the lower left vertex (%d,%d)n", q.lato, q.v.x, q.v.y);
}
void stampaLista(NODO *head) {
if(head->next==NULL) {
printf("List empty!n");
} else {
while(head->next != NULL) {
head = head->next;
stampaQuadrato(head->q);
}
}
}
/*ADD*/
QUADRATO creaQuadrato() {

QUADRATO nuovo;

printf("Side measurement: ");
scanf("%d", &nuovo.lato);
printf("n");
printf("Coordinates of the lower left vertexn");
printf("x: ");
scanf("%d", &nuovo.v.x);
printf("n");
printf("y: ");
scanf("%d", &nuovo.v.y);
printf("n");

return nuovo;

}
void insert(NODO *head) {
NODO* nuovoNodo = malloc(sizeof(NODO));
nuovoNodo->q = creaQuadrato();
if(head==NULL) {
head = head->next;
head = nuovoNodo;
} else {
while(head->next != NULL) {
head = head->next;
}
head->next = nuovoNodo;
}
}
/*DELETE*/
void deleteDuplicates(NODO *head) {
if(head==NULL) {
printf("There are no element in the list!n");
} else {
while(head->next != NULL) {
if(head->q.lato == head->next->q.lato &&
head->q.v.x == head->next->q.v.x &&
head->q.v.y == head->next->q.v.y) {
NODO* nodoQuad = head->next;
head->next = nodoQuad->next;
//head->next = head->next->next;
free(nodoQuad);
printf("Element deleted!n");
} else {
head = head->next;
}
}
}
}
int main() {
NODO *head = malloc(sizeof(NODO));
head->next = NULL;
int scelta = -1;

while(scelta != 0) {
printf("Choose onen");
printf("Press 1 --> See listn");
printf("Press 2 --> Insert new square(quadrato)n");
printf("Press 3 --> Delete duplicatesn");
printf("Press 0 --> Stop programn");
scanf("%d", &scelta);

if(scelta==1) {
stampaLista(head);
}
else if(scelta==2) {
insert(head);
}
else if(scelta==3) {
deleteDuplicates(head);
}
else if(scelta==0) {
printf("See you soon ;)n");
}

}

}

对于初学者来说,最初列表应该是空的。所以你必须写

NODO *head = NULL;

而不是

NODO *head = malloc(sizeof(NODO));

此while loop

int scelta = -1;
while(scelta != 0) {
//...
}

应该代替do-while循环,例如

do
{
//...
} while ( scelta != 0 );

在do-while循环中,最好使用switch语句,例如,而不是使用if语句

do
{
scelta = 0;
printf("Choose onen");
printf("Press 1 --> See listn");
printf("Press 2 --> Insert new square(quadrato)n");
printf("Press 3 --> Delete duplicatesn");
printf("Press 0 --> Stop programn");
scanf("%d", &scelta);

enum { Exit = 0, Stampa = 1, Insert = 2, Delete = 3 };
switch ( scelta )
{
case Exit:
puts( "See you soon" );
break;
case Stampa:
stampaLista( head );
break;
case Insert:
insert( &head );
break;
case Delete:
deleteDuplicates( head );
break;
case default:
puts( "Invalid input. Try anew." );
break;
}
} while ( scelta != 0 );

函数insert的不正确

void insert(NODO *head) {
NODO* nuovoNodo = malloc(sizeof(NODO));
nuovoNodo->q = creaQuadrato();
if(head==NULL) {
head = head->next;
head = nuovoNodo;
} else {
while(head->next != NULL) {
head = head->next;
}
head->next = nuovoNodo;
}
}

因为它不改变在main中声明的原始指针CCD_ 2。更改副本不会影响原始指针。

此外,您忘记将创建的节点的数据成员next设置为NULL

功能可以通过以下方式声明和定义

int insert( NODO **head ) 
{
NODO *nuovoNodo = malloc( sizeof( NODO ) );
int success = nuovoNodo != NULL;
if success )
{
nuovoNodo->q = creaQuadrato();
nuovoNodo->next = NULL;
while ( *head != NULL ) head = &( *head )->next;
*head = nuovoNodo;
}
return success;
}

函数stampaLista的参数应具有限定符const

void stampaLista( const NODO *head ) 
{
if ( head == NULL ) 
{
puts( "List empty!" );
} 
else 
{
for ( ; head != NULL; head = head->next )
{
stampaQuadrato( head->q );
}
}
}

对于函数deleteDuplicates,则它仅移除相邻的重复节点。如果你想删除所有重复的节点(不仅仅是相邻的(,那么可以通过以下方式定义功能

void deleteDuplicates( NODO *head ) 
{
if ( head == NULL ) 
{
puts( "There are no element in the list!" );
} 
else 
{
for ( ; head != NULL; head = head->next )
{
for ( NODO *current = head; current->next != NULL; )
{
if ( head->q.lato == current->next->q.lato &&
head->q.v.x  == current->next->q.v.x  &&
head->q.v.y  == current->next->q.v.y ) 
{
NODO *nodoQuad = current->next;
current->next = nodoQuad->next;
free(nodoQuad);
puts( "Element deleted!" );
} 
else 
{
current = current->next;
}
}
}
}
}

您可以这样做:

/* removeDuplicate() will remove duplicate nodes from the list */
void removeDuplicate(NODO *self) {
/* Node current will point to head  */
NODO *current = self->head;  
NODO *index = NULL;  
NODO *temp = NULL;  

if(self->head == NULL) {
return;
} else {
while(current != None) {
/* Node temp will point to previous node to index */
temp = current;  
/* Index will point to node next to current */
index = current->next;  

while(index != NULL) {
/* If current node's data is equal to index node's data */
if(current->data == index->data) {
/* Here, index node is pointing to the node which is duplicate of current node */
/* Skips the duplicate node by pointing to next node */
temp->next = index->next;  
} else { 
/* Temp will point to the previous node of the index. */
temp = index;  
}
index = index->next;
}
current = current->next;
}
}
}

您需要根据代码的需要编写与上面类似的内容。

我发现了一个很好的功能,可以在其他用户的帮助下删除重复项。这是代码:

void deleteDuplicates(NODO* head) {
while(head != NULL && head->next != NULL) {
NODO* nodoQuad = head;
while(nodoQuad->next != NULL) {
if(head->q.lato == nodoQuad->next->q.lato &&
head->q.v.x == nodoQuad->next->q.v.x &&
head->q.v.y == nodoQuad->next->q.v.y) {
NODO* dealloca = nodoQuad->next;
nodoQuad->next = nodoQuad->next->next
free(dealloca);
printf("Element deleted!n");
} else {
nodoQuad = nodoQuad->next;
}
}
head = head->next;
}
}

相关内容

  • 没有找到相关文章

最新更新