给far的单链表代码

[size=xx-large]纯纪念[/size]
#include <stdio.h>
#include <stdlib.h>
struct student
{
char name[20];
struct student *next;
};

// 创建一个结点的函数
struct student* createNode()
{
struct student* p = (struct student *)malloc(sizeof(struct student));
p->next = NULL;
return p;
}

// 加结点有两种,一是在头加,一是在尾加
struct student* addHeadNode(struct student *p)
{
struct student *head = createNode();
head->next = p;
return head;
}

//struct student * addTailNode(struct student *p)

// 增加size
struct student* addSizeNode(struct student *head, int size)
{
int i = 0;
for (i=0; i < size; i++)
{
head = addHeadNode(head);
}
return head;
}


// 获取链表大小
int getSize(struct student *p)
{
int size = 0;
while (p != NULL)
{
size++;
p = p->next;
}

return size;
}

// 对结点赋值
void setNode(struct student *p)
{
gets(p->name);
}

// 对整个链表赋值
void setList(struct student *head)
{
while (head != NULL)
{
setNode(head);
head = head->next;
}
}

// 打印链表
void printList(struct student *head)
{
while (head != NULL)
{
printf("name = %s\n", head->name);
head = head->next;
}
}


int main(int argc, char *argv[])
{
// 链表头结点
struct student* head;


head = createNode();
printf("link list size = %d\n", getSize(head));


head = addSizeNode(head, 1);
printf("link list size = %d\n", getSize(head));


setList(head);
printList(head);


return 0;
}


[size=x-large]使用typedef后[/size]
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char name[20];
struct student *next;
} * Student;

// 创建一个结点的函数
Student createNode()
{
Student p = (struct student *)malloc(sizeof(struct student));
p->next = NULL;
return p;
}

// 加结点有两种,一是在头加,一是在尾加
Student addHeadNode(Student p)
{
struct student *head = createNode();
head->next = p;
return head;
}

//struct student * addTailNode(Student p)

// 增加size
Student addSizeNode(Student head, int size)
{
int i = 0;
for (i=0; i < size; i++)
{
head = addHeadNode(head);
}
return head;
}


// 获取链表大小
int getSize(Student p)
{
int size = 0;
while (p != NULL)
{
size++;
p = p->next;
}

return size;
}

// 对结点赋值
void setNode(Student p)
{
gets(p->name);
}

// 对整个链表赋值
void setList(Student head)
{
while (head != NULL)
{
setNode(head);
head = head->next;
}
}

// 打印链表
void printList(Student head)
{
while (head != NULL)
{
printf("name = %s\n", head->name);
head = head->next;
}
}


int main(int argc, char *argv[])
{
// 链表头结点
Student head;


head = createNode();
printf("link list size = %d\n", getSize(head));


head = addSizeNode(head, 1);
printf("link list size = %d\n", getSize(head));


setList(head);
printList(head);


return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值