单向链表代码程序(2)
来源:互联网 发布:中日关系论断 知乎 编辑:程序博客网 时间:2024/06/05 19:27
#include<stdio.h>
#define N 5
#include<stdlib.h>
#include<string.h>
typedef struct student{
unsigned id;
char name[20];
struct student *next;
}st_t;
st_t *mk_node(int id,char *name)
{
st_t *p = (st_t *)malloc(sizeof(st_t));
if(p != NULL){
p->id = id;
strcpy(p->name ,name);
p->next = NULL;
}
return p;
}
st_t *insert_tail(st_t *head,int id,char *name)
{
st_t *newp = mk_node(id,name);
st_t *tail;
if(!head||!newp){
if(!head)
head = newp;
return head;
}
for(tail = head;tail->next!=NULL;tail=tail->next)
;
tail->next = newp;
return head;
}
st_t *insert_head(st_t *head,int id,char *name)
{
st_t *newp = mk_node(id,name);
if(!head||!newp){
if(!head)
head = newp;
return head;
}
newp->next = head;
head = newp;
return head;
}
st_t *insert_body(st_t *head,int id,char *name)
{
st_t *newp = mk_node(id,name);
st_t *p,*q;
p =head;
if(!head||!newp){
if(!head)
head = newp;
return head;
}
while(( (p->id)< id)&&((p->next) != NULL))
{
q = p;
p = p->next;
}
if(( p->id)> id){
if(head == p)
head = insert_head(head,id,name);
else
{
q->next = newp;
newp->next = p;
// q = q->next;
}
}
else
head = insert_tail(head,id,name);
return head;
}
void link_destroy(st_t *head)
{
st_t *cur,*next;
for(cur = head;cur;cur = next){
next = cur -> next;
free(cur);
}
}
void link_print(st_t *head)
{
st_t *cur;
for(cur = head;cur!=NULL;cur = cur->next)
printf("%d %s\n",cur->id,cur->name);
printf("\n");
}
int main(void)
{
st_t *head = NULL;
int i;
char name[20];
int id;
//st_t stu[N];
for(i =0;i<N;i++)
{
printf("shu ru:");
scanf("%d%s",&id,name);
head = insert_body(head,id,name);
}
link_print(head);
return 0;
}
#define N 5
#include<stdlib.h>
#include<string.h>
typedef struct student{
unsigned id;
char name[20];
struct student *next;
}st_t;
st_t *mk_node(int id,char *name)
{
st_t *p = (st_t *)malloc(sizeof(st_t));
if(p != NULL){
p->id = id;
strcpy(p->name ,name);
p->next = NULL;
}
return p;
}
st_t *insert_tail(st_t *head,int id,char *name)
{
st_t *newp = mk_node(id,name);
st_t *tail;
if(!head||!newp){
if(!head)
head = newp;
return head;
}
for(tail = head;tail->next!=NULL;tail=tail->next)
;
tail->next = newp;
return head;
}
st_t *insert_head(st_t *head,int id,char *name)
{
st_t *newp = mk_node(id,name);
if(!head||!newp){
if(!head)
head = newp;
return head;
}
newp->next = head;
head = newp;
return head;
}
st_t *insert_body(st_t *head,int id,char *name)
{
st_t *newp = mk_node(id,name);
st_t *p,*q;
p =head;
if(!head||!newp){
if(!head)
head = newp;
return head;
}
while(( (p->id)< id)&&((p->next) != NULL))
{
q = p;
p = p->next;
}
if(( p->id)> id){
if(head == p)
head = insert_head(head,id,name);
else
{
q->next = newp;
newp->next = p;
// q = q->next;
}
}
else
head = insert_tail(head,id,name);
return head;
}
void link_destroy(st_t *head)
{
st_t *cur,*next;
for(cur = head;cur;cur = next){
next = cur -> next;
free(cur);
}
}
void link_print(st_t *head)
{
st_t *cur;
for(cur = head;cur!=NULL;cur = cur->next)
printf("%d %s\n",cur->id,cur->name);
printf("\n");
}
int main(void)
{
st_t *head = NULL;
int i;
char name[20];
int id;
//st_t stu[N];
for(i =0;i<N;i++)
{
printf("shu ru:");
scanf("%d%s",&id,name);
head = insert_body(head,id,name);
}
link_print(head);
return 0;
}
插入节点代码也可以简化成如下代码
node_t *insert_body(node_t *head,int n)
{
node_t *newp = mk_node(n);
node_t *p,*q;
p =head;
if(!head||!newp){
if(!head)
head = newp;
return head;
}
for(pre = cur = head; cur && cur->item<n; )
{
pre = cur;
cur = cur->next;
}
if(cur == head){
newp->next = head;
head=newp;
}
else{
newp->next = cur;
pre->next = newp;
}
return head;
}
- 单向链表代码程序(2)
- 单向链表编写代码
- 建立单向链表的两个程序
- 单向链表反转的代码
- 单向链表的插入删除代码
- C#单向链表 实例代码
- 数据结构--单向链表2
- Java 单向链表和单向循环链表的代码实现
- 单向链表的创建和逆转(完整程序)
- 基本头部插入式单向链表程序
- 翻转一个单向链表的c语言代码
- 【数据结构】单向有序链表---最水的代码
- Linus:利用二级指针删除单向链表-实例代码
- 约瑟夫问题的单向循环链表的代码实现
- 数据结构学习之单向链表[附Java实现代码]
- 数据结构2 (单向链表)
- C++实现单向链表(2)
- Linux C 单向链表2
- 使用python的termcolor包
- 一、引言
- bootchart工具在Android系统开机测量中的应用
- Windows 下安装mysql 配置 调整my.ini中InnoDB性能 以及 mysql服务的重启
- 如何实现Nginx+Keepalived中Nginx进程的高可用
- 单向链表代码程序(2)
- Android AIDL创建Service
- 最全的 Web 开发项目检查清单
- C++快速入门 (七) 再谈指针和函数
- javaCsv解析Csv文件
- web程序中在jsp中调用spring 容器下的service
- 论 mysql 主键
- jquery设置元素的readonly和disabled
- oracle提高查询效率的34条方法