数据结构-单链表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using namespace std;
struct LNode{
    char data;
    LNode *next;
};
class LinkList{
    private:
        LNode *head;
    public:
        LNode *createLinkListHead();//头插法创建
        LNode *createLinkListTail(); //尾插法创建
        int length();//链表长
        bool empty();//判空
        void show();//输出整个链表
         
         //增
        int insert(int i,char x);//插入一个元素
        int push_back(char x);//尾插
        int push_front(char x);//头插
         
         //删
        int erase(int i);//删除一个元素
        int clear();//清空?
         
        //改
        int updateNode(int i,char x);
         
        //查
        char get(int i);//按序号查找
        int locate(char x);//按值查找
     
};
 
 
 
//头插法建立单链表,数据读入顺序与链表顺序相反
 
LNode *LinkList::createLinkListHead(){
    char flag='.';
    LNode *s;
    char x;
    cout<<"输入一串字符'.'输入结束。 ";
    cin>>x;  
    while(x!=flag){
        s=new LNode;
        s->data=x;
        s->next=head->next;
        head->next=s;
        cin>>x;
    }
    return head;
}
LNode *LinkList::createLinkListTail(){
    LNode *s,*rear;
    char x;
    cout<<"输入一串字符'.'输入结束。 ";
    cin>>x;
     
    head->next=rear=NULL;
    while(x!='.'){
        s=new LNode;
        s->data=x;
        if(head->next==NULL)
            head->next=s;
        else
            rear->next=s;
        rear=s;
        cin>>x;
    }
    rear->next=NULL;
    return head;
}
int LinkList::length(){
    int i=0;
    LNode *p;
    p=head;
    while(p->next){
        i++;
        p=p->next;
    }
    return i;
}
bool LinkList::empty(){
    if(head->next==NULL){
        return true;
    }
    return false;
}
void LinkList::show(){
    LNode *p=head->next;
    if(p==NULL){
        cout<<"表空";
    }
    while(p!=NULL){
        cout<<p->data;
        p=p->next;
    }
    cout<<endl;
}
 
int LinkList::insert(int i,char x){
    LNode *p=head;
    while(i--){
        p=p->next;
    }
    LNode *s=new LNode;
    s->data=x;
    s->next=p->next;
    p->next=s;
    return 1;
     
}
 
 
int LinkList::push_back(char x){
    LNode *p=head,*s;
    while(p->next){
        p=p->next;
    }  
    s=new LNode;
    s->data=x;
    s->next=NULL;
    p->next=s;
    return 1;
}
int LinkList::push_front(char x){
    LNode *p=new LNode;
    p->data=x;
    p->next=head->next;
    head->next=p;
    return 1;
}
int LinkList::erase(int i){
    LNode *p=head,*s;
    while(i--){
        p=p->next;
    }
     
    s=p->next;
    p->next=s->next;
    delete s;
    return 1;
}
int LinkList::clear(){
    LNode *p;
    p=head;
    while(p->next){
        LNode *s=new LNode;
        s=p->next;
        p->next=s->next;
        delete s;
    }
}
int LinkList::updateNode(int i,char x){
    LNode *p,*s,*t;
    p=head;
    while(i--){
        p=p->next;
    }
    t=p->next;
    s=new LNode;
    s->data=x;
    s->next=t->next;
    p->next=s;
    delete t;
}
char LinkList::get(int i){
    LNode *p;
    p=head->next;
    while(i--){
        p=p->next;
    }
    return p->data;
}
int LinkList::locate(char x){
    int i;
    LNode *p;
    p=head->next;
    while(p->data!=x){
        p=p->next;
        i++;
    }
    return i;
}

  

using namespace std;struct LNode{char data;LNode *next;};class LinkList{private:LNode *head;public:LNode *createLinkListHead();//头插法创建 LNode *createLinkListTail(); //尾插法创建int length();//链表长 bool empty();//判空void show();//输出整个链表  //增 int insert(int i,char x);//插入一个元素 int push_back(char x);//尾插int push_front(char x);//头插 //删 int erase(int i);//删除一个元素int clear();//清空?//改int updateNode(int i,char x); //查 char get(int i);//按序号查找int locate(char x);//按值查找 };


//头插法建立单链表,数据读入顺序与链表顺序相反
LNode *LinkList::createLinkListHead(){char flag='.';LNode *s;char x;cout<<"输入一串字符'.'输入结束。 ";cin>>x; while(x!=flag){s=new LNode;s->data=x;s->next=head->next;head->next=s;cin>>x;}return head;} LNode *LinkList::createLinkListTail(){LNode *s,*rear;char x;cout<<"输入一串字符'.'输入结束。 ";cin>>x;head->next=rear=NULL;while(x!='.'){s=new LNode;s->data=x;if(head->next==NULL)head->next=s;elserear->next=s;rear=s;cin>>x;}rear->next=NULL;return head;}int LinkList::length(){int i=0;LNode *p;p=head;while(p->next){i++;p=p->next;}return i;}bool LinkList::empty(){if(head->next==NULL){return true;}return false;} void LinkList::show(){LNode *p=head->next;if(p==NULL){cout<<"表空"; }while(p!=NULL){cout<<p->data;p=p->next;}cout<<endl;}
int LinkList::insert(int i,char x){LNode *p=head;while(i--){p=p->next;}LNode *s=new LNode;s->data=x;s->next=p->next;p->next=s;return 1;}

int LinkList::push_back(char x){LNode *p=head,*s;while(p->next){p=p->next;}s=new LNode;s->data=x;s->next=NULL; p->next=s;return 1;}int LinkList::push_front(char x){LNode *p=new LNode;p->data=x;p->next=head->next;head->next=p;return 1;}int LinkList::erase(int i){LNode *p=head,*s;while(i--){p=p->next;}s=p->next;p->next=s->next; delete s;return 1;}int LinkList::clear(){LNode *p;p=head;while(p->next){LNode *s=new LNode;s=p->next;p->next=s->next; delete s;}}int LinkList::updateNode(int i,char x){LNode *p,*s,*t;p=head;while(i--){p=p->next;}t=p->next;s=new LNode;s->data=x;s->next=t->next;p->next=s;delete t;}char LinkList::get(int i){LNode *p;p=head->next;while(i--){p=p->next;}return p->data;}int LinkList::locate(char x){int i;LNode *p;p=head->next;while(p->data!=x){p=p->next;i++;}return i;}

posted @   赵钱富贵  阅读(201)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
点击右上角即可分享
微信分享提示