链表,值,添加和删除

时间:2017-03-05 04:48:18

标签: c list

如何修改下面的代码,在插入后插入值为25的元素 值为20的元素,并从列表中删除值为15的元素?我无法弄清楚。谢谢你的帮助! main.c中

#include "includes.h"
ListType list[5];
int main()
{
 ListType list[5];
 list[0].value = 0; list[0].next = list+1;
 list[1].value = 10; list[1].next = list+2;
 list[2].value = 20; list[2].next = NULL;
 ListReport(list);
 // Insert an element
 list[3].value = 15; list[3].next = list+2;
 list[1].next = list+3;
 ListReport(list);
 // Remove an element
 list[0].next = list+3;
 ListReport(list);
 return 0;
}
// Report values in linked lisk
void ListReport(ListType *plist)
{
 int nn = 0;
 printf("ListReport\n");
 while(plist != NULL){
 printf("%d, value = %d\n",nn++, plist->value);
 plist = plist->next;
 }
}

INCLUDES.H

#include <stdio.h>
#include <stdlib.h>
typedef struct entry
 {
 int value;
 struct entry *next;
 } ListType;
 void ListReport(ListType *plist);

// end of includes.h

1 个答案:

答案 0 :(得分:0)

// Insert
list[4].value = 20; 
list[2].next = list+3;
list[4].next = NULL;

// Delete
list[1].next = list+2;