码迷,mamicode.com
首页 > 其他好文 > 详细

线性表

时间:2019-08-10 17:10:07      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:lin   pre   col   lse   def   i++   free   输出   can   

数据组织

typedef int ElemType;

  

typedef struct Node1 {
	ElemType data[MaxCol];
	struct Node1* next;
}DList;

  

typedef struct Node2 {
	int Row, Col;
	DList* next;
}HList;

尾插法

void CreateTable(HList*& h)
{
	DList* r, * s;
	h = (HList*)malloc(sizeof(HList));
	h->next = NULL;
	printf("表的行数列数:");
	scanf("%d%d", &h->Row, &h->Col);

	for (int i = 0; i < h->Row; i++)
	{
		printf("第%d行", i + 1);
		s = (DList*)malloc(sizeof(DList));
		
		for (int j = 0; j < h->Col; j++)
		{
			scanf("%d", &s->data[j]);
		}

		if (h->next == NULL)
		{
			h->next = s;
			r = s;
		}
		if(h->next != NULL)
		{
			r->next = s;
			r = s;
		}
		
	}
	r->next = NULL;
}

  销毁

void DistroyTable(HList*& h)
{
	DList* pre = h->next;
	DList* p = pre->next;
	while (p != NULL)
	{
		free(pre);
		pre = p;
		p = pre->next;
	}
	free(pre);
	free(h);
}

  输出

void DispTable(HList* h)
{
	DList* p = h->next;
	

	while (p != NULL)
	{
		for (int i = 0; i < h->Col; i++)
		{
			printf("%5d", p->data[i]);
		}
		printf("\n");
		p = p->next;
	}

}

  表连接运算

void LinkTable(HList* h1, HList* h2, HList*& h)
{
	int i, j;
	DList* p = h1->next;
	DList* q, * s, * r;
	h = (HList*)malloc(sizeof(HList));
	h->next = NULL;
	h->Row = 0;
	h->Col = h1->Col + h2->Col;

	printf("连接的字段是:第一个表序号,第二个表序号\n");
	scanf("%d%d", &i, &j);
	

	while (p != NULL)
	{
		q = h2->next;
		while (q != NULL)
		{
			if (p->data[i-1] == q->data[j-1])
			{
				s = (DList*)malloc(sizeof(DList));
				for (int k = 0; k < h1->Col; k++)
				{
					s->data[k] = p->data[k];
				}
				for (int k = 0; k < h2->Col; k++)
				{
					s->data[k + h1->Col] = q->data[k];
				}
				if (h->next == NULL)
				{
					h->next = s;
				}
				else
				{
					r->next = s;
				}
				r = s;
				h->Row++;
			}
			
			q = q->next;
		}
		p = p->next;
	}
	r->next = NULL;

}

线性表

标签:lin   pre   col   lse   def   i++   free   输出   can   

原文地址:https://www.cnblogs.com/KIROsola/p/11331954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!