Files
Hermes Bot 232ecd7c22 Rebuild blog source from GitHub Pages artifact
- 308 posts reverse-extracted from wahyd4.github.com (Hexo 6.3.0 + NexT 8.25)
- Hexo project with NexT theme, reading-experience custom styles
- publish-post.sh: write post -> hexo build -> PR (master untouched)
2026-08-04 09:44:07 +10:00

144 lines
4.9 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "C++实现顺序链表所有操作代码"
date: 2010-12-23 00:00:00
tags: [代码, c++]
---
今天才开始复习数据结构,而且这本书复习起来很慢啊。但是还是不得不复习。
今天从早上到现在的时候,我终于把顺序单链表的所有操作搞熟悉了,建立,插入,删除,排序,查找,打印,遍历。
现在就附上代码吧,如果觉得有用就拿去吧,代码却对正确,已经在visual studio 2010 调试通过。
C++语言: [临时自用代码](http://fayaa.com/code/view//)
001 #include
002 using namespace std;
003
004 struct list{
005     int Maxsize;
006     int \*student;
007     int length;
008  
009 };
010 void initList(list &stuList,int size)
011 {
012    //这里定义了链表长度
013     stuList.Maxsize = 10;
014     stuList.student=new int[stuList.Maxsize];
015     if(stuList.student==NULL){
016         cout<<"内存不够!"<<endl;
017         exit(1);
018     }else{
019         cout<<"创建成功"<<endl;
020     }
021     stuList.length=size;;
022
023
024 }
025 void createList(list &stuList)
026 {
027     //输入链表
028     cout<<"请输入顺序链表:"<<endl;
029     for(int i=;i<stuList.length;i++){
030         cin>>stuList.student[i];
031     }
032 }
033 void outputList(list &stuList)
034 {
035     //打印链表
036     cout<<"你的链表为:"<<endl;
037     for(int i=;i<stuList.length;i++){
038         cout<<stuList.student[i]<<" ";
039     }
040     cout<<endl;
041 }
042 void sortList(list &stuList)
043 {
044     //为链表排序,排序说明:第一次前两个数比较,第二次第三个数和前两个数比较,以此类推
045     int i,j;
046     int temp;
047     for(int i=1;i<stuList.length;i++){
048         temp=stuList.student[i];
049         for(j=i-1;j>=;j){
050             if(stuList.student[j]>temp){
051                 stuList.student[j+1]=stuList.student[j];
052             }else break;
053         }
054         stuList.student[j+1]=temp;
055     }
056
057 }
058 bool update(list &stuList,int updateTo,int item)
059 {
060     cout<<"你要更改的数为"<<item<<endl;
061       int i;
062       for(i=;i<stuList.length;i++){
063           if(item==stuList.student[i])
064               break;
065          
066       }//获取被更改元素的位置;
067        if(i==stuList.length)
068         return false;//已经将链表全部遍历,仍没找到,则返回false
069       stuList.student[i]=updateTo;
070       return true;
071 }
072 bool insert(list &stuList,int item)
073 {
074     cout<<"你要插入的数为"<<item<<endl;
075     stuList.length++;
076     stuList.student[(stuList.length)-1]=item;//将新增的链表的最后一个元素等于item.
077     return true;
078 }
079 bool deleteNum(list &stuList,int item)
080 {
081     cout<<"你要删除的数为"<<item<<endl;
082       int i;
083       for(i=;i<stuList.length;i++){
084           if(item==stuList.student[i])
085               break;
086         
087       }//获取被删除元素的位置;
088       if(i==stuList.length)
089           return false;//已经将链表全部遍历,仍没找到,则返回false
090       for(int y=i+1;y<stuList.length;y++){
091                 stuList.student[y-1]=stuList.student[y];
092                
093              }//删除元素
094       
095     stuList.length;
096     return true;
097 }
098 int main()
099 {  
100     list jList;
101    
102     initList( jList,5);//5为链表长度
103     createList(jList);
104     if(insert(jList,10)){
105         cout<<"插入成功"<<endl;
106     }else{
107         cout<<"插入失败"<<endl;
108     }
109     sortList(jList);
110     outputList(jList);
111     if(deleteNum( jList,20)){
112         cout<<"删除成功!"<<endl;
113     }else{
114         cout<<"删除失败"<<endl;
115     }
116     if(update( jList,30,12)){
117         cout<<"更改成功!"<<endl;
118     }else{
119         cout<<"更改失败"<<endl;
120     }
121    
122     sortList(jList);
123     outputList(jList);
124
125    
126    
127     system ("pause");
128     return ;
129 }