C语言分头结点和没有头结点实现单链表的基本操作,包括初始化 (虽然没用上),头插法建立单链表,尾插法建立单链表,按值查询,按位置查询,按值前插,按位前插,按值后插,按位后插,删除某特定值,删除某位置上的值
有头结点
#include <stdio.h>
#include <malloc.h>
//定义单链表
typedef struct LNode
{
int data;//存储数据
struct LNode* next;//指向下一个节点的指针
}Node,*ChainList;
//头插法建立单链表
ChainList HeadInsert ()
{
ChainList s;//存储读入数据后创建的节点的位置
int x = 0;
//创建头指针
ChainList head = (Node*)malloc(sizeof(struct LNode));
int length = 0;
printf("请输入想创建的链表长度\n");
scanf("%d",&length);
head->data = length;
for(int i = 0;i < length;i++)
{
printf("请输入第%d个位置的值\n",length-i);
scanf("%d",&x);
s = (Node*)malloc(sizeof(struct LNode));
s->data = x;
s->next = head;
head = s;
}
return head;
}
//尾插法创建单链表
ChainList TailCreate()
{
ChainList s;
int x = 0;
ChainList head = (Node*)malloc(sizeof(struct LNode));
int length;
ChainList tail = NULL;
printf("请输入想创建的链表长度\n");
scanf("%d",&length);
if(length < 1)
{
head = NULL;
} else{
head->data = length;
head->next = NULL;
tail = head;
for(int i = 0;i < length;i++)
{
printf("请输入第%d个位置上的值\n",i+1);
scanf("%d",&x);
s = (Node*)malloc(sizeof(struct LNode));
s->data = x;
tail->next = s;
s->next = NULL;
tail = s;
}
}
return head;
}
//打印单链表
void Printf(ChainList p)
{
Node* q = p->next;//不打印头结点,因为头结点中存储了链表长度
if(q == NULL)
printf("这是空的单链表\n");
else{
int i = 0;
while(q != NULL)
{
i++;
printf("链表第%d个位置上的值为%d\n",i,q->data);
q = q->next;
}
}
}
//按位置查找元素
void LocatSearch(ChainList s)
{
Node *q = s->next;
int loc = 0;
printf("请输入想查询的位置\n");
scanf("%d",&loc);
if(loc < 1)
{
printf("输入错误\n");
}else if(loc <= s->data){
Node* p = q;
int i = 1;
while(p != NULL)
{
if(i < loc)
{
p = p->next;
i++;
}if(i == loc)
{
printf("找到了,第%d个位置上的值为%d\n",i,p->data);
break;
}
}
} else if (loc > s->data){
printf("位置输入过大\n");
}
}
//按值查找元素
void ValueSearch(ChainList s)
{
Node *q = s->next;
int x = 0;
printf("请输入想查找的元素值\n");
scanf("%d",&x);
Node* p = q;
int loc = 1;
while(p != NULL)
{
if(p->data == x)
{
printf("找到了,元素值%d在第%d个位置\n",x,loc);
break;
}else
{
p = p->next;
loc++;
}
}
if (p == NULL)
printf("这个值不在链表中\n");
}
//按指定位置前插入
ChainList HeadLocate(ChainList s)
{
Node *q = s->next;
int loc = 0;
printf("请输入想插在哪个位置之前\n");
scanf("%d",&loc);
int length = s->data;
if(loc < 1 || loc > length + 1)
{
printf("输入错误\n");
return NULL;
}
else {
if (loc == length + 1) {
Node *tail = (Node *) malloc(sizeof(struct LNode));
int t = 0;
printf("请输入想插入的值\n");
scanf("%d", &t);
while (q) {
if (q->next == NULL) {
q->next = tail;
tail->next = NULL;
tail->data = t;
break;
}
q = q->next;
}
return s;
} else {
Node *p = (Node *) malloc(sizeof(struct LNode));
int v = 0;
printf("请输入想插入的值\n");
scanf("%d", &v);
int i = 1;
while (q) {
if (i < loc) {
q = q->next;
i++;
} else if (i == loc) {
p->next = q->next;
p->data = q->data;
q->data = v;
q->next = p;
break;
}
}
return s;
}
}
}
//按指定位置后插入
ChainList TailLocate(ChainList s)
{
Node *q = s->next;
int loc = 0;
printf("请输入想插在哪个位置之后\n");
scanf("%d",&loc);
int length = s->data;
if(loc < 1 || loc > length)
{
printf("输入错误\n");
return NULL;
}
else{
Node* p = (Node*)malloc(sizeof(struct LNode));
int v = 0;
printf("请输入想插入的值\n");
scanf("%d",&v);
int i = 1;
while(q)
{
if(i < loc)
{
q = q->next;
i++;
}else if (i == loc)
{
p->next = q->next;
p->data = v;
q->next = p;
break;
}
}
return s;
}
}
//在指定的值前插入
ChainList HeadValue(ChainList s)
{
Node *q = s->next;
int x = 0;
printf("请输入想插在哪个值之前\n");
scanf("%d",&x);
int length = s->data;
int loc = 1;
Node* a = q;
while(a)
{
if(a->data == x)
{
break;
}else{
a = a->next;
loc++;
}
}
if(a == NULL)
{
printf("该值不在链表中\n");
return NULL;
}else{
if (loc == length + 1) {
Node *tail = (Node *) malloc(sizeof(struct LNode));
int t = 0;
printf("请输入想插入的值\n");
scanf("%d", &t);
while (q) {
if (q->next == NULL) {
q->next = tail;
tail->next = NULL;
tail->data = t;
break;
}
q = q->next;
}
return s;
} else {
Node *p = (Node *) malloc(sizeof(struct LNode));
int v = 0;
printf("请输入想插入的值\n");
scanf("%d", &v);
int i = 1;
while (q) {
if (i < loc) {
q = q->next;
i++;
} else if (i == loc) {
p->next = q->next;
p->data = q->data;
q->data = v;
q->next = p;
break;
}
}
return s;
}
}
}
//在指定的值后插入
ChainList TailValue(ChainList s)
{
Node *q = s->next;
int x = 0;
printf("请输入想插在哪个值之后\n");
scanf("%d",&x);
int length = s->data;
int loc = 1;
Node* a = q;
while(a)
{
if(a->data == x)
{
break;
}else{
a = a->next;
loc++;
}
}
if(a == NULL)
{
printf("该值不在链表中\n");
return NULL;
}
else{
Node* p = (Node*)malloc(sizeof(struct LNode));
int v = 0;
printf("请输入想插入的值\n");
scanf("%d",&v);
int i = 1;
while(q)
{
if(i < loc)
{
q = q->next;
i++;
}else if (i == loc)
{
p->next = q->next;
p->data = v;
q->next = p;
break;
}
}
return s;
}
}
//删除指定位置的值.
ChainList LocateDelete(ChainList q)
{
int loce = 0;
printf("请输入想删除的位置\n");
scanf("%d",&loce);
int length = q->data;
Node* d = q;
if(loce < 1 || loce > length)
{
printf("输入有误\n");
return NULL;
}
else{
int i = 0;
int loc = loce + 1;
for(i = 1; i < loc - 1;i++)
{
q = q->next;
}
Node* g = q->next;
q->next = g->next;
return d;
}
}
//删除指定值
ChainList ValueDelete(ChainList q)
{
int x = 0;
printf("请输入想删除的值\n");
scanf("%d",&x);
int loc = 1;
Node* w = q;
while(q != NULL)
{
Node* e = q->next;
if(e->data == x)
{
q->next = e->next;
break;
}
else{
q = q->next;
loc++;
}
}
if(q == NULL)
{
printf("单链表中不存在这个值\n");
return NULL;
}else{
return w;
}
}
int main() {
ChainList head = TailCreate();
Printf(head);
//LocatSearch(head);
//ValueSearch(head);
//ChainList a = HeadLocate(head);
//Printf(a);
//ChainList b = TailLocate(head);
//Printf(b);
//ChainList c = HeadValue(head);
//Printf(c);
//ChainList d = TailValue(head);
//Printf(d);
//ChainList e = LocateDelete(head);
//Printf(e);
ChainList f = ValueDelete(head);
Printf(f);
return 0;
}
无头结点
#include <stdio.h>
#include <stdlib.h>
//定义链表结构体
typedef struct LNode
{
int data;//定义链表中存储数据的地方
struct LNode* next; //定义存储下一个节点位置的地方
}Node,*ChainList;
//初始化(若存在头节点则可在这个地方创建头节点)
void InitialNode(ChainList q)
{
q = NULL;
printf("初始化成功\n");
}
//头插法建立单链表
ChainList HeadInsert ()
{
ChainList s;//存储读入数据后创建的节点的位置
int x = 0;
//创建头指针
ChainList head = NULL;
int length = 0;
printf("请输入想创建的链表长度\n");
scanf("%d",&length);
for(int i = 0;i < length;i++)
{
printf("请输入第%d个位置的值\n",length-i);
scanf("%d",&x);
s = (Node*)malloc(sizeof(struct LNode));
s->data = x;
s->next = head;
head = s;
}
return head;
}
//打印单链表
void Printf(Node* q)
{
if(q == NULL)
printf("这是空的单链表\n");
else{
int i = 0;
while(q != NULL)
{
i++;
printf("链表第%d个位置上的值为%d\n",i,q->data);
q = q->next;
}
}
}
//尾插法建立单链表
ChainList ListInsert()
{
ChainList s;
int x = 0;
ChainList head = (Node*)malloc(sizeof(struct LNode));
int length;
ChainList tail = NULL;
printf("请输入想创建的链表长度\n");
scanf("%d",&length);
if(length < 1)
head = NULL;
for(int i = 0;i < length;)
{
printf("请输入第%d个位置上的值\n",i+1);
scanf("%d",&x);
if(i == 0)
{
head->data = x;
head->next = NULL;
tail = head;
i++;
}else{
s = (Node*)malloc(sizeof(struct LNode));
s->data = x;
tail->next = s;
s->next = NULL;
tail = s;
i++;
}
}
return head;
}
//按位置查找元素
void LocatSearch(ChainList q)
{
int loc = 0;
printf("请输入想查询的位置\n");
scanf("%d",&loc);
if(loc < 1)
{
printf("输入错误\n");
}
else{
Node* p = q;
int i = 1;
while(p != NULL)
{
if(i < loc)
{
p = p->next;
i++;
}if(i == loc)
{
printf("找到了,第%d个位置上的值为%d\n",i,p->data);
break;
}
}
if(p == NULL )
printf("位置输入过大\n");
}
}
//按值查找元素
void ValueSearch(ChainList q)
{
int x = 0;
printf("请输入想查找的元素值\n");
scanf("%d",&x);
Node* p = q;
int loc = 1;
while(p != NULL)
{
if(p->data == x)
{
printf("找到了,元素值%d在第%d个位置\n",x,loc);
break;
}else
{
p = p->next;
loc++;
}
}
if (p == NULL)
printf("这个值不在链表中\n");
}
//求链表长度
int Length(ChainList q)
{
int length = 0;
Node* head = q;
if(head->next == NULL)
{
return 1;
}else{
while(head)
{
head = head->next;
length++; }
return length; }
}
//按指定位置前插入
ChainList HeadLocate(ChainList q)
{
int loc = 0;
printf("请输入想插在哪个位置之前\n");
scanf("%d",&loc);
int length = Length(q);
if(loc < 1 || loc > length + 1)
{
printf("输入错误\n");
return NULL;
}
else{
if (loc == 1){
Node* head = (Node*)malloc(sizeof(struct LNode));
int x = 0;
printf("请输入想插入的值\n");
scanf("%d",&x);
head-> data = x;
head-> next = q;
return head;
}else if(loc == length + 1)
{
Node* tail = (Node*)malloc(sizeof(struct LNode));
int t = 0;
printf("请输入想插入的值\n");
scanf("%d",&t);
Node* n = q;
while(q)
{
if(q->next == NULL)
{
q->next = tail;
tail->next = NULL;
tail->data = t;
break;
}
q = q->next;
}
return n;
}
else{
Node* p = (Node*)malloc(sizeof(struct LNode));
int v = 0;
printf("请输入想插入的值\n");
scanf("%d",&v);
int i = 1;
Node* h = q;
while(q)
{
if(i < loc)
{
q = q->next;
i++;
}else if (i == loc)
{
p->next = q->next;
p->data = q->data;
q->data = v;
q->next = p;
break;
}
}
return h;
}
}
}
/按指定位置后插入
ChainList TailLocate(ChainList q)
{
int loc = 0;
printf("请输入想插在哪个位置之后\n");
scanf("%d",&loc);
int length = Length(q);
if(loc < 1 || loc > length)
{
printf("输入错误\n");
return NULL;
}
else{
if (loc == 1){
Node* p = (Node*)malloc(sizeof(struct LNode));
int x = 0;
printf("请输入想插入的值\n");
scanf("%d",&x);
p-> data = x;
p-> next = q->next;
q-> next = p;
return q;
}else if(loc == length)
{
Node* tail = (Node*)malloc(sizeof(struct LNode));
int t = 0;
printf("请输入想插入的值\n");
scanf("%d",&t);
Node* n = q;
while(q)
{
if(q->next == NULL)
{
q->next = tail;
tail->next = NULL;
tail->data = t;
break;
}
q = q->next;
}
return n;
}
else{
Node* p = (Node*)malloc(sizeof(struct LNode));
int v = 0;
printf("请输入想插入的值\n");
scanf("%d",&v);
int i = 1;
Node* h = q;
while(q)
{
if(i < loc)
{
q = q->next;
i++;
}else if (i == loc)
{
p->next = q->next;
p->data = v;
q->next = p;
break;
}
}
return h;
}
}
}
//在指定的值前插入
ChainList HeadValue(ChainList q)
{
int x = 0;
printf("请输入想插在哪个值之前\n");
scanf("%d",&x);
int length = Length(q);
int loc = 1;
Node* a = q;
while(a)
{
if(a->data == x)
{
break;
}else{
a = a->next;
loc++;
}
}
if(a == NULL)
{
printf("该值不在链表中\n");
return NULL;
}else{
if (loc == 1){
Node* head = (Node*)malloc(sizeof(struct LNode));
int d = 0;
printf("请输入想插入的值\n");
scanf("%d",&d);
head-> data = d;
head-> next = q;
return head;
}else if(loc == length + 1)
{
Node* tail = (Node*)malloc(sizeof(struct LNode));
Node* n = q;
int f = 0;
printf("请输入想插入的值\n");
scanf("%d",&f);
while(q)
{
if(q->next == NULL)
{
q->next = tail;
tail->next = NULL;
tail->data = f;
break;
}
q = q->next;
}
return n;
}
else{
Node *p = (Node*)malloc(sizeof(struct LNode));
int i = 1;
int j = 0;
printf("请输入想插入的值\n");
scanf("%d",&j);
Node* h = q;
while(q)
{
if(i < loc)
{
q = q->next;
i++;
}else if (i == loc)
{
p->next = q->next;
p->data = q->data;
q->data = j;
q->next = p;
break;
}
}
return h;
}
}
}
//在指定的值后插入
ChainList TailValue(ChainList q)
{
int x = 0;
printf("请输入想插在哪个值之后\n");
scanf("%d",&x);
int length = Length(q);
int loc = 1;
Node* a = q;
while(a)
{
if(a->data == x)
{
break;
}else{
a = a->next;
loc++;
}
}
if(a == NULL)
{
printf("该值不在链表中\n");
return NULL;
}
else{
if (loc == 1){
Node* p = (Node*)malloc(sizeof(struct LNode));
int x = 0;
printf("请输入想插入的值\n");
scanf("%d",&x);
p-> data = x;
p-> next = q->next;
q-> next = p;
return q;
}else if(loc == length)
{
Node* tail = (Node*)malloc(sizeof(struct LNode));
int t = 0;
printf("请输入想插入的值\n");
scanf("%d",&t);
Node* n = q;
while(q)
{
if(q->next == NULL)
{
q->next = tail;
tail->next = NULL;
tail->data = t;
break;
}
q = q->next;
}
return n;
}
else{
Node* p = (Node*)malloc(sizeof(struct LNode));
int v = 0;
printf("请输入想插入的值\n");
scanf("%d",&v);
int i = 1;
Node* h = q;
while(q)
{
if(i < loc)
{
q = q->next;
i++;
}else if (i == loc)
{
p->next = q->next;
p->data = v;
q->next = p;
break;
}
}
return h;
}
}
}
/删除指定位置的值.
ChainList LocateDelete(ChainList q)
{
int loc = 0;
printf("请输入想删除的位置\n");
scanf("%d",&loc);
int length = Length(q);
Node* d = q;
if(loc < 1 || loc > length)
{
printf("输入有误\n");
return NULL;
}else if(loc == 1)
{
Node* h = q->next;
return h;
}
else{
int i = 0;
for(i = 1; i < loc - 1;i++)
{
q = q->next;
}
Node* s = q->next;
q->next = s->next;
return d;
}
}
//删除指定值
ChainList ValueDelete(ChainList q)
{
int x = 0;
printf("请输入想删除的值\n");
scanf("%d",&x);
int loc = 1;
Node* w = q;
if(q->data == x)
{
Node* o = q->next;
return o;
}
else{
while(q != NULL)
{
Node* e = q->next;
if(e->data == x)
{
q->next = e->next;
break;
}
else{
q = q->next;
loc++;
}
}
if(q == NULL)
{
printf("单链表中不存在这个值\n");
return NULL;
}else{
return w;
}
}
}
int main()
{
//ChainList q = HeadInsert();
ChainList q = ListInsert();
Printf(q);
LocatSearch(q);
ValueSearch(q);
int length = Length(q);
printf("链表长度为%d\n",length);
ChainList p = HeadLocate(q);
Printf(p);
ChainList t = TailLocate(q);
Printf(t);
ChainList v = HeadValue(q);
Printf(v);
ChainList a = TailValue(q);
Printf(a);
ChainList b = LocateDelete(q);
Printf(b);
ChainList c = ValueDelete(q);
Printf(c);
return 0;
}