c语言简单链表实现
2016-12-01 15:44
507 查看
#ifndef _LIST_H_
#define _LIST_H_
//避免重复包含----条件编译
typedef int DataType;
typedef struct node
{
DataType data;
struct node *next;
}Node, *Pnode;
//01创建节点
Pnode createNode(DataType data);
//02插入节点
int insertNextNode(Pnode p1, Pnode p2);
int insertDataBySort(Pnode *phead, DataType data);
//03查询
void printList(Pnode head);
//04删除
void destroyList(Pnode head);
#endif
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
//创建节点
Pnode createNode(DataType data)
{
Pnode p = (Pnode)malloc(sizeof(Node));
if (p != NULL)
{
p->data = data;
p->next = NULL;
}
return p;
}
//插入节点
int insertNextNode(Pnode p1, Pnode p2)
{
if (p1 != NULL&&p2 != NULL)
{
if (p1->next == NULL)
{
p1->next = p2;
}
else
{
p2->next = p1->next;
p1->next = p2;
}
return 0;
}
return -1;
}
int insertDataBySort(Pnode *phead, DataType data)
{
if (phead == NULL)
{
return -1;
}
if (*phead == NULL)
{
*phead=createNode(data);
}
else
{
Pnode pnew = createNode(data);
Pnode p1 = *phead;
if (p1->data > data)//解决头指针插入问题
{
insertNextNode(pnew, p1);
*phead = pnew;
return 0;
}
Pnode pre = NULL;
while (p1 != NULL)
{
if (p1->data <data)
{
pre = p1;
}
p1 = p1->next;
}
insertNextNode(pre,pnew);
}
return 0;
}
//03查询
void printList(Pnode head)
{
if (head != NULL)
{
Pnode ptemp = head;
while (ptemp != NULL)
{
printf("%d\n", ptemp->data);
ptemp = ptemp->next;
}
printf("------------------------------\n");
}
}
//04删除
void destroyList(Pnode head)
{
if (head != NULL)
{
Pnode ptemp = head;
head = head->next;
free(ptemp);
}
}
test1()
{
Pnode p1= createNode(1);
Pnode p2 = createNode(2);
Pnode p3 = createNode(3);
insertNextNode(p1, p3);
printList(p1);
insertNextNode(p1, p2);
printList(p1);
getchar();
}
test2()
{
Pnode head = NULL;
insertDataBySort(&head, 2);
insertDataBySort(&head, 3);
printList(head);
//destroyList(head);
//head = NULL;
insertDataBySort(&head, 1);
insertDataBySort(&head, 4);
printList(head);
//destroyList(head);
//head = NULL;
//printList(head);
getchar();
}
int main()
{
//test1();
test2();
}
#define _LIST_H_
//避免重复包含----条件编译
typedef int DataType;
typedef struct node
{
DataType data;
struct node *next;
}Node, *Pnode;
//01创建节点
Pnode createNode(DataType data);
//02插入节点
int insertNextNode(Pnode p1, Pnode p2);
int insertDataBySort(Pnode *phead, DataType data);
//03查询
void printList(Pnode head);
//04删除
void destroyList(Pnode head);
#endif
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
//创建节点
Pnode createNode(DataType data)
{
Pnode p = (Pnode)malloc(sizeof(Node));
if (p != NULL)
{
p->data = data;
p->next = NULL;
}
return p;
}
//插入节点
int insertNextNode(Pnode p1, Pnode p2)
{
if (p1 != NULL&&p2 != NULL)
{
if (p1->next == NULL)
{
p1->next = p2;
}
else
{
p2->next = p1->next;
p1->next = p2;
}
return 0;
}
return -1;
}
int insertDataBySort(Pnode *phead, DataType data)
{
if (phead == NULL)
{
return -1;
}
if (*phead == NULL)
{
*phead=createNode(data);
}
else
{
Pnode pnew = createNode(data);
Pnode p1 = *phead;
if (p1->data > data)//解决头指针插入问题
{
insertNextNode(pnew, p1);
*phead = pnew;
return 0;
}
Pnode pre = NULL;
while (p1 != NULL)
{
if (p1->data <data)
{
pre = p1;
}
p1 = p1->next;
}
insertNextNode(pre,pnew);
}
return 0;
}
//03查询
void printList(Pnode head)
{
if (head != NULL)
{
Pnode ptemp = head;
while (ptemp != NULL)
{
printf("%d\n", ptemp->data);
ptemp = ptemp->next;
}
printf("------------------------------\n");
}
}
//04删除
void destroyList(Pnode head)
{
if (head != NULL)
{
Pnode ptemp = head;
head = head->next;
free(ptemp);
}
}
test1()
{
Pnode p1= createNode(1);
Pnode p2 = createNode(2);
Pnode p3 = createNode(3);
insertNextNode(p1, p3);
printList(p1);
insertNextNode(p1, p2);
printList(p1);
getchar();
}
test2()
{
Pnode head = NULL;
insertDataBySort(&head, 2);
insertDataBySort(&head, 3);
printList(head);
//destroyList(head);
//head = NULL;
insertDataBySort(&head, 1);
insertDataBySort(&head, 4);
printList(head);
//destroyList(head);
//head = NULL;
//printList(head);
getchar();
}
int main()
{
//test1();
test2();
}
相关文章推荐
- 多项式相加(链表的汇编语言实现)
- [导入]Winform程序多语言国际化实现的简单方法
- 简单链表的实现
- 用RMI实现一个简单的实时聊天系统(java语言)
- 一个简单的双向循环链表的实现
- Java语言中链表和双向链表的实现
- C# 简单链表的实现下(使用泛型)
- 转载的标准文档:C语言实现一个简单的单向链表list
- 全球化与本地化之全球化 多语言简单实现
- Dev的多语言简单实现
- asp.net 2.0 三层架构的实现(最最原始,简单,一般, 但不平凡)用通俗的语言说出难以理解的。。。
- 使用C#链表简单实现的约瑟夫环
- Winform程序多语言国际化实现的简单方法
- c#中简单链表的实现(非泛型)
- 单向链表的简单实现
- java 语言中链表和双向链表的实现!
- C语言实现一个简单的单向链表list
- JAVA 语言中链表和双向链表的实现
- Java语言中链表和双向链表的实现
- C# 简单链表的实现上(object类型)