user-0 提问: 链表头插入和尾插入

回复:
user: user-0
content: 能不能看一下后面的题目，六楼的这个我看不大懂

user: user-1
content: 这是我参考你的代码写的。 #ifndef _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #endif  #include <stdio.h> #include <stdlib.h>  typedef struct tagNode { 	void*           data; 	char*           type; 	struct tagNode* next; } Node;  Node* getNode(void* data, char* type, Node* next) { 	Node* node = (Node*)malloc(sizeof(Node)); 	if(NULL != node) 	{ 		node->data = data; 		node->type = type; 		node->next = next; 	} 	return (node); }  const char* getFormat(char* type) { 	return "%s"; }  void  display(Node* node) { 	printf("=-=-=-=-=-=-=-=-=-=-start=-=-=-=-=-=-=-=\n"); 	while(NULL != node) 	{ 		printf("type: %s, data:", node->type); 		printf(getFormat(node->type), node->data); 		printf("\n"); 		node = node->next; 	} 	printf("=-=-=-=-=-=-=-=-=-=-end=-=-=-=-=-=-=-=-=\n"); }  Node* addHead(Node* head, void* data, char* type) { 	return getNode(data, type, head); }  Node* addTail(Node* head, void* data, char* type) { 	Node* node = head; 	while(NULL != node && NULL != node->next) 	{ 		node = node->next; 	} 	node->next = getNode(data, type, NULL); 	return (head); }  void  destroy(Node* head) { 	Node* next = NULL; 	while(NULL != head) 	{ 		next = head->next; 		free(head->data);//这里要求data是由malloc分配 		free(head); 		head = next; 	} }  int main() { 	char* data = NULL; 	int i = 0; 	Node* head = NULL; 	Node* tail = NULL;  	data = (char*)malloc(sizeof(char)*5); 	sprintf(data, "%s", "Head"); 	head = getNode(data, "String", NULL); 	 	data = (char*)malloc(sizeof(char)*5); 	sprintf(data, "%s", "Tail"); 	tail = getNode(data, "String", NULL); 	 	for(; i < 9; i++) 	{ 		data = (char*)malloc(sizeof(char)*5); 		sprintf(data, "T%03d", i); 		tail = addHead(tail, data, "String"); 		data = (char*)malloc(sizeof(char)*5); 		sprintf(data, "H%03d", i); 		head = addTail(head, data, "String"); 	} 	display(head); 	display(tail); 	destroy(head); 	destroy(tail); 	return 0; }

user: user-0
content: 真的太感谢了

user: user-0
content: 谢谢，谢谢，我先仔细看一下

user: user-2
content: 引用 1 楼 super_admi 的回复:#include <stdio.h> #include <stdlib.h>  typedef int ElemType; struct Node; typedef struct Node *PtrToNode; typedef PtrToNode List; typedef PtrToNode Position;  struct Node { 	ElemType data; 	Position  next; }; List CreateList() { 	List head=(List)malloc(sizeof(struct Node)); 	if(head==NULL) 	{ 		printf("分配失败！"); 		exit(-1); 	} 	head->next=NULL; 	return head; } void PrintList(List L) { 	List p=L->next; 	if(p==NULL) 		printf("链表为空!"); 	else 	{ 		while(p!=NULL) 		{ 			printf("%d ",p->data); 			p=p->next; 		} 	} } void InsertAtHead(List L,ElemType X) { 	List p; 	p=(List)malloc(sizeof(struct Node)); 	if(L==NULL) 	{ 		printf("链表为空!"); 		exit(-1); 	} 	if(p==NULL) 	{ 		printf("ERROR!"); 		exit(-1); 	} 	p->data=X; 	L->next=p->next; 	L->next=p; } void InsertAtTail(List L,ElemType X) { 	List p,tail; 	p=(List)malloc(sizeof(struct Node)); 	if(L==NULL) 	{ 		printf("链表为空!"); 		exit(-1); 	} 	if(p==NULL) 	{ 		printf("ERROR!"); 		exit(-1); 	} 	p->data=X; 	p->next=NULL; 	tail=L; 	while(!tail && tail->next) 	{ 		tail=tail->next; 	} 	tail->next=p; } int main() { 	int i; 	List L; 	printf("List is:"); 	for(i=1;i<=10;i++) 	{ 		L=(List)malloc(sizeof(struct Node)); 		InsertAtTail(L,i); 		PrintList(L); 		L=L->next; 	} 	return 0; }  你修改的链表，头插法的结果和尾插的结果一样呢

user: user-2
content: #include <stdio.h> #include <stdlib.h>  typedef int ElemType; struct Node; typedef struct Node *PtrToNode; typedef PtrToNode List; typedef PtrToNode Position;  struct Node {     ElemType data;     Position  next; };  List CreateList() {     List head=(List)malloc(sizeof(struct Node));     if(head==NULL)     {         printf("分配失败！");         exit(-1);     }     head->next=NULL;     return head; }  void PrintList(List L) {     List p = L->next;     if (!p) {         printf("链表为空!");         return;     }     while (p) {         printf("%d ", p->data);         p = p->next;     }     putchar(10); } void InsertAtHead(List L,ElemType X) {     List p;     p = (List)malloc(sizeof(struct Node));     if (!L) {         printf("链表为空!");         exit(-1);     }     if (!p) {         printf("ERROR!");         exit(-1);     } #if 0     L->next =p->next;     L->next=p; #else     p->data = X;     p->next = L->next;     L->next = p; #endif } void InsertAtTail(List L,ElemType X) {     List p,tail;     p = (List)malloc(sizeof(struct Node));     if(!L)     {         printf("链表为空!");         exit(-1);     }     if (!p)     {         printf("ERROR!");         exit(-1);     }     p->data=X;     p->next=NULL;      tail = L;     while (tail->next)         tail=tail->next;      tail->next = p; }  int main() {     int i;     List L1, L2;      printf("List is:");     L1 = CreateList();     for(i=1;i<=10;i++) {         //L = (List)malloc(sizeof(struct Node));         InsertAtTail(L1,i);         //L=L->next;     }     PrintList(L1);     L2 = CreateList();     for(i=1;i<=10;i++) {         //L = (List)malloc(sizeof(struct Node));         InsertAtHead(L2, i);         //L=L->next;     }     printf("List is:");     PrintList(L2);      return 0; }  参考一下吧 建议把链表释放加上。

user: user-1
content: #include <stdio.h> #include <stdlib.h>  typedef int ElemType; struct Node; typedef struct Node *PtrToNode; typedef PtrToNode List; typedef PtrToNode Position;  struct Node { 	ElemType data; 	Position  next; }; List CreateList() { 	List head=(List)malloc(sizeof(struct Node)); 	if(head==NULL) 	{ 		printf("分配失败！"); 		exit(-1); 	} 	head->next=NULL; 	return head; } void PrintList(List L) { 	List p=L->next; 	if(p==NULL) 		printf("链表为空!"); 	else 	{ 		while(p!=NULL) 		{ 			printf("%d ",p->data); 			p=p->next; 		} 	} } void InsertAtHead(List L,ElemType X) { 	List p; 	p=(List)malloc(sizeof(struct Node)); 	if(L==NULL) 	{ 		printf("链表为空!"); 		exit(-1); 	} 	if(p==NULL) 	{ 		printf("ERROR!"); 		exit(-1); 	} 	p->data=X; 	L->next=p->next; 	L->next=p; } void InsertAtTail(List L,ElemType X) { 	List p,tail; 	p=(List)malloc(sizeof(struct Node)); 	if(L==NULL) 	{ 		printf("链表为空!"); 		exit(-1); 	} 	if(p==NULL) 	{ 		printf("ERROR!"); 		exit(-1); 	} 	p->data=X; 	p->next=NULL; 	tail=L; 	while(!tail && tail->next) 	{ 		tail=tail->next; 	} 	tail->next=p; } int main() { 	int i; 	List L; 	printf("List is:"); 	for(i=1;i<=10;i++) 	{ 		L=(List)malloc(sizeof(struct Node)); 		InsertAtTail(L,i); 		PrintList(L); 		L=L->next; 	} 	return 0; }