链表技术。
来源:互联网 发布:星界边境正版没网络 编辑:程序博客网 时间:2024/05/29 13:18
#include <stdio.h>
#include <stdlib.h>
#define ERROR1 "*** Wrong : the position is less than 1 ***/n"
#define ERROR2 "*** Wrong : the position is more than the length of linklist ***/n"
typedef struct node
{
int data;
struct node *next;
} listnode, *linklist;
/***************************************/
void print()
{
printf("******************linklist a********************/n");
printf("* */n");
printf("* 1> insert a node into the linklist */n");
printf("* 2> insert a node into the linklist ascendly */n");
printf("* 3> delete a node depending on position */n");
printf("* 4> delete nodes depending on value */n");
printf("* 5> search the linklist according to a value */n");
printf("* 6> return the value depending on position */n");
printf("* 7> clear the linklist */n");
printf("* 8> reverse the linklist */n");
printf("* 9> merge the linklist with La and Lb */n");
printf("* 10> exit */n");
printf("* */n");
printf("************************************************/n");
}
/***************************************/
linklist CreateLinklist_1()
{
linklist head;
head = (linklist)malloc(sizeof(listnode));
head->next = NULL;
return head;
}
/***************************************/
void CreateLinklist_2(listnode **head)
{
*head = (linklist)malloc(sizeof(listnode));
(*head)->next = NULL;
return ;
}
/***************************************/
int LengthLinklist(linklist head)
{
int count = 0;
linklist p = head->next;
while( p )
{
count++;
p = p->next;
}
return count;
}
/***************************************/
void VisitLinklist(linklist head)
{
linklist p = head->next;
while ( p )
{
printf("%d ", p->data);
p = p->next;
}
printf("/n");
}
/***************************************/
int EmptyList(linklist head)
{
return ( NULL == head->next ? 1 : 0 );
}
/***************************************/
int SearchLinklist(linklist head, int value)
{
linklist p = head->next;
int pos = 1;
while ( p )
{
if ( p->data == value )
{
return pos;
}
p = p->next;
pos++;
}
return 0;
}
/***************************************/
int GetLinklist(linklist head, int pos, int *val)
{
linklist p = head;
if ( pos < 1 )
{
printf(ERROR1);
return -1;
}
while ( pos-- )
{
p = p->next;
if ( NULL == p )
{
printf(ERROR2);
return -1;
}
}
*val = p->data;
return 0;
}
/***************************************/
int InsertLinklist_1(linklist head, int pos, int value)
{
linklist p,q;
if ( pos < 1 )
{
printf(ERROR1);
return -1;
}
p = head;
while ( --pos )
{
p = p->next;
if ( NULL == p )
{
printf(ERROR2);
return -1;
}
}
q = (linklist)malloc(sizeof(listnode));
q->data = value;
q->next = p->next;
p->next = q;
return 0;
}
/***************************************/
void InsertLinklist_2(linklist head, int value)
{
linklist p = head, q;
while ( (p->next != NULL) && (p->next->data < value) )
{
p = p->next;
}
q = (linklist)malloc(sizeof(listnode));
q->data = value;
q->next = p->next;
p->next = q;
return;
}
/***************************************/
int DeleteLinklist_1(linklist head, int pos)
{
linklist p = head,q;
if ( pos < 1 )
{
printf(ERROR1);
return -1;
}
while( --pos )
{
p = p->next;
if ( NULL == p )
{
printf(ERROR2);
return -1;
}
}
if ( NULL == (q = p->next) )
{
printf(ERROR2);
return -1;
}
p->next = q->next;
free(q);
return 0;
}
/***************************************/
void DeleteLinklist_2(linklist head, int value)
{
linklist p,q;
p = head;
q = p->next;
while( q )
{
if ( q->data == value )
{
p->next = q->next;
free(q);
}
else
{
p = q;
}
q = p->next;
}
return;
}
/***************************************/
void ClearLinklist(linklist head)
{
linklist p,q;
p = head->next;
while ( p )
{
q = p;
p = p->next;
free(q);
}
head->next = NULL;
return;
}
/***************************************/
void MergeLinklist(linklist La, linklist Lb)//按链表数值的大小连接两个链表
{
linklist pa,pb,r;
r = La;
pa = La->next;
pb = Lb->next;
while ( pa && pb )
{
if ( pa->data < pb->data )
{
r->next = pa;
r = pa;
pa = pa->next;
}
else
{
r->next = pb;
r = pb;
pb = pb->next;
}
}
if ( pa == NULL )
{
r->next = pb;
}
else
{
r->next = pa;
}
Lb->next = NULL;
return;
}
/***************************************/
void ReverseLinklist(linklist head)//对单链表实现就地逆转。
{
linklist p,q;
p = head->next;
head->next = NULL;
while(p)
{
q = p;
p = p->next;
q->next = head->next;
head->next = q;
}
}
/***************************************/
void ReverseLinklist_1(linklist h, linklist p)
{
linklist q;
if ( NULL == p ) return;
else
{
q = p->next;
p->next = h->next;
h->next = p;
ReverseLinklist(h, q);
}
}
/***************************************/
int main()
{
int i,value,pos,result;
linklist ha, hb;
ha = CreateLinklist_1();
CreateLinklist_2(&hb);
for(i=0;i<10;i++)
{
InsertLinklist_1(hb,i+1,i+1);
}
printf("length of the linklist(b) : %d /n", LengthLinklist(hb));
VisitLinklist(hb);
while ( 1 )
{
print();
printf("Please choose your operation : ");
scanf("%d", &i);
if ( i == 10 ) break;
switch ( i )
{
case 1 :
printf("Please input as format <value> <pos> : ");
scanf("%d %d", &value, &pos);
InsertLinklist_1(ha, pos, value);
break;
case 2 :
printf("Please input as format <value> : ");
scanf("%d", &value);
InsertLinklist_2(ha, value);
break;
case 3 :
printf("Please input the position you want to delete : ");
scanf("%d", &pos);
DeleteLinklist_1(ha, pos);
break;
case 4 :
printf("Please input the value you want to delete : ");
scanf("%d", &value);
DeleteLinklist_2(ha, value);
break;
case 5 :
printf("Pleae input the value you want to search : ");
scanf("%d", &value);
if ( (result=SearchLinklist(ha, value)) > 0 )
{
printf("# Yes, we got it, the position is %d/n", result);
}
else
{
printf("# No, we can't find it/n");
}
break;
case 6 :
printf("Please input the position you want to get : ");
scanf("%d", &pos);
if ( GetLinklist(ha, pos, &value) == 0 )
{
printf("The value of node #%d is %d/n", pos, value);
}
break;
case 7 :
ClearLinklist(ha);
break;
case 8 :
ReverseLinklist(ha);
break;
case 9 :
MergeLinklist(ha, hb);
break;
}
printf("length of the linklist(a) : %d /n", LengthLinklist(ha));
VisitLinklist(ha);
printf("/n");
}
ClearLinklist(ha);
ClearLinklist(hb);
free(ha);
free(hb);
return 0;
}
- 链表技术。
- 区块链技术术语表
- 区块链技术术语表
- IIS防盗链技术
- 网站防盗链技术
- 防盗链技术
- 05-防盗链技术
- PHP防盗链技术
- 区块链技术
- 区块链技术安全
- 区块链技术原理
- 区块链技术原理
- 区块链技术原理
- 防盗链技术
- 区块链技术进阶
- 区块链技术原理
- 什么是区块链技术?
- 链路备份技术
- struts2 spring3 hibernate3整合
- 最大公约数算法
- 泛型算法
- Shell Programming
- Python 与PHP
- 链表技术。
- 问题
- g723源码详细分析(二) lpc转lsp
- MSD30X系列芯片软件编译说明
- Base64
- linux下的内存映射函数mmap详解及示例代码
- SQL导出表数据到远程服务器
- 美国中情局刺杀拉登实录
- 美国中情局刺杀拉登实录