//创建一个单链表
struct Node {
int data;//4
struct Node* next;//4
};
struct Node* create_link(struct Node* head)
{
head = (struct Node *)malloc(sizeof(struct Node));//8
head->next = NULL;
struct Node* tail = head;
for (int i = 1; i <= 10; i++)
{
struct Node* temp = head = (struct Node*)malloc(sizeof(struct Node));//分配一块内存空间
temp->data = i;
temp->next = NULL;
tail->next = temp;
tail = temp;
}
return head;
}
void print_link(struct Node* head)
{
//不带头节点
head = head->next;
while (head != NULL) {
printf("%d\n", head->data);
head = head->next;
}
}
int main() {
struct Node *head;
head = create_link(head);
print_link(head);
return 0;
}
```结果显示没初始化head内存
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C4700 使用了未初始化的局部变量“head”
```c
#include<stdio.h>
#include<stdlib.h>
//创建一个单链表
struct Node {
int data;//4
struct Node* next;//4
};
struct Node* create_link(struct Node* head)
{
head = (struct Node *)malloc(sizeof(struct Node));//8
head->next = NULL;
struct Node* tail = head;
for (int i = 1; i <= 10; i++)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));//分配一块内存空间
temp->data = i;
temp->next = NULL;
tail->next = temp;
tail = temp;
}
return head;
}
void print_link(struct Node* head)
{
//不带头节点
head = head->next;
while (head != NULL) {
printf("%d\n", head->data);
head = head->next;
}
}
int main() {
struct Node *head;
head = create_link(head);
print_link(head);
return 0;
}
```
初始化不需要把每一遍都声明成head
这是我写的链表的代码,希望对你有帮助
#include<bits/stdc++.h>
using namespace std;
struct information;
struct node;
typedef node* n;
struct iter;
struct linkl;
typedef linkl jll;
struct information {
int data;
int k;
int init()
{
srand(time(0));
this->k = rand() % 10007 + 1;
this->data = -1;
return this->k;
}
int getifm(int key)
{
if(k == this->k)
return this->data;
else
return 0x7fffffff;
}
int setifm(int key, int v)
{
if(k == this->k)
{
this->data = v;
return v;
}
else
return 0x7fffffff;
}
};
struct node {
information ifm;
n next;
n prev;
int k;
node()
{
this->k = this->ifm.init();
this->next = NULL;
this->prev = NULL;
}
int getifm()
{
return this->ifm.getifm(this->k);
}
int setifm(int i)
{
return this->ifm.setifm(this->k, i);
}
};
struct iter {
n nd;
n get()
{
return this->nd;
}
friend ostream& operator<<(ostream& os, iter self)
{
os << self.get()->getifm();
return os;
}
iter operator++()
{
if(this->nd->next == NULL) return *this;
this->nd = this->nd->next;
return *this;
}
iter operator++(int)
{
if(this->nd->next == NULL) return *this;
iter i = *this;
this->nd = this->nd->next;
return i;
}
iter operator--()
{
if(this->nd->prev == NULL) return *this;
this->nd = this->nd->prev;
return *this;
}
iter operator--(int)
{
if(this->nd->prev == NULL) return *this;
iter i = *this;
this->nd = this->nd->prev;
return i;
}
iter operator+(int l)
{
iter it = *this;
for(int i=1; i<=l; i++) it++;
return it;
}
iter operator-(int l)
{
iter it = *this;
for(int i=1; i<=l; i++) it--;
return it;
}
bool operator==(const iter other)
{
return (this->nd) == (other.nd);
}
bool operator!=(const iter other)
{
return (this->nd) != (other.nd);
}
iter real_head()
{
iter p = *this;
iter r = p--;
while(p.nd != r.nd) p--, r--;
return p;
}
iter real_end()
{
iter p = *this;
iter r = p++;
while(p.nd != r.nd) p++, r++;
iter ready;
ready.nd = p.get();
return ready;
}
iter head()
{
return ++this->real_head();
}
iter end()
{
return --this->real_end();
}
};
struct linkl {
n h;
n e;
int len;
// 初始化,重定位,拷贝操作
int init(int l=0)
{
this->h = new node;
this->e = new node;
n p, r;
p = this->h;
for(int i=1; i<=l; i++)
{
r = new node;
p->next = r;
r->prev = p;
p = r;
}
p->next = this->e;
this->e->prev = p;
this->len = l;
}
int real_index(int x)
{
if(x<0) return this->check_len()+x+1;
else return x;
}
int check_len()
{
n p = this->h;
int t = 0;
while(p->next != this->e)
{
t++;
p = p->next;
}
this->len = t;
return t;
}
linkl copy()
{
linkl result;
n noden = this->h;
result.h = new node;
n nn, pnn=result.h;
while(this->e != noden)
{
noden = noden->next;
n nnn = new node;
nnn->k = noden->k;
nnn->ifm = noden->ifm;
pnn->next = nnn;
nnn->prev = pnn;
pnn = nnn;
}
result.e = pnn;
result.len = this->len;
return result;
}
// 迭代器访问操作
iter get_iter(int o)
{
o = this->real_index(o);
n p = this->h;
for(int i=1; i<=o; i++)
{
p = p->next;
}
iter r;
r.nd = p;
return r;
}
iter iter_real_head()
{
return this->get_iter(0);
}
iter iter_head()
{
return this->get_iter(1);
}
iter iter_end()
{
return this->get_iter(-1);
}
iter iter_real_end()
{
return ++this->get_iter(-1);
}
// IO操作
void print(string sep=" ", string end="\n")
{
n p = this->h;
int i;
for(i=1; p->next != this->e; i++)
{
p = p->next;
cout << p->getifm() << sep;
}
cout << end;
this->len = i-1;
}
void reversed_print(string sep=" ", string end="\n")
{
n p = this->e;
int i;
for(i=1; p->prev != this->h; i++)
{
p = p->prev;
cout << p->getifm() << sep;
}
cout << end;
this->len = i-1;
}
string string_print(string sep=" ", string end="\n")
{
string result;
n p = this->h;
int i;
for(i=1; p->next != this->e; i++)
{
p = p->next;
result += to_string(p->getifm()) + sep;
}
result += end;
this->len = i-1;
return result;
}
void known_read(int t)
{
iter start = this->iter_end();
this->new_node(t);
for(int i=1; i<=t; i++)
{
start++;
int v;
cin >> v;
start.get()->setifm(v);
}
start.get()->next = this->e;
}
int read()
{
this->init(0);
int t;
cin >> t;
this->known_read(t);
return 0;
}
int write()
{
this->print();
return 0;
}
// 数量增删操作
int new_node(int l=1)
{
n p, r;
p = this->e->prev;
for(int i=1; i<=l; i++)
{
r = new node;
p->next = r;
r->prev = p;
p = r;
}
p->next = this->e;
this->e->prev = p;
this->len += l;
}
int del_node(int l=1)
{
n r;
r = this->e;
for(int i=1; i<=l; i++)
{
r = r->prev;
delete r->next;
}
r->next = this->e;
this->e->prev = r;
this->len -= l;
}
int clear_items()
{
this->del_node(this->check_len());
this->len = 0;
}
int self_del_node(int i, int l=1)
{
i = this->real_index(i);
iter it = this->get_iter(i);
--it;
iter now = it+l;
++now;
n begin = it.get(), end = now.get();
begin->next = end;
end->prev = begin;
this->len -= l;
}
int self_new_node(int i, int l=1)
{
i = this->real_index(i);
n f = this->get_iter(i).get();
n r, p=f;
n nextf = f->next;
for(int i=1; i<=l; i++)
{
r = new node;
p->next = r;
r->prev = p;
p = r;
}
p->next = nextf;
nextf->prev = p;
this->len += l;
}
// 数据更改操作
int fill(int i, int from=1, int end=-1)
{
from = this->real_index(from);
end = this->real_index(end);
iter first = this->get_iter(from-1);
for(int j=from; j<=end; j++)
{
++first;
first.get()->setifm(i);
}
}
int swap(int i1, int i2)
{
n n1 = this->get_iter(i1).get();
n n2 = this->get_iter(i2).get();
int tmp;
tmp = n1->getifm();
n1->setifm(n2->getifm());
n2->setifm(tmp);
}
int clear()
{
this->fill(-1);
}
int set(int o, int i)
{
o = this->real_index(o);
n p = this->h;
for(int i=1; i<=o; i++)
{
p = p->next;
}
return p->setifm(i);
}
int get(int o)
{
o = this->real_index(o);
n p = this->h;
for(int i=1; i<=o; i++)
{
p = p->next;
}
return p->getifm();
}
// 链表集体操作
int merge(linkl other, int o=-1)
{
other = other.copy();
n noden = this->get_iter(o).get();
n nnoden = noden->next;
iter noth = other.iter_head();
noden->next = noth.get();
noth.get()->prev = noden;
noth = noth.end();
noth.get()->next = nnoden, nnoden->prev = noth.get();
this->len += other.len;
}
linkl get_part(int from=1, int end=-1)
{
linkl listt = this->copy();
from = listt.real_index(from);
end = listt.real_index(end);
int sub = end-from;
linkl result;
result.init(sub+1);
iter a = listt.get_iter(from);
iter b = result.get_iter(1);
for(int i=1; i<=sub+1; ++i, ++a, ++b)
b.get()->setifm(a.get()->getifm());
result.len = sub+1;
return result;
}
// 链表高级操作
int find(int v)
{
iter n = this->iter_real_head();
iter e = this->iter_real_end();
int i = 0;
while(n.get()->next != e.get())
{
++i, ++n;
if(n.get()->getifm() == v) return i;
}
return -1;
}
int count(int v)
{
int r = 0;
iter n = this->iter_real_head();
iter e = this->iter_real_end();
while(n.get()->next != e.get())
{
++n;
if(n.get()->getifm() == v) r++;
}
return r;
}
int sort(int low=1, int high=-1)
{
low = this->real_index(low);
high = this->real_index(high);
if(low<high)
{
int i = low, j = high;
int x = this->get(low);
while(i<j)
{
while(i<j && this->get(j) > x) j--;
if(i<j) this->set(i++, this->get(j));
while(i<j && this->get(i) < x) i++;
if(i<j) this->set(j--, this->get(i));
}
this->set(i, x);
this->sort(low, i-1);
this->sort(i+1, high);
}
}
};