我的第九个链表程序

代码如下:

#include <stdio.h>
#include <stdlib.h>
typedef struct
{
	char id[500];
	char password[500];
}Elemtype;
typedef struct
{
	Elemtype data;
	struct LNode *next;
}LNode;
LNode ListInsert(LNode *L)
{
	L=(LNode*)malloc(sizeof(LNode));
	L->next=NULL;
	return *L;
}
LNode datasList(LNode *L)
{
	char *a[]={"passqword","passworddd","password56"};
	char *b[]={"1","2","3"};
	LNode *boy3;
	boy3=L;
	int c;
	for(c=0;c<3;c++)
	{
		Elemtype e;
		strcpy(e.id,a[c]);
		strcpy(e.password,b[c]);
		LNode *boy2;
		boy2=(LNode*)malloc(sizeof(LNode));
		boy2->data=e;
		boy2->next=NULL;
		boy3->next=boy2;
		boy3=boy2;
	}
	return *L;
}
int main(int argc, char *argv[])
{
	LNode p,*e;
	p=ListInsert(&p);
	e=(datasList(&p)).next;
	while(e)
	{
		printf("%s\t%s\n",e->data.id,e->data.password);
		e=e->next;
	}
	system("pause");
	return 0;
}

程序运行如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个示例程序,可以实现输入两个链表并合并它们: ```c #include <stdio.h> #include <stdlib.h> // 链表节点结构体 struct ListNode { int val; struct ListNode *next; }; // 创建新节点 struct ListNode* createNode(int val) { struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode)); node->val = val; node->next = NULL; return node; } // 合并两个链表 struct ListNode* mergeLists(struct ListNode* l1, struct ListNode* l2) { struct ListNode dummy = {0, NULL}; struct ListNode* tail = &dummy; while (l1 && l2) { if (l1->val < l2->val) { tail->next = l1; l1 = l1->next; } else { tail->next = l2; l2 = l2->next; } tail = tail->next; } tail->next = l1 ? l1 : l2; return dummy.next; } // 释放链表内存 void freeList(struct ListNode* head) { while (head) { struct ListNode* node = head; head = head->next; free(node); } } int main() { struct ListNode *l1 = NULL, *l2 = NULL; int val; // 输入第一个链表 printf("Input the first list (separated by spaces, end with a negative number):\n"); while (scanf("%d", &val) == 1 && val >= 0) { struct ListNode* node = createNode(val); if (!l1) l1 = node; else { struct ListNode* tail = l1; while (tail->next) tail = tail->next; tail->next = node; } } // 输入第二个链表 printf("Input the second list (separated by spaces, end with a negative number):\n"); while (scanf("%d", &val) == 1 && val >= 0) { struct ListNode* node = createNode(val); if (!l2) l2 = node; else { struct ListNode* tail = l2; while (tail->next) tail = tail->next; tail->next = node; } } // 合并链表 struct ListNode* merged = mergeLists(l1, l2); // 输出合并后的链表 printf("Merged list:\n"); struct ListNode* p = merged; while (p) { printf("%d ", p->val); p = p->next; } printf("\n"); // 释放内存 freeList(l1); freeList(l2); freeList(merged); return 0; } ``` 示例输入: ``` Input the first list (separated by spaces, end with a negative number): 1 3 5 7 9 -1 Input the second list (separated by spaces, end with a negative number): 2 4 6 8 10 -1 ``` 示例输出: ``` Merged list: 1 2 3 4 5 6 7 8 9 10 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值