单链表的一般处理(C语言)

单链表操作,包括创建,插入,删除,排序,逆置
  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<string.h>
  4 #include<conio.h>
  5 using namespace std;
  6 
  7 typedef struct student
  8 {
  9   int data;
 10   struct student *next;
 11 }node;
 12 
 13 node *creat()
 14 {
 15  node *head,*p,*s;
 16  int x,cycle=1;
 17  head=(node*)malloc(sizeof(node));
 18  p=head;
 19  while(cycle)
 20  {
 21    printf("
please input the data:");
 22    scanf("%d",&x);
 23    if(x!=0)
 24    {
 25     s=(node *)malloc(sizeof(node));
 26     s->data=x;
 27     printf("
%d",s->data);
 28     p->next=s;
 29     p=s;
 30     
 31    }
 32    else
 33        cycle=0;
 34  
 35  }
 36 
 37  head=head->next;
 38  p->next=NULL;
 39  printf("
   yyy   %d",head->data);
 40  return(head);
 41 
 42 }
 43 
 44 int length(node *head)
 45 {
 46   int n=0;
 47   node *p;
 48   p=head;
 49   while(p!=NULL)
 50   {
 51       p=p->next;
 52       n++;
 53   }
 54   return(n);
 55 }
 56 
 57 void print(node *head)
 58 {
 59   node *p;
 60   int n;
 61   n=length(head);
 62   printf("
Now,These %d records are:
",n);
 63   p=head;
 64   if(head!=NULL)
 65       while(p!=NULL)
 66       {
 67        printf("
  uuu  %d    ",p->data);
 68        p=p->next;
 69       }
 70 }
 71 
 72 node *del(node *head,int num)
 73 {
 74   node *p1,*p2;
 75   p1=head;
 76   while(num!=p1->data&&p1->next!=NULL)
 77   {p2=p1;p1=p1->next;}
 78 
 79   if(num==p1->data)
 80   {
 81     if(p1==head)
 82     {
 83       head=p1->next;
 84       free(p1);
 85     }
 86     else
 87         p2->next=p1->next;
 88     
 89   }
 90   else
 91       printf("
%d could not been found",num);
 92   return(head);
 93 }
 94 
 95 
 96 node *insert(node *head,int num)
 97 {
 98   node *p0,*p1,*p2;
 99   p1=head;
100   p0=(node *)malloc(sizeof(node));
101   p0->data=num;
102   while(p0->data>p1->data&&p1->next!=NULL)
103   {p2=p1;p1=p1->next;}
104 
105   if(p0->data<=p1->data)
106   {
107     if(head==p1)

108     {
109         p0->next=p1;
110         head=p0;
111     }
112     else
113     {
114         p2->next=p0;
115         p0->next=p1;
116     }
117 
118   }
119   else
120   {
121       p1->next=p0;p0->next=NULL;
122   }
123   return(head);
124 }
125 
126 node *sort(node *head)
127 {
128  node *p,*p2,*p3;
129  int n;int temp;
130  n=length(head);
131  if(head==NULL||head->next==NULL)
132      return head;
133  p=head;
134  for(int j=1;j<n;++j)
135  {
136    p=head;
137    for(int i=0;i<n-j;++i)
138    {
139        if(p->data>p->next->data)
140        {
141            temp=p->data;
142            p->data=p->next->data;
143            p->next->data=temp;
144        }
145        p=p->next;
146    }
147  }
148  return head;
149 }
150 
151 
152 node *reverse(node *head)
153 {
154   node *p1,*p2,*p3;
155   
156   if(head==NULL||head->next==NULL)
157       return head;
158 
159   p1=head,p2=p1->next;
160   while(p2)
161   {
162       p3=p2->next;
163       p2->next=p1;
164       p1=p2;
165       p2=p3;
166   }
167   head->next=NULL;
168   head=p1;
169   return head;
170 }
171 
172 int main()
173 {
174   node *head,stud;
175   int n,del_num,insert_num;
176   head=creat();
177   print(head);
178   cout<<"
Int :";
179   cin>>del_num;
180   head=del(head,del_num);
181   print(head);
182   cout<<"
please input the insert data: ";
183   cin>>insert_num;
184   head=insert(head,insert_num);
185   print(head);
186 
187   return 0;
188 
189 }
}

  

原文地址:https://www.cnblogs.com/mood3604/p/3227943.html