1. 程式人生 > >資料結構(C語言版)第三版 基礎實驗二

資料結構(C語言版)第三版 基礎實驗二

1、編寫函式slnklist delx(linklist head, datatype x),刪除不帶頭結點單鏈表head中第一個值為x 的結點。
並構造測試用例進行測試。

2、假設線性表(a1,a2,a3,…an)採用不帶頭結點的單鏈表儲存,
請設計演算法函式linklist reverse1(linklist  head)和
void reverse2(linklist *head)將不帶頭結點的單鏈表head就地倒置,
使表變成(an,an-1,…a3.a2,a1)。並構造測試用例進行測試。

3、假設不帶頭結點的單鏈表head是升序排列的,設計演算法函式linklist insert(linklist head,datatype x),
將值為x的結點插入到連結串列head中,並保持連結串列有序性。
分別構造插入到表頭、表中和表尾三種情況的測試用例進行測試。

4、編寫演算法函式linklist delallx(linklist head, int x),刪除不帶頭結點單鏈表head中所有值為x的結點。

不帶頭結點的連結串列結構定義及頭插法建表,尾插法建表和列印連結串列等函式定義

#include<stdio.h>
#include<stdlib.h>
typedef int datatype;
typedef struct link_node
{
	datatype info;
	struct link_node *next;
}node;
typedef node *link_list;
link_list creatbystack()/*頭插法建立一個單鏈表*/
{
	link_list head, s;
	datatype x;
	head = NULL;
	printf("請輸入若干整數序列:\n");
	scanf_s("%d", &x);
	while (x != 0)/*以0結束輸入*/
	{
		s = (link_list)malloc(sizeof(node));
		s->info = x;
		s->next = head;/*/將新結點插入到單鏈表最前面*/
		head = s;
		scanf_s("%d", &x);
	}
	return head;/*返回建立的新連結串列*/
}
link_list creatbyqueue()/*尾插法建立單鏈表*/
{
	link_list head, s, r;
	datatype x;
	head = r = NULL;
	printf("請輸入若干整數序列");
	scanf_s("%d", &x);
	while (x != 0)/*以0結束輸入*/
	{
		s = (link_list)malloc(sizeof(node));
		s->info = x;
		if (head == NULL) /*將新結點插入到連結串列最後面*/
			head = s;
		else
			r->next = s;
		r = s;
		scanf_s("%d", &x);
	}
	if (r)
		r->next = NULL;
	return head;

}
void print(link_list head)
{
	link_list p;
	int i = 0;
	p = head;
	printf("List is:\n");
	while (p)
	{
		printf("%5d", p->info);
		p = p->next;
		i++;
		if (i % 10 == 0)
			printf("\n");
	}
	printf("\n");
}
void delList(link_list head)/*釋放不帶頭結點的單鏈表*/
{
	link_list p = head;
	while (p)
	{
		head = p->next;
		free(p);
		p = head;
	}
}

一,

void delList(link_list head)/*釋放不帶頭結點的單鏈表*/
{
	link_list p = head;
	while (p)
	{
		head = p->next;
		free(p);
		p = head;
	}
}
link_list delx(link_list head, datatype x)/*刪除不帶頭結點單鏈表head中的第一個值為x的值*/
{
	link_list p = head, q, pre = NULL;
	while (p&&p->info != x)
	{
		pre = p;
		p = p->next;
	}
	if (p)
	{
		if (!pre)
			head = head->next;
		else
			pre->next = p->next;
		free(p);
	}
	return head;
}
int main()

{
	link_list head;
	datatype x;
	head = creatbyqueue();
	print(head);
	printf("請輸入你要刪除的數:");
	scanf_s("%d", &x);
	head = delx(head, x);
	print(head);
	delList(head);
	system("pause");
	return 0;
}

二,

link_list reverse1(link_list head)
{
	link_list p = head, q, pre = NULL;
	while (p != NULL)
	{
		q = p;
		p = p->next;
		q->next = pre;
		pre = q;
	}
	return pre;
}
void reverse2(link_list *head)
{
	link_list p = *head, pre = NULL;
	while (p != NULL)
	{
		*head = p->next;
		p->next = pre;
		pre = p;
		p = *head;
	}
	*head = pre;
}
int main()
{
	link_list head;
	datatype x;
	head = creatbystack();/*頭插法單鏈表*/
	print(head);/*輸出原單鏈表*/
	head = reverse1(head);/*倒置單鏈表*/
	print(head);/*輸出倒置後的單鏈表*/
	reverse2(&head);/*倒置單鏈表*/
	print(head);
	delList(head);
	system("pause");
	return 0;
}

 三,

link_list insert(link_list head, datatype x)
{
	link_list pre = NULL, p = head, h;
	h = (link_list)malloc(sizeof(node));
	h->info = x;
	while (p&&p->info < x)
	{
		pre = p;
		p = p->next;
	}
	if (pre == NULL)
	{
		h->next = head;
		head = h;
	}
	else if (pre != NULL)
	{
		h->next = p->next;
		p->next = h;
	}
	else
	{
		pre->next = h;
		h->next = NULL;
	}
	return head;
}

int main()
{
	datatype x;
	link_list head;
	printf("輸入一組升序排列的整數:\n");
	head = creatbyqueue();				/*尾插入法建立單鏈表*/
	print(head);
	printf("請輸入要插入的值:");
	scanf_s("%d", &x);
	head = insert(head, x);				/*將輸入的值插入到單鏈表適當位置*/
	print(head);
	delList(head);
	system("pause");
	return 0;

 四,

link_list delallx(link_list head, datatype x)
{
	link_list p = head, pre = NULL;
	do
	{
		while (p&&p->info != x)
		{
			pre = p;
			p = p->next;
		}
		if (p)
		{
			if (!pre)
				head = head->next,pre = NULL;
			else
				pre->next = p->next;
		}
		p = p->next;
	} while (p);
	return head;
}
int main()
{
	datatype x;
	link_list head;
	head = creatbyqueue();				/*尾插入法建立單鏈表*/
	print(head);
	printf("請輸入要刪除的值:");
	scanf_s("%d", &x);
	head = delallx(head, x);
	print(head);
	delList(head);
	system("pause");
	return 0;
}