键盘输入造链表1

#include <stdio.h>
#include <malloc.h>
//键盘输入造链表

struct wyq{
char name[100];
int age;
char address[100];
struct wyq*next;
};

int main()
{
struct wyq*
head;
struct wyq*p;
struct wyq*q;
int i;

head=(struct wyq*)malloc(sizeof(struct wyq));
if(NULL==head){
printf("没有申请成功");
exit(-1);
}

printf("输入链表节点内容:\n");
printf("姓名->");
scanf("%s",head->name);
printf("年龄->");
scanf("%s",head->age);
printf("地址->");
scanf("%s",head->address);
head->next=NULL;


p=head;
q=head;

printf("1;继续;2停止\n");
scanf("%d",&i);
while(i==1) {
p=(struct wyq*)malloc(sizeof(struct wyq));
printf("输入链表节点内容:\n");
printf("姓名->");
scanf("%s",p->name);
printf("年龄->");
scanf("%s",p->age);
printf("地址->");
scanf("%s",p->address);
p->next=NULL;

q->next=p;
q=p;

printf("1;继续;2停止\n");
scanf("%d",&i);

if(i==2){
break;
}
}


return 0;
}

转载于:https://www.cnblogs.com/wangyuqi/p/5567773.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用C++来输入链表和输出链表。以下是一个简单的示例代码: ```cpp #include <iostream> // 链表节点的定义 struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(nullptr) {} }; // 输入链表 ListNode* inputLinkedList() { int n; std::cout << "输入链表节点个数: "; std::cin >> n; ListNode* head = nullptr; ListNode* curr = nullptr; std::cout << "输入链表节点值: "; for (int i = 0; i < n; i++) { int val; std::cin >> val; ListNode* newNode = new ListNode(val); if (head == nullptr) { head = newNode; curr = newNode; } else { curr->next = newNode; curr = curr->next; } } return head; } // 输出链表 void outputLinkedList(ListNode* head) { std::cout << "链表节点值: "; while (head != nullptr) { std::cout << head->val << " "; head = head->next; } std::cout << std::endl; } int main() { ListNode* head = inputLinkedList(); outputLinkedList(head); return 0; } ``` 在这个示例中,我们首先定义了一个名为 `ListNode` 的结构体,用于表示链表的节点。然后,我们编写了两个函数 `inputLinkedList` 和 `outputLinkedList` 分别用于输入和输出链表。 在 `inputLinkedList` 函数中,我们首先要求用户输入链表节点的个数。然后,我们使用一个循环来读取每个节点的值,并将其插入链表中。最后,函数返回链表的头节点。 在 `outputLinkedList` 函数中,我们遍历链表并输出每个节点的值。 在 `main` 函数中,我们首先调用 `inputLinkedList` 函数来输入链表,然后调用 `outputLinkedList` 函数来输出链表的节点值。 你可以根据自己的需要修改代码,并根据输入和输出格式进行相应的调整。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值