当前位置:首页 > 数据结构实验1
}
5:
#include
typedef struct tagDataList {
int CreatDataList(int data) { }
int Up_InsertData(int data) //对于升序序列的插入 {
if(pstFirstNode -> data >= data)
pstCurrentNode -> data = data; pstCurrentNode -> pstNextNode = NULL;
//初始化
DataList *pstCurrentNode = malloc(sizeof(DataList)); //开辟空间 DataList *pstTmpNode = NULL; DataList *pstPreNode = NULL;
//初始化
//初始化
DataList *pstCurrentNode pstCurrentNode -> data if (pstFirstNode == NULL) { }
else { }
pstLastNode = pstCurrentNode; return 0;
//移动末尾指针
pstLastNode -> pstNextNode = pstCurrentNode; //不是空表加入表尾 pstFirstNode = pstCurrentNode;
= malloc(sizeof(DataList)); = data;
//赋值 //赋值
//开辟空间
DataList *pstFirstNode = NULL; DataList *pstLastNode = NULL;
int data;
struct tagDataList *pstNextNode;
//数据链表
}DataList;
pstCurrentNode -> pstNextNode = NULL;
//判断链表是否为空,空就把节点加入头指针
//对于要插入的数跟链表中的头指针指向的数据比较 }
int Down_InsertData(int data) //对于降序序列的插入 {
if(pstLastNode -> data >= data) //跟头指针比较 {
pstCurrentNode -> data = data; pstCurrentNode -> pstNextNode = NULL;
DataList *pstCurrentNode = malloc(sizeof(DataList)); DataList *pstTmpNode = NULL; { }
else if(pstLastNode -> data <= data) { } else
//跟中间数据比较 {
pstPreNode = pstFirstNode;
pstTmpNode = pstPreNode -> pstNextNode; while(pstTmpNode) {
if(pstTmpNode -> data >= data) {
pstLastNode -> pstNextNode = pstCurrentNode; pstLastNode = pstCurrentNode;
pstCurrentNode -> pstNextNode = pstFirstNode; pstFirstNode = pstCurrentNode;
//对于要插入的数跟链表中的尾指针指向的数据比较
//如果要插入的数据大于当前指针指向的数据,上一指针的 //Next指向要插入的指针 } return 0;
}
}
pstPreNode = pstPreNode -> pstNextNode; //往下移动 pstTmpNode = pstTmpNode -> pstNextNode;
pstPreNode -> pstNextNode = pstCurrentNode; pstCurrentNode -> pstNextNode = pstTmpNode; break;
DataList *pstPreNode = NULL;
}
}
pstLastNode -> pstNextNode = pstCurrentNode; pstLastNode = pstCurrentNode;
//跟尾指针比较
else if(pstFirstNode -> data <= data ) { } else { }
pstPreNode = pstFirstNode;
pstCurrentNode -> pstNextNode = pstFirstNode; pstFirstNode = pstCurrentNode;
//中间数据变焦
pstTmpNode = pstPreNode -> pstNextNode; while(pstTmpNode) { }
if(pstTmpNode -> data <= data && pstPreNode -> data >= data) { }
pstPreNode = pstPreNode -> pstNextNode; pstTmpNode = pstTmpNode -> pstNextNode;
pstPreNode -> pstNextNode = pstCurrentNode; pstCurrentNode -> pstNextNode = pstTmpNode; break;
//要插入的数据大于下一指针。小于上一指针
return 0; int main() {
printf(\输入-1退出有序序列的输入\\n\); printf(\请输入有序序列:\\n\); while(1) {
scanf(\,&nSign_Data); if(nSign_Data != -1) //创建链表 { }
CreatDataList(nSign_Data);
int nSign_Data = 0; int nInsertData; int nSelect = 0;
DataList *pstCurrentNode;
}
}
else
{ //当输入-1时,退出链表的创建 }
break;
printf(\请输入要插入的数据 : \); scanf(\, &nInsertData);
nSelect = pstFirstNode -> data - pstLastNode -> data; ///选择升序的插入还是降序的插入 if(nSelect < 0) { } else { }
pstCurrentNode = pstFirstNode; while(pstCurrentNode) { }
printf(\); return 0;
printf(\, pstCurrentNode -> data);
pstCurrentNode = pstCurrentNode -> pstNextNode; Down_InsertData(nInsertData); Up_InsertData(nInsertData);
共分享92篇相关文档