//最后附上改进版
1 //============================================================================ 2 // Name : shangji1.cpp 3 // Author : menglei 4 // Version : 5 // Copyright : Your copyright notice 6 // Description : Hello World in C++, Ansi-style 7 //============================================================================ 8 /* 9 1)创建单链表; 10 2)插入操作:分别在当前结点后、表头、表尾插入值为x的结点; 11 3)删除操作:分别删除表头结点、表尾结点和当前结点的后继结点; 12 4)存取操作:分别存取当前结点的值和单链表中第k个结点的值; 13 5)查找操作:查找值为x的元素在单链表中出现的位置(是链表中的第几个元素)。 14 */ 15 #include <iostream> 16 #include<stdio.h> 17 #include<stdlib.h> 18 19 #define len sizeof(struct data) 20 using namespace std; 21 /*定义一个结构体,data */ 22 typedef struct data{ 23 int num; 24 class data *next; 25 }data; 26 27 /*下面定义链表类*/ 28 class mlink{ 29 public : 30 //mlink(); //无参构造函数 31 mlink(int n); //有参构造函数,传入参数n 32 void add(int n); //添加 33 void delHead (void); //删除 34 int find(int n); //查找 35 void print(void)const; 36 void addtail(int n); //add elements at tail 37 38 private: 39 data *head; // 为链结的数据 40 }; 41 void mlink::addtail(int n){ //add at tail 42 data *temp = (data*)malloc (len); 43 data *p; 44 temp->num=n; 45 cout<<"在表尾添加元素:"<<n<<"\n"; 46 if(head==NULL){ 47 head->next=temp; 48 49 50 }else //头非空 51 { 52 p=head->next; 53 while (p->next!=NULL){ //find tail element 54 p=p->next; 55 } 56 p->next=temp; 57 temp->next=NULL; 58 } 59 } 60 void mlink::print(void)const{ //print link 61 data *p = head; 62 while(p!=NULL){ 63 printf("%d",p->num); 64 p = p->next; 65 if(p!=NULL) 66 printf("->"); 67 } 68 printf("\n"); 69 } 70 void mlink::add(int n){ //add at head 71 data *temp =(data*) malloc(len); //申请空间 72 /*在头结点后加入结点*/ 73 if(head==NULL){ 74 printf("链表为空,只有表头\n"); 75 temp->num = n ; //赋值 76 head->next = temp; 77 temp->next=NULL; 78 } 79 else // 链表非空 80 { 81 data *p1; 82 printf("链表非空,在表头添加结点:%d\n",n); 83 temp->num= n; 84 p1=head; 85 head=temp; 86 temp->next=p1; 87 } 88 } 89 void mlink::delHead(void){ //删除 90 //删除头结点 91 data *p,*q; 92 p=head->next; 93 q=head; 94 head=p; 95 q->next=NULL; 96 free(q); 97 printf("头元素删除成功!\n"); 98 } 99 int mlink::find(int n){ //元素n存在于链表中则返回位置 100 data *p = head; 101 int count = 1; 102 while(p!=NULL){ 103 if(p->num==n) 104 return count; 105 else 106 { 107 p=p->next; 108 count++; 109 } 110 } 111 return 0; 112 } 113 mlink::mlink(int n){ 114 data *first =(data*) malloc(len); //申请空间 115 head=first; //****************important********** 116 cout<<"创建链表:\n"; 117 first->num=n; //把n传入 118 first->next=NULL; 119 } 120 121 122 123 //===================main==================== 124 int main() { 125 126 mlink m1(342); //创建链表对象 127 m1.print(); 128 m1.add(32); 129 m1.print(); 130 m1.addtail(56); 131 m1.print(); 132 m1.add(33); 133 m1.print(); 134 int n = m1.find(342); 135 printf("\"342\"元素的位置为%d\n",n); 136 137 m1.delHead(); 138 m1.print(); 139 m1.delHead(); 140 m1.print(); 141 142 143 144 return 0; 145 } 146
运行结果:
改进版:
//============================================================================ // Name : shangji1.cpp // Author : menglei // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ /* 1)创建单链表; 2)插入操作:分别在当前结点后、表头、表尾插入值为x的结点; 3)删除操作:分别删除表头结点、表尾结点和当前结点的后继结点; 4)存取操作:分别存取当前结点的值和单链表中第k个结点的值; 5)查找操作:查找值为x的元素在单链表中出现的位置(是链表中的第几个元素)。 */ #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{ 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 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 ++; } } private: data *head; // 为链结的数据 }; void mlink::addtail(int n){ //add at tail data *temp = (data*)malloc (len); data *p; temp->num=n; cout<<"【添加】在表尾添加元素:"<<n<<"\n"; if(head==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); //申请空间 head=first; //****************important********** cout<<"【创建】创建链表:\n"; first->num=n; //把n传入 first->next=NULL; } //===================main==================== int main() { mlink m1(342); //创建链表对象 m1.print(); m1.add(32);//在表头添加元素 m1.print(); m1.addtail(56);// 在表尾部添加元素 m1.print(); m1.add(33); m1.print(); cout<<"【查找位置】第二个链结的元素为:"<<m1.findValue(2)<<endl; int n = m1.find(342); //查找元素342的位置 printf("【查找元素】\"342\"元素的位置为%d\n",n); m1.delHead();//删除头元素 m1.print(); m1.delHead(); m1.print(); return 0; }
运行结果:
转载文章请注明出处: http://www.cnblogs.com/menglei/