链表的基本操作。。。
来源:互联网 发布:陌陌切图软件 编辑:程序博客网 时间:2024/06/01 11:19
#include "node.h"
/*打印链表*/
void print(node* head)
{
if(head == NULL)
printf("链表为空\n");
printf("链表数据依次为:");
while(head != NULL)
{
printf("%d ",head->data);
head = head->next;
}
printf("\n");
}
/*从尾部插入*/
void insert_tail(node** head,const int d)
{
node* t = *head;
node* p = (node*)malloc(sizeof(node*));
p->data = d;
if(*head == NULL)
{
*head = p;
p->next = NULL;
return;
}
while(t->next != NULL)
{
t = t->next;
}
t ->next = p;
p ->next = NULL;
}
/*从头部插入*/
void insert_head(node** head,const int d)
{
node* p = (node*)malloc(sizeof(node*));
p -> data = d;
if((*head) == NULL)
{
(*head) = p;
p ->next = NULL;
return;
}
p->next = *head;
*head = p;
}
/*指定位置插入*/
void insert_pos(node** head, int pos,const int d)
{
node* t = *head;
node* p =(node*)malloc(sizeof(node*));
p -> data = d;
if(pos > (size(*head)+1))
{
printf("位置输入有误,请重新输入\n");
return ;
}
if(*head == NULL)
{
(*head) = p;
p->next = NULL;
return ;
}
while(pos-1) /*找到pos位置*/
{
t = t->next;
pos--;
}
p->next = t->next;
t->next = p;
}
/*指定位置删除*/
void del_pos(node** head,int pos)
{
node* t = *head;
if(pos > size(*head) || pos == 0)
{
printf("位置输入有误,请重新输入\n");
return ;
}
if(pos == 1)
{
node* fre = *head;
(*head) = t->next;
free(fre);
return ;
}
while(pos - 2) /*找到要删除节点的前一个节点*/
{
t = t->next;
pos--;
}
node* fre = t->next;
t->next = fre->next;
free(fre);
}
/*链表长度*/
int size(node* head)
{
int n = 0;
while(head != NULL)
{
head = head ->next;
n++;
}
return n;
}
/*反转链表*/
void rev(node** head)
{
node* p1 = *head;
node* p2 = p1->next;
node* tmp = NULL;
while(p2 != NULL)
{
tmp = p2->next;
p2->next = p1;
p1 = p2;
p2 = tmp;
}
(*head)->next = NULL;
*head = p1;
}
/*打印链表*/
void print(node* head)
{
if(head == NULL)
printf("链表为空\n");
printf("链表数据依次为:");
while(head != NULL)
{
printf("%d ",head->data);
head = head->next;
}
printf("\n");
}
/*从尾部插入*/
void insert_tail(node** head,const int d)
{
node* t = *head;
node* p = (node*)malloc(sizeof(node*));
p->data = d;
if(*head == NULL)
{
*head = p;
p->next = NULL;
return;
}
while(t->next != NULL)
{
t = t->next;
}
t ->next = p;
p ->next = NULL;
}
/*从头部插入*/
void insert_head(node** head,const int d)
{
node* p = (node*)malloc(sizeof(node*));
p -> data = d;
if((*head) == NULL)
{
(*head) = p;
p ->next = NULL;
return;
}
p->next = *head;
*head = p;
}
/*指定位置插入*/
void insert_pos(node** head, int pos,const int d)
{
node* t = *head;
node* p =(node*)malloc(sizeof(node*));
p -> data = d;
if(pos > (size(*head)+1))
{
printf("位置输入有误,请重新输入\n");
return ;
}
if(*head == NULL)
{
(*head) = p;
p->next = NULL;
return ;
}
while(pos-1) /*找到pos位置*/
{
t = t->next;
pos--;
}
p->next = t->next;
t->next = p;
}
/*指定位置删除*/
void del_pos(node** head,int pos)
{
node* t = *head;
if(pos > size(*head) || pos == 0)
{
printf("位置输入有误,请重新输入\n");
return ;
}
if(pos == 1)
{
node* fre = *head;
(*head) = t->next;
free(fre);
return ;
}
while(pos - 2) /*找到要删除节点的前一个节点*/
{
t = t->next;
pos--;
}
node* fre = t->next;
t->next = fre->next;
free(fre);
}
/*链表长度*/
int size(node* head)
{
int n = 0;
while(head != NULL)
{
head = head ->next;
n++;
}
return n;
}
/*反转链表*/
void rev(node** head)
{
node* p1 = *head;
node* p2 = p1->next;
node* tmp = NULL;
while(p2 != NULL)
{
tmp = p2->next;
p2->next = p1;
p1 = p2;
p2 = tmp;
}
(*head)->next = NULL;
*head = p1;
}
0 0
- 链表的基本操作
- 链表的基本操作
- 链表的基本操作
- 链表的基本操作
- 链表的基本操作
- 链表的基本操作
- 链表的基本操作
- 链表的基本操作
- 链表的基本操作
- 链表的基本操作
- 链表的基本操作
- 链表的基本操作
- 链表的基本操作
- 链表的基本操作
- 链表的基本操作
- 链表的基本操作
- 链表的基本操作
- 链表的基本操作
- Oracle 8.1.7.4 数据库故障恢复一例
- java数据类型及运算符、结构控制
- Building a LAMP Server
- cocos2d-x 3.0 Android编译环境的搭建win7+Eclipse
- 通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件
- 链表的基本操作。。。
- Oracle current_date和sysdate时间不一致
- python爬虫中对含中文的url处理
- XML中的DTD和XSD
- Oracle Rac clusterware startup sequence (11gR2)
- 控件使用中的ShowWindow 和 EnableWindow选择
- Eclipse的使用快捷键
- 从微软的DBML文件中我们能学到什么(它告诉了我们什么是微软的重中之重)~七 为DBContext对象应该作为其它实体操作类型的基类,并为它提供统一的提交动作
- 面向切面编程(AOP)的理解