【数据结构上机练习】4.链表的简单操作(2)
2012-10-30 16:23
513 查看
上机3.1 第三次上机第一题目,比较简单,链表类用了之前定义的。
题目:
输出结果:
注:编译环境为eclipse和codeblocks联合使用
题目:
//============================================================================ // Name : shangji3.1.cpp // Author : menglei // Version : 2012.10.30 // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ /** * 1、设计一个算法,将单链表中元素的次序完全颠倒 */ #include <iostream> #include<stdio.h> #include<stdlib.h> #define len sizeof(struct data) using namespace std; /*定义一个结构体,data */ typedef struct data{ int num; class data *next; }data; /*下面定义链表类*/ class mlink{ private: data *head; // 为链结的数据 public : //mlink(); //无参构造函数 mlink(int n); //有参构造函数,传入参数n void add(int n); //添加 void delHead (void); //删除 int find(int n); //查找 void print(void)const; void addtail(int n); //add elements at tail void transform();//链表的反序************************************ new ******** int findValue(int pos){ //查找指定位置的元素 data *p; p = head; int count=1; if(pos<=0) { cout<<"不存在的位置\n"; return 0; } while(p!=NULL){ if(count == pos) return p->num; p = p->next; count ++; } } }; void mlink::addtail(int n){ //add at tail data *temp = (data*)malloc (len); data *p; temp->num=n; cout<<"【添加】在表尾添加元素:"<<n<<"\n"; if(head->next==NULL){ head->next=temp; }else //头非空 { p=head->next; while (p->next!=NULL){ //find tail element p=p->next; } p->next=temp; temp->next=NULL; } } void mlink::print(void)const{ //print link data *p = head; while(p!=NULL){ printf("%d",p->num); p = p->next; if(p!=NULL) printf("->"); } printf("\n"); } void mlink::add(int n){ //add at head data *temp =(data*) malloc(len); //申请空间 /*在头结点后加入结点*/ if(head==NULL){ printf("链表为空,只有表头\n"); temp->num = n ; //赋值 head->next = temp; temp->next=NULL; } else // 链表非空 { data *p1; printf("【添加】链表非空,在表头添加结点:%d\n",n); temp->num= n; p1=head; head=temp; temp->next=p1; } } void mlink::delHead(void){ //删除 //删除头结点 data *p,*q; p=head->next; q=head; head=p; q->next=NULL; free(q); printf("【删除】头元素删除成功!\n"); } int mlink::find(int n){ //元素n存在于链表中则返回位置 data *p = head; int count = 1; while(p!=NULL){ if(p->num==n) return count; else { p=p->next; count++; } } return 0; } mlink::mlink(int n){ data *first =(data*) malloc(len); //申请空间 //data *head = new data; head=first; //****************important********** cout<<"【创建】创建链表 "<<"头结点为:"<<n<<endl; first->num=n; //把n传入 first->next=NULL; } void mlink::transform(){//**************************************** new ********************* //对链表进行反序操作 data *prep, *nowp , *nextp; prep = NULL; nowp = head; while(nowp->next != NULL){ nextp = nowp ->next; //赋值 nowp ->next = prep; //改值 prep = nowp; //移动 nowp = nextp; //移动 } nowp->next = prep; head = nowp ; cout<<"反序完成!"<<endl; } int main() { cout<<"hehe"<<endl; mlink m(37); m.add(3); m.addtail(5); m.addtail(32); m.addtail(65); m.addtail(34); m.addtail(26); m.print(); cout<<"链表创建完成,下面对链表进行反序操作"<<endl; m.transform(); m.print(); return 0; }
输出结果:
注:编译环境为eclipse和codeblocks联合使用
相关文章推荐
- 【数据结构上机练习】3.栈的简单操作
- 【数据结构上机练习】5.栈的简单操作(2)
- 【数据结构上机练习】6. 二叉树的简单操作(1)
- 【数据结构上机练习】7. 二叉树的简单操作(2)
- 数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-2:单链表操作B
- 对链表的操作(数据结构线性表算法设计练习)
- 数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构链表的简单操作
- SDUT1138数据结构上机测试2-1:单链表操作A
- 数据结构之链表与数组(二) -单向链表上的简单操作问题
- 数据结构上机测试2-2:单链表操作B
- SDUTOJ(1138)数据结构上机测试2-1:单链表操作A
- 数据结构初试 简单链表操作
- 数据结构上机测试2-2:单链表操作B