顺序表的实现操作

#define _CRT_SECURE_NO_WARNINGS

//放测试代码

#include <stdio.h>

#include "dsqlist.h"

//设顺序表va中的数据元素递增有序。试写一算法,将x插入到顺序表的适当位置上,

//以保持该表的有序性

bool Insert_x(PDSqList va,ElemType x)

{

int i;

for (i = va->length - 1; i >= 0; i--)//从后往前找,如果当前数字比x大,直接后移一位

{

if (va->elem[i] > x)

va->elem[i + 1] = va->elem[i];

else//va->elem[i]<=x

break;

}

//x放在i的后面

va->elem[i + 1] = x;

va->length++;

return true;

}

int main()

{

DSqList ds;

InitList(&ds);

Insert_tail(&ds,1);

Insert_tail(&ds, 3);

Insert_tail(&ds, 8);

Insert_tail(&ds, 12);

Show(&ds);

Insert_x(&ds,5);

Insert_x(&ds, -5);

Insert_x(&ds, 50);

Show(&ds);

return 0;

}

#include <vld.h>

//#include "dlist.h"

//

//int main()

//{

// DuLNode head;

// InitList(&head);

// for (int i = 0; i < 10; i++)

// {

// //Insert_head(&head,i);

// Insert_tail(&head,i);

// }

// printf("%d\n",GetLength(&head));

// ElemType val;

// if (GetElem(&head, 50, &val))

// printf("%d\n", val);

// else

// printf("获取数据失败\n");

//

// GetElem(&head, 5, &val);

// printf("%d\n",val);

//

// GetPrio(&head,5,&val);

// printf("%d\n",val);

//

// GetNext(&head, 5, &val);

// printf("%d\n", val);

//

// //GetNext(&head, 5, &val);

// //printf("%d\n", val);

//

// Insert(&head,0,100);

// Insert(&head, 5, 200);

// Insert(&head, 12, 300);

//

// Delete(&head,100);

// Delete(&head, 200);

// Delete(&head, 300);

//

// Show(&head);

//

// Destroy(&head);

// Destroy(&head);

//

// return 0;

//}

//#include "nlist.h"

//int main()

//{

// NLinkList list;

// InitList(&list);

// for (int i = 0; i < 10; i++)

// {

// Insert_tail(&list,i);

// }

// Delete(&list,0);

// Delete(&list, 5);

// Delete(&list, 9);

// Show(list);

// Destroy(&list);

// Destroy(&list);

//

// return 0;

//}

//#include "clist.h"

//

//int main()

//{

// CNode head;//循环链表头节点

// InitList(&head);

// for (int i = 0; i < 10; i++)

// {

// //Insert_head(&head,i);

// Insert_tail(&head,i);

// }

//

// int n;

// GetNext(&head, 3, &n);

// printf("%d\n",n);

// Insert(&head,0,100);

// Delete(&head,100);

// Delete(&head, 9);

// Show(&head);

// Destroy(&head);

// Destroy(&head);

//

// return 0;

//}

//每天一道力扣"链表"题目,截图发到QQ群里,坚持到过年

/*

#include "nlist.h"//不带头节点单链表

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {

struct ListNode head = { 0,NULL };//头节点(局部变量,函数结束自动销毁)

if (list1 == NULL)

return list2;

else if (list2 == NULL)

return list1;

struct ListNode* p1; //指向list1的下一个节点

struct ListNode* p2; //指向list2的下一个节点

struct ListNode* tail = &head;//head链表的尾指针

while (list1 != NULL && list2 != NULL) { //两条链表都还有数据,需要比较

if (list1->val <= list2->val) {//list1小,把它插入到head链表中

p1 = list1->next;

//将list1插在tail后面

list1->next = NULL;

tail->next = list1;

tail = list1;

list1 = p1;

}

else {//list2小,把它插入到head链表中

p2 = list2->next;

//将list2插在tail后面

list2->next = NULL;

tail->next = list2;

tail = list2;

list2 = p2;

}

}

//一个链表已经没有数据,另一个还有数据,直接连接到tail后面

if (list1 != NULL)

tail->next = list1;

if (list2 != NULL)

tail->next = list2;

return head.next;

}

int main()

{

NLinkList pl1;

NLinkList pl2;

InitList(&pl1);

InitList(&pl2);

Insert_tail(&pl1,1);

Insert_tail(&pl1, 2);

Insert_tail(&pl1, 4);

Insert_tail(&pl2, 1);

Insert_tail(&pl2, 3);

Insert_tail(&pl2, 4);

Show(pl1);

Show(pl2);

struct ListNode *pl3 = mergeTwoLists(pl1,pl2);

Show(pl3);

return 0;

}

*/

//int main()

//{

//NLinkList pl;//头指针,4字节

//InitList(&pl);

//

//for (int i = 0; i < 10; i++)

//Insert_tail(&pl,i);//Insert_head(&pl,i);

//

//Show(pl);

//

//return 0;

//}

//void Fun(int** x)

//{

//*x = NULL;

//}

//

//int main()

//{

//int a = 10;

//int* p = &a;

//Fun(&p);

//printf("%d\n",*p);//10

//

//return 0;

//}

/*

#include "list.h"//单链表

int main()

{

LNode head;

InitList(&head);

for (int i = 0; i < 10; i++)

{

//Insert_head(&head,i);

Insert_tail(&head,i);

}

Show(&head);//

int val;

GetElem(&head,3,&val);

printf("%d\n",val);//6

GetPrio(&head, 3, &val);

printf("%d\n", val);//7

GetNext(&head,3,&val);

printf("%d\n", val);//5

Insert(&head,5,100);

Show(&head);

Delete(&head,0);

Delete(&head,5);

Delete(&head,9);

Show(&head);

Destroy(&head);

Destroy(&head);

Show(&head);

return 0;

}

*/

//全局变量:定义在函数外部的变量,生命周期很长,不安全

//静态变量:前面加static ,生命周期很长

//char buf[100];

//把str复制n次

//char* Strcpy_n(const char *str,int n)

//{

//static char buf[100];//错误的

//buf[0] = '\0';

//for (int i = 0; i < n; i++)

//strcat(buf,str);

//return buf;

//}

//int main()

//{

//char *p = Strcpy_n("xyz",2);

//

//char* p2 = Strcpy_n("abc", 2);

//

//printf("%s\n", p);

//printf("%s\n", p2);

//

//return 0;

//}

//int main()

//{

//LNode ln;//头节点

//InitList(&ln);

//for (int i = 0; i < 10; i++)

//Insert_head(&ln,i);

//Show(&ln);

//

LNode* pln;//野指针,错误

InitList(pln);

//

int* p;//野指针,错误

*p = 10;

//

//return 0;

//}

//#include "dsqlist.h"

//

A=A n B

例如:A={1,2,3,5,4};B={5,3,1} ;结果为{5,3,1}

//void Merge(PDSqList pa, PDSqList pb)

//{

//for (int i = 0; i < GetLength(pa);)

//{

//if (Seach(pb, pa->elem[i]) == -1)//pb没有这个元素,需要删除

//{

//Delete(pa, pa->elem[i]);

//}

//else

//i++;

//}

//}

//

A=AUB,并集

例如:A={1,2,3,5,4};B={5,3,1,8} ;AUB={1,2,3,4,5,8};

//void Unio(PDSqList pa, PDSqList pb)

//{

//for (int i = 0; i < GetLength(pb); i++)

//{

//if (Seach(pa, pb->elem[i]) == -1)//不在,添加到pa

//{

//Insert_tail(pa,pb->elem[i]);

//}

//}

//}

//int main()

//{

//DSqList dsa;

//DSqList dsb;

//InitList(&dsa);

//InitList(&dsb);

//

//Insert_tail(&dsa,1);

//Insert_tail(&dsa, 2);

//Insert_tail(&dsa, 3);

//Insert_tail(&dsa, 5);

//Insert_tail(&dsa, 4);

//Show(&dsa);

//

//Insert_tail(&dsb,5);

//Insert_tail(&dsb, 3);

//Insert_tail(&dsb, 1);

//Show(&dsb);

//Merge(&dsa,&dsb);

//printf("两个集合的交集为:\n");

//Show(&dsa);

//

//Destroy(&dsa);

//Destroy(&dsb);

//return 0;

//}

//int main()

//{

//DSqList ds;

//InitList(&ds);

//

//for (int i = 0; i < 21; i++)

//{

//Insert(&ds,i,i);

//}

//Insert_head(&ds,-1);

//Insert_tail(&ds,100);

//Show(&ds);

//Destroy(&ds);

//

//return 0;

//}

//#include "seqlist.h"

//

extern bool IsFull(PSqList);//引用外部符号

//

编译:只需要函数声明

链接:

//int main()

//{

//SqList sq;

//InitList(&sq);

//

//for (int i = 0; i < 20; i++)

//{

//Insert(&sq,i,i);

//}

//Insert_head(&sq,-1);

//Insert_tail(&sq,100);

//Delete(&sq,10);

//Show(&sq);

//

//return 0;

//}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值