·C语言·192
发布网友
发布时间:2023-10-23 10:48
我来回答
共3个回答
热心网友
时间:2024-12-13 00:57
#include
#define MAX 10
int testData[2][MAX] = { 1,2,3,4,5,6,7,8,9,10,
100,200,300,400,500,600,700,800,900,1000 };
struct List {
int number;
int data;
struct List *Next;
};
typedef struct List Node;
typedef Node *Link;
int searchTimes = 0;
Link createList(Link Head) {
int newNumber;
int newData;
Link Pointer;
Link New;
int i;
printf("分配节点内存\t");
Head = (Link)malloc(sizeof(Node));
if(Head == NULL) {
printf("assign memory error");
}
else {
printf("数据项存入节点\t");
Head->number = testData[0][0];
Head->data = testData[1][0];
Head->Next = NULL;
Pointer = Head;
for(i=1;i New = (Link)malloc(sizeof(Node));
New->number = testData[0][i];
New->data = testData[1][i];
New->Next = NULL;
Pointer->Next = New;
Pointer = New;
}
printf("节点组链完毕\n");
}
return Head;
}
Link insertList(Link Head,Link New,int insertNumber) {
Link Pointer;
Pointer = Head;
while(1) {
if(Pointer == NULL) {
Pointer->Next = Head;
Head = Pointer;
break;
}
if(Pointer->number == insertNumber) {
printf("插入到 %d\t%d 之后\n",Pointer->number,Pointer->data);
New->Next = Pointer->Next;
Pointer->Next = New;
break;
}
Pointer = Pointer->Next;
}
return Head;
}
void freeList(Link Head) {
Link Pointer;
while(Head != NULL) {
Pointer = Head;
Head = Head->Next;
free(Pointer);
}
}
void printList(Link Head) {
Link Pointer;
Pointer = Head;
printf("打印链表\n数据项1\t数据项2\n");
while(Pointer != NULL) {
printf("%d\t",Pointer->number);
printf("%d\n",Pointer->data);
Pointer = Pointer->Next;
}
}
int main() {
Link Head;
Link New;
int key;
Head = createList(Head);
printList(Head);
New = (Link)malloc(sizeof(Node));
printf("输入新节点数据项1 数据项2 : ");
scanf("%d %d",&New->number,&New->data);
printf("输入插入位置 : ");
scanf("%d",&key);
Head = insertList(Head,New,key);
printList(Head);
freeList(Head);
return 0;
}
/*
D:\>CAIAFList
分配节点内存 数据项存入节点 节点组
打印链表
数据项1 数据项2
1 100
2 200
3 300
4 400
5 500
6 600
7 700
8 800
9 900
10 1000
输入新节点数据项1 数据项2 : 56 5656
输入插入位置 : 5
插入到 5 500 之后
打印链表
数据项1 数据项2
1 100
2 200
3 300
4 400
5 500
56 5656
6 600
7 700
8 800
9 900
10 1000
*/
热心网友
时间:2024-12-13 00:58
Status ListInsert_L(LinkList &L, int i, ElemType e)
{
p = L; j = 0;
while(p) {p = p->next; }
if(!p) return ERROR;
s = (LinkList) malloc(sizeof(LNode));
s->data = e;
s->next = p->next;
p->next = null;
return OK;
}
热心网友
时间:2024-12-13 00:58
双链哩?