程序员面试宝典——数据结构之单链表

单链表创建--------->单链表长度--------->单链表删除-------->单链表插入-------->单链表排序--------->单链表逆置

  1 #include<iostream>

  2 #include<stdio.h>

  3 #include<string.h>

  4 #include<stdlib.h>

  5 //#include<conio.h>

  6 using namespace std;

  7 class Node

  8 {

  9 public:

 10     int data;

 11     Node* next;

 12 };

 13 Node* create()

 14 {

 15     Node* head,*p,*s;

 16     int x,cycle=1;

 17     head=(Node*)new Node;

 18     p=head;

 19     while(cycle)

 20     {

 21      printf("\nplease input the data:");

 22      scanf("%d",&x);

 23      if(x!=0){

 24      s=(Node*)new Node;

 25       s->data=x;

 26     // printf("\n%d",s->data);

 27      p->next=s;

 28      p=s;

 29      }

 30      else cycle=0;

 31     }

 32     head=head->next;

 33     p->next=NULL;

 34     printf("\nhead data is: %d\n",head->data);

 35     return (head);

 36 }

 37 

 38 Node*  Delete(Node* head,int num)

 39 {

 40     Node* p1,*p2;

 41     p1=head;

 42     while(num!=p1->data&&p1->next!=NULL)

 43     {

 44         p2=p1;//前一个node

 45         p1=p1->next;

 46     }

 47     if(num==p1->data)

 48     {

 49         if(p1==head)

 50         {

 51             head=p1->next;

 52             delete p1;

 53         }

 54         else{

 55         p2->next=p1->next;

 56         delete p1;

 57         }

 58     }

 59     else

 60     printf("\n%d could not been found ",num);

 61     return (head);

 62 }

 63 

 64 int length(Node *head)

 65 {

 66     int n=0;

 67     Node* p;

 68     p=head;

 69     while(p!=NULL){

 70     p=p->next;

 71     n++;

 72     }

 73     return (n);

 74 }

 75 

 76 void print(Node* head)

 77 {    

 78     Node*p;

 79     p=head;

 80     while(p!=NULL){

 81     printf("%d->",p->data);

 82     p=p->next;

 83     }

 84     printf("\n");

 85 }

 86 

 87 Node* insert(Node* head,int num)

 88 {

 89     Node* p0,*p1,*p2;

 90     p1=head;

 91     p0=(Node*) new Node;

 92     p0->data=num;

 93     while(p0->data>p1->data&&p1->next!=NULL)

 94     {

 95         p2=p1;

 96         p1=p1->next;

 97     }

 98     if(p0->data<=p1->data)

 99     {

100         if(head==p1)

101         {

102             p0->next==p1;

103             head=p0;

104         }

105         else

106         {

107             p2->next=p0;

108             p0->next=p1;

109         }

110     }

111     else

112     {

113         p1->next=p0;

114         p0->next=NULL;

115     }

116     return (head);

117 }

118 

119 Node* sort(Node* head)

120 {

121     Node*p,*p2,*p3;

122     int n;

123     int temp;

124     n=length(head);

125     if(head==NULL||head->next==NULL)

126         return head;

127     p=head;

128     for(int j=1;j<n;j++){

129     p=head;

130     for(int i=0;i<n-j;++i){

131     if(p->data>p->next->data){

132     temp=p->data;

133     p->data=p->next->data;

134     p->next->data=temp;

135     }

136     p=p->next;

137     }

138     }

139     return head;

140 }

141 

142 Node* reverse(Node* head)

143 {

144     Node *p1,*p2,*p3;

145     if(head==NULL||head->next==NULL)

146         return head;

147     p1=head;

148     p2=p1->next;

149     while(p2)

150     {

151         p3=p2->next;

152         p2->next=p1;

153         p1=p2;

154         p2=p3;

155     }

156     

157     head->next=NULL;

158     head=p1;

159     return head;

160 }

161 

162 int main()

163 {

164     Node *head=create();

165     printf("\nlist length is:%d\n",length(head));

166     printf("********list data is*************\n");

167     print(head);

168     head=Delete(head,4);

169     printf("\nafter delete list length is %d\n",length(head));

170     printf("********list data is************\n");

171     print(head);

172     cout<<"************INSERT*****************"<<endl;

173     head=insert(head,888);

174     cout<<"after insert list length is "<<length(head)<<endl;

175     cout<<"**********list data is***********"<<endl;

176     print(head);

177     cout<<"**********SORT****************"<<endl;

178     head=sort(head);

179     print(head);

180     cout<<"**********REVERSE*******************"<<endl;

181     head=reverse(head);

182     print(head);

183     return 0;

184 }

程序员面试宝典——数据结构之单链表

你可能感兴趣的:(数据结构)