单链表的建立,插入,删除,逆置
2010-01-22 16:45
435 查看
#include<stdio.h>
#include<stdlib.h>
#define FLAG -1
typedef struct node
{
int data;
struct node *next;
}Node, *Linklist;
/*
Linklist HeadCreat()
{
Linklist H = NULL;
Node *s;
int x = 0;
scanf("%d",&x);
while(x != FLAG)
{
s = (Linklist)malloc(sizeof(Node));
s->data = x;
s->next = H;
H = s;
scanf("%d",&x);
}
return H;
}
*/
Linklist TailCreat()
{
Linklist H = NULL;
Node *s,*t = NULL;
int x = 0;
scanf("%d",&x);
while(x != FLAG)
{
s = (Linklist)malloc(sizeof(Node));
s->data = x;
s->next = NULL;
if(H == NULL)
H = s;
else t->next = s;
t = s;
scanf("%d",&x);
}
if(t != NULL)
t->next = NULL;
return H;
}
int Listlen(Linklist H)
{
int j = 0;
Node *s = H;
if (s == NULL)
return 0;
else
while (s->next)
{
s = s->next;
j++;
}
return j+1;
}
Linklist Locate(Linklist H,int x)
{
Node *before = NULL;
Node *local = H;
while((local) && (local->data < x))
{
before = local;
local = local->next;
}
if((local)&&(before->data < x)&&(local->data > x))
return before;
}
Linklist Insert(Linklist H,int x)
{
Node *p = NULL;
Node *s = NULL;
p = Locate(H,x);
if(p == NULL)
{
printf(" Error!");
return H;
}
else
{
s = (Linklist)malloc(sizeof(Node));
s->data = x;
s->next = p->next;
p->next = s;
return H;
}
}
Node * Get(Linklist H,int i)
{
Node *p = H;
int j=0;
while( (p->next != NULL) && (j<i) )
{
p=p->next;
j++;
}
if(j == i)
return p;
else
return NULL;
}
Linklist Del(Linklist H,int i)
{
Node *p = NULL;
Node *s = NULL;
p = Get(H,i-1);
if (p == NULL)
{
printf("The (i-1)th node is not in! ");
return H;
}
else if(p->next == NULL)
{
printf("The ith node is not in!!");
return H;
}
else
{
s=p->next;
p->next=s->next;
free(s);
return H;
}
}
Linklist Delsame(Linklist H)
{ // 若输入11221133只能输出1213 怎么改可以去掉所有相同的节点
Node *p = NULL;
Node *s = NULL;
Node *t = NULL;
if (H == NULL)
return H;
p = H;
while( p->next)
{
s = p;
t = p->next;
while(s->next)
{
if (s->data == t->data)
{
s->next = t->next;
free(t);
t = s->next;
}
else
{
s = s->next;
t = t->next;
}
}
s = s->next;
if( s == NULL)
return H;
}
}
Linklist Reverse(Linklist H)
{
Node *p_1 = NULL;
Node *p_2 = NULL;
p_1=H;
p_2 = p_1->next;
H = p_2->next;
p_1->next = NULL;
while(H->next)
{
p_2->next = p_1;
p_1 = p_2;
p_2 = H;
H = H->next;
}
H->next = p_2;
p_2->next = p_1;
return H;
}
void print(Linklist H)
{
Linklist p = NULL;
p = H;
while(p)
{
printf("<<%d>>",p->data);
p=p->next;
}
}
int main()
{
Linklist H = NULL;
Node *p;
int x=0;
int n=0;
printf("Please input the numbers:!/n");
// H=HeadCreat();
H=TailCreat();
printf("The numbers you input is :/n");
print(H);
printf("/n");
x=Listlen(H);
printf("The lenth of the list is : %d /n",x);
printf("Please in put the number you wanna to add in :/n");
scanf("%d",&x);
H=Insert(H,x);
printf("The total numbers you input is :/n");
print(H);
printf("/n");
printf("Please in put the number you wanna to delete:/n");
scanf("%d",&n);
printf("The total numbers you input is :/n");
H=Del(H,n);
print(H);
printf("/n");
printf("The total numbers you input after reverseis :/n");
H=Reverse(H);
print(H);
printf("/n");
printf("Please input the numbers:/n");
H=TailCreat();
H=Delsame(H);
print(H);
}
#include<stdlib.h>
#define FLAG -1
typedef struct node
{
int data;
struct node *next;
}Node, *Linklist;
/*
Linklist HeadCreat()
{
Linklist H = NULL;
Node *s;
int x = 0;
scanf("%d",&x);
while(x != FLAG)
{
s = (Linklist)malloc(sizeof(Node));
s->data = x;
s->next = H;
H = s;
scanf("%d",&x);
}
return H;
}
*/
Linklist TailCreat()
{
Linklist H = NULL;
Node *s,*t = NULL;
int x = 0;
scanf("%d",&x);
while(x != FLAG)
{
s = (Linklist)malloc(sizeof(Node));
s->data = x;
s->next = NULL;
if(H == NULL)
H = s;
else t->next = s;
t = s;
scanf("%d",&x);
}
if(t != NULL)
t->next = NULL;
return H;
}
int Listlen(Linklist H)
{
int j = 0;
Node *s = H;
if (s == NULL)
return 0;
else
while (s->next)
{
s = s->next;
j++;
}
return j+1;
}
Linklist Locate(Linklist H,int x)
{
Node *before = NULL;
Node *local = H;
while((local) && (local->data < x))
{
before = local;
local = local->next;
}
if((local)&&(before->data < x)&&(local->data > x))
return before;
}
Linklist Insert(Linklist H,int x)
{
Node *p = NULL;
Node *s = NULL;
p = Locate(H,x);
if(p == NULL)
{
printf(" Error!");
return H;
}
else
{
s = (Linklist)malloc(sizeof(Node));
s->data = x;
s->next = p->next;
p->next = s;
return H;
}
}
Node * Get(Linklist H,int i)
{
Node *p = H;
int j=0;
while( (p->next != NULL) && (j<i) )
{
p=p->next;
j++;
}
if(j == i)
return p;
else
return NULL;
}
Linklist Del(Linklist H,int i)
{
Node *p = NULL;
Node *s = NULL;
p = Get(H,i-1);
if (p == NULL)
{
printf("The (i-1)th node is not in! ");
return H;
}
else if(p->next == NULL)
{
printf("The ith node is not in!!");
return H;
}
else
{
s=p->next;
p->next=s->next;
free(s);
return H;
}
}
Linklist Delsame(Linklist H)
{ // 若输入11221133只能输出1213 怎么改可以去掉所有相同的节点
Node *p = NULL;
Node *s = NULL;
Node *t = NULL;
if (H == NULL)
return H;
p = H;
while( p->next)
{
s = p;
t = p->next;
while(s->next)
{
if (s->data == t->data)
{
s->next = t->next;
free(t);
t = s->next;
}
else
{
s = s->next;
t = t->next;
}
}
s = s->next;
if( s == NULL)
return H;
}
}
Linklist Reverse(Linklist H)
{
Node *p_1 = NULL;
Node *p_2 = NULL;
p_1=H;
p_2 = p_1->next;
H = p_2->next;
p_1->next = NULL;
while(H->next)
{
p_2->next = p_1;
p_1 = p_2;
p_2 = H;
H = H->next;
}
H->next = p_2;
p_2->next = p_1;
return H;
}
void print(Linklist H)
{
Linklist p = NULL;
p = H;
while(p)
{
printf("<<%d>>",p->data);
p=p->next;
}
}
int main()
{
Linklist H = NULL;
Node *p;
int x=0;
int n=0;
printf("Please input the numbers:!/n");
// H=HeadCreat();
H=TailCreat();
printf("The numbers you input is :/n");
print(H);
printf("/n");
x=Listlen(H);
printf("The lenth of the list is : %d /n",x);
printf("Please in put the number you wanna to add in :/n");
scanf("%d",&x);
H=Insert(H,x);
printf("The total numbers you input is :/n");
print(H);
printf("/n");
printf("Please in put the number you wanna to delete:/n");
scanf("%d",&n);
printf("The total numbers you input is :/n");
H=Del(H,n);
print(H);
printf("/n");
printf("The total numbers you input after reverseis :/n");
H=Reverse(H);
print(H);
printf("/n");
printf("Please input the numbers:/n");
H=TailCreat();
H=Delsame(H);
print(H);
}
相关文章推荐
- 使用JAVA实现单链表的建立、测长、打印、删除、插入、排序、逆置
- 【数据结构】单链表的建立,测长,打印,删除,插入, 排序, 逆置
- 程序员面试宝典(第三版)——单链表的基本操作:建立,求长度,输出,排序,插入,删除,逆置
- 单链表的建立,测长,打印,删除,插入,排序,逆置
- 程序员面试宝典(第三版)——单链表的基本操作:建立,求长度,输出,排序,插入,删除,逆置(转)
- 单链表的建立,查找,插入,删除,测长,打印,逆置操作实现
- 单链表的建立,测长度,打印,删除,插入,逆置c++代码实例及运行结果
- 实现单链表的创建、打印、插入、删除、逆置
- 单链表的建立,测长度,打印,删除,插入,逆置c++代码实例及运行结果
- 单链表的建立、插入、删除等简单操作
- 单链表的建立、插入、删除、遍历
- 单链表(建立、插入、删除、打印)
- 编程实现双链表的建立、插入、删除、求长、逆置
- 单链表的建立,测长度,打印,删除,插入,逆置c++代码实例及运行结果
- 单链表的建立,测长度,打印,删除,插入,逆置c++代码实例及运行结果
- 单链表的建立,插入,显示,查找删除以及反转
- 单链表中头结点的有无. 并讨论下有无头结点在单链表的创建,打印,插入,逆置,删除中的区别.
- 单链表的建立、求长、插入、删除、输出和释放 (带头结点)
- 单链表的创建(头插尾插),表长,输出,插入,删除,查找,逆置,分解长两个链表(奇数偶数链表),查找倒数第k个元素,产出相同元素
- C++实现单链表的创建、插入、删除、逆置操作