您的位置:首页 > 理论基础 > 数据结构算法

数据结构——链表

2016-01-14 21:36 483 查看

一.线性链表

一般的建立线性链表有两种:

1.正序法:需要三个指针,head作为头指针,pre作为前一个指针,cur作为当前指针用来建立空间

2.倒序法,利用指针的插入,只需要两个指针,不断的往头指针后插入新空间,不过插入的越早,离头指针越远,也就越后面输出

1.线性链表的建立及查找删除

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 45
struct node
{
char title[MAX];
int rating;
struct node *next;
};
struct node *head = NULL;

void insert(struct node *current)//插在第i个后面
{
current = head;
int j = 0,place;
printf("\nInput the place you want to insert\n");
scanf("%d", &place);
getchar();//吃掉换行符
while (j < place-1)
{
current = current->next;
j++;
}
struct node *s =(struct node*)malloc(sizeof(node));
printf("Input the title of insert book\n");
gets_s(s->title);
puts("Enter your rating <0-10>:");
scanf("%d", &s->rating);
s->next = current->next;
current->next = s;
}
void del(struct node *current)//结点删除函数,结点是从0开始的,所以输入1删除的是第二个
{                             //而且有缺陷,输入0还是删除第二个,从1开始都正常
current = head;
int j=0,place;
printf("\nInput the place you want to delete:\n");
scanf("%d", &place);
getchar();
while (j < place - 1)
{
current = current->next;
j++;
}
struct node*q;
q = current->next;
current->next =q->next;
free(q);
}
void outcome(struct node *current)//打印链表
{
printf("\nHere is the list of movie:\n");
current = head;
while (current != NULL)
{
printf("Movie: %s   Rating: %d\n", current->title, current->rating);
current = current->next;
}
}
void clean(struct node *current)//有问题,编译可以通过但最后会出错
{
current = head;
while (current != NULL)//从头开始清空链表
{
free(current);
current = current->next;
}
}

int main()
{
int place;
struct node *pre, *current;
char input[MAX];
puts("Enter first movie title:");
while (gets_s(input) != NULL&&input[0] != '\0')//建立链表并输入数据
{
current = (struct node*)malloc(sizeof(struct node));
if (head == NULL)
head = current;
else
pre->next = current;
current->next = NULL;
strcpy(current->title, input);
puts("Enter your rating <0-10>:");
scanf("%d", current->rating);
while (getchar() != '\n')     //吃掉scanf输入的换行符,防止get将换行符视为输入
continue;
puts("Enter next movie title:");
pre = current;
}
outcome(current);
insert(current);
outcome(current);
del(current);
outcome(current);
//clean(current);
return 0;
}


二:顺序表的合并

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void create(struct node *l, int n)//反序建立链表,后输入的链表排在前面
{
l->next=NULL;
for (int i = n; i > 0; i--)
{
struct node *p = (struct node*)malloc(sizeof(node));
scanf("%d", &p->data);
p->next = l->next;
l->next = p;
}
}
void outcome(const node*l)
{
struct node*p;
p = l->next;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void mergelist(struct node *la, struct node *lb, struct node *lc)
{//la,lb都是顺序的归并后lc也是顺序的
struct node *pa, *pb, *pc;
pa = la->next;
pb = lb->next;
pc = la;
while (pa&&pb)
{
if (pa->data <= pb->data)
{
pc->next=pa;
pc = pa;
pa = pa->next;
}
else
{
pc->next = pb;
pc = pb;
pb = pb->next;
}
}
pc->next = pa ? pa : pb;
}
int main()
{
struct node *la, *lb,*lc;
la = (struct node*)malloc(sizeof(node));
lb = (struct node*)malloc(sizeof(node));
lc = la;
create(la, 5);
create(lb, 3);
mergelist(la, lb, lc);
outcome(lc);
return 0;
}

二:静态链表

1.静态链表的建立和输入输出
#include<stdio.h>
#include<string.h>
#define maxn 3

struct node
{
int data;
int cur;
};

void locate(struct node s[], int e)//查找函数
{
int i = s[0].cur;
while (s[i].data != e)
{
i = s[i].cur;
}
printf("The location is %d\n", i);
}
void creat(struct node s[])//创建函数
{
printf("Input the first cur of s:\n");
scanf("%d",&s[0].cur);//s[0]相当于链表头
int i=s[0].cur;
printf("Input 0 to quit\n");
while (i)
{
printf("Input the data:\n");
scanf("%d", &s[i].data);
printf("Input the cur:\n");
scanf("%d", &s[i].cur);
i = s[i].cur;
}
}
void output(struct node s[])//输出函数
{
printf("This is the list of linklist\n");
int i = s[0].cur;
while (i)
{
printf("%d ", s[i].data);
i = s[i].cur;
}
}

int main()
{
struct node s[maxn];
creat(s);
output(s);
printf("\nInput the data you what to search\n");
int n;
scanf("%d", &n);
locate(s, n);
return 0;
}
2.以(A-B)∪(B-A)为例的静态链表的运用
#include<stdio.h>
#define maxn 1000
struct node
{
int data;
int cur;
};
void init(struct node space[])	  //备用空间的建立
{
for (int i = 0; i < maxn; i++)
space[i].cur = i + 1;
space[maxn - 1].cur = 0;
}
int mall(struct node space[])	  //分配一个结点
{
int i = space[0].cur;
if (space[0].cur)
space[0].cur = space[i].cur;
return i;
}
void clean(struct node space[],int k)//将删除的结点回收到备用空间
{
space[k].cur = space[0].cur;
space[0].cur = k;
}

int main()
{
int i, j, a, b;
struct node space[maxn];
init(space);
int s = mall(space);		  //建立一个头指针
int r = s;					  //建立一个尾指针,指向当前最后结点
printf("Enter the number of A and B\n");
scanf("%d%d", &a, &b);
printf("Enter the date of A\n");
for (j = 1; j <= a; j++)
{
i = mall(space);
scanf("%d", &space[i].data);
space[r].cur = i;		  //插入到表尾
r = i;
}
space[r].cur = 0;             //尾结点的游标为0
printf("Enter the data of B\n");
int num,p,k;
for (j = 1; j <= b; j++)
{
scanf("%d", &num);
p = s;                    //p作为一个结点的前驱
k = space[s].cur;         //k作为同一个结点的后继
while (k != space[r].cur && space[k].data != num)
{						  //查找是否A中有与num一样的数
p = k;
k = space[p].cur;
}
if (p == r)				  //已经搜索到最后说明不存在一样的
{						  //插入在表最后,即r结点后面,但注意r不变,num比较一直与r前比较即A
i = mall(space);	  //不与已经插入的B进行比较,并且结果是反序,因为都插在r后,先插的反而
space[i].data = num;  //排在后面
space[i].cur = space[r].cur;
space[r].cur = i;	  //这几句的次序不能反
}
else					  //元素已经在表中
{
space[p].cur = space[k].cur;
clean(space,k);       //将删除的结点放回备用空间
if (r == k)
r = p;			  //如果删除最后一个结点,尾指针指向的位置要改变
}
}
printf("The following is the result:\n");
i = space[s].cur;
while(i)
{
printf("%d ", space[i].data);
i= space[i].cur;
}
return 0;
}

三:双向链表

1.插入倒序法建立双向链表以及删除操作
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
int data;
struct node *prior;
struct node *next;
}TYPE;
TYPE *l = (TYPE*)malloc(sizeof(TYPE));

void outcome(TYPE *p)	     //输出函数
{
p = l->next;
printf("The following is the list of data\n");
while (p!= NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void del(TYPE *p)           //删除函数
{
int i, j = 1;
printf("Enter the place you want you delete:\n");
scanf("%d", &i);
p = l->next;
while (j < i)
{
p = p->next;
j++;
}
p->prior->next = p->next;
p->next->prior = p->prior;
free(p);
}
int main()
{
TYPE *p=l;
l->next = l->prior = NULL;
int num;
printf("Enter the number of list you want to establish\n");
scanf("%d", &num);
while (num--)
{
p = (TYPE*)malloc(sizeof(TYPE));
scanf("%d",&p->data);
p->next = l->next;
p->prior = l;
if(l->next!=NULL)
l->next->prior = p;
l->next = p;
}
outcome(p);
del(p);
outcome(p);
return 0;
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: