数据结构代码(用C语言) 单链表的插入和删除
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构代码(用C语言) 单链表的插入和删除相关的知识,希望对你有一定的参考价值。
要求单链表的数据域是字符串,且不允许重复的串插入表中;删除操作是根据输入的字符串,先找到相应的节点,然后删除
我就要个单链表的插入和删除,你搞这么复杂,汗!!!
#include <stdio.h>
#include <stdlib.h>
typedef struct node
int nDate;
struct node *pstnext;
Node;
//链表输出
void output(Node *head)
Node *p = head->pstnext;
while(NULL != p)
printf("%d ", p->nDate);
p = p->pstnext;
printf("\r\n");
//链表建立
Node* creat()
Node *head = NULL, *p = NULL, *s = NULL;
int Date = 0, cycle = 1;
head = (Node*)malloc(sizeof(Node));
if(NULL == head)
printf("分配内存失败\r\n");
return NULL;
head->pstnext = NULL;
p = head;
while(cycle)
printf("请输入数据且当输入数据为0时结束输入\r\n");
scanf("%d", &Date);
if(0 != Date)
s = (Node*)malloc(sizeof(Node));
if(NULL == s)
printf("分配内存失败\r\n");
return NULL;
s->nDate = Date;
p->pstnext = s;
p = s;
else
cycle = 0;
p->pstnext = NULL;
return(head);
//单链表测长
void length(Node *head)
Node *p = head->pstnext;
int j=0;
while(NULL != p)
p = p->pstnext;
j++;
printf("%d\r\n", j);
//链表按值查找
void research_Date(Node *head, int date)
Node *p;
int n=1;
p = head->pstnext;
while(NULL != p && date != p->nDate)
p = p->pstnext;
++n;
if(NULL == p)
printf("链表中没有找到该值");
else if(date == p->nDate)
printf("要查找的值%d在链表中第%d个位置\r\n", date, n);
return;
//按序号查找
void research_Number(Node *head, int Num)
Node *p=head;
int i = 0;
while(NULL != p && i < Num)
p = p->pstnext;
i++;
if(p == NULL)
printf("查找位置不合法\r\n");
else if(i == 0)
printf("查找位置为头结点\r\n");
else if(i == Num)
printf("第%d个位置数据为%d\r\n", i, p->nDate);
//在指定元素之前插入新结点
void insert_1(Node *head, int i, int Newdate)
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre && j < i-1)
pre = pre->pstnext;
j++;
if(NULL == pre || j > i-1)
printf("插入位置不存在\r\n");
else
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
printf("分配内存失败\r\n");
return;
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
//在指定元素之后插入新结点
void insert_2(Node *head, int i, int Newdate)
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre->pstnext && j < i)
pre = pre->pstnext;
j++;
if(j == i)
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
printf("分配内存失败\r\n");
return;
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
else
printf("插入位置不存在\r\n");
//删除指定结点
void Delete_1(Node *head, int i3)
Node *p = head, *pre = NULL;
int j = 0;
while(NULL != p && j < i3)
pre = p;
p = p->pstnext;
j++;
if(NULL == p)
printf("删除位置不存在\r\n");
else
pre->pstnext = p->pstnext;
free(p);
//指定删除单链表中某个数据,并统计删除此数据的个数
int Delete_2(Node *head, int Delete_date)
int count = 0;
Node *p = head, *q;
while(NULL != p->pstnext)
q = p->pstnext;
if(q->nDate == Delete_date)
p->pstnext = q->pstnext;
free(q);
++count;
else
p = q;
return count;
//链表逆置
void Reverse_list(Node *head)
Node *q, *s;
if(NULL == head->pstnext || NULL == head->pstnext->pstnext)
return;
q = head->pstnext->pstnext;
head->pstnext->pstnext = NULL;
while(NULL != q)
s = q->pstnext;
q->pstnext = head->pstnext;
head->pstnext = q;
q = s;
//单链表的连接
void connect_list(Node *head, Node *head_New)
Node *p = head;
while(NULL != p->pstnext)
p = p->pstnext;
p->pstnext = head_New->pstnext;
//单链表销毁
void destroy_list(Node* head)
while (NULL != head)
Node* temp = head;
head = head->pstnext;
free(temp);
void main()
int date, num; //待查找数据
int i3; //指定删除元素的位置
int i1, i2, Newdate_1, Newdate_2; //待插入的新数据
int Delete_date, k; //待删除的数据与其个数
Node *Head = NULL; //定义头结点
Node *Head_New = NULL;
//链表建立
Head = creat();
printf("输出建立的单链表\r\n");
output(Head);
//单链表测长
printf("单链表长度为\r\n");
length(Head);
//链表按值查找
printf("请输入待查找的数据\r\n");
scanf("%d", &date);
research_Date(Head, date);
//链表按序号查找
printf("请输入待查找序号\r\n");
scanf("%d", &num);
research_Number(Head, num);
//在指定第i1个元素之前插入新元素Newdate
printf("在指定第i个元素之前插入新元素Newdate");
printf("请输入i与元素且以逗号间隔\r\n");
scanf("%d,%d", &i1, &Newdate_1);
insert_1(Head, i1, Newdate_1);
printf("插入后新链表\r\n");
output(Head);
//在指定第i2个元素之后插入新元素Newdate
printf("在指定第i个元素之后插入新元素Newdate");
printf("请输入i与元素且以逗号间隔\r\n");
scanf("%d,%d", &i2, &Newdate_2);
insert_2(Head, i2, Newdate_2);
printf("插入后新链表\r\n");
output(Head);
//指定删除i3元素
printf("删除元素的位置\r\n");
scanf("%d", &i3);
Delete_1(Head, i3);
printf("删除后新链表\r\n");
output(Head);
//指定删除单链表中某个数据,并统计删除此数据的个数
printf("请输入待删除的元素\r\n");
scanf("%d", &Delete_date);
k = Delete_2(Head, Delete_date);
printf("删除后新链表\r\n");
output(Head);
printf("删除指定元素在链表中的个数为:");
printf("%d\r\n", k);
//单链表逆置
Reverse_list(Head);
printf("逆置后输出\r\n");
output(Head);
//单链表的连接
printf("建立一个新链表\r\n");
Head_New = creat();
printf("输出新链表");
output(Head);
printf("将新链表连接到原来链表的尾部并输出\r\n");
connect_list(Head, Head_New);
output(Head);
destroy_list(Head);
return;
参考技术A 单链表功能大全,嘿嘿
#include <stdio.h>
#include <stdlib.h>
typedef struct node
int nDate;
struct node *pstnext;
Node;
//链表输出
void output(Node *head)
Node *p = head->pstnext;
while(NULL != p)
printf("%d ", p->nDate);
p = p->pstnext;
printf("\r\n");
//链表建立
Node* creat()
Node *head = NULL, *p = NULL, *s = NULL;
int Date = 0, cycle = 1;
head = (Node*)malloc(sizeof(Node));
if(NULL == head)
printf("分配内存失败\r\n");
return NULL;
head->pstnext = NULL;
p = head;
while(cycle)
printf("请输入数据且当输入数据为0时结束输入\r\n");
scanf("%d", &Date);
if(0 != Date)
s = (Node*)malloc(sizeof(Node));
if(NULL == s)
printf("分配内存失败\r\n");
return NULL;
s->nDate = Date;
p->pstnext = s;
p = s;
else
cycle = 0;
p->pstnext = NULL;
return(head);
//单链表测长
void length(Node *head)
Node *p = head->pstnext;
int j=0;
while(NULL != p)
p = p->pstnext;
j++;
printf("%d\r\n", j);
//链表按值查找
void research_Date(Node *head, int date)
Node *p;
int n=1;
p = head->pstnext;
while(NULL != p && date != p->nDate)
p = p->pstnext;
++n;
if(NULL == p)
printf("链表中没有找到该值");
else if(date == p->nDate)
printf("要查找的值%d在链表中第%d个位置\r\n", date, n);
return;
//按序号查找
void research_Number(Node *head, int Num)
Node *p=head;
int i = 0;
while(NULL != p && i < Num)
p = p->pstnext;
i++;
if(p == NULL)
printf("查找位置不合法\r\n");
else if(i == 0)
printf("查找位置为头结点\r\n");
else if(i == Num)
printf("第%d个位置数据为%d\r\n", i, p->nDate);
//在指定元素之前插入新结点
void insert_1(Node *head, int i, int Newdate)
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre && j < i-1)
pre = pre->pstnext;
j++;
if(NULL == pre || j > i-1)
printf("插入位置不存在\r\n");
else
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
printf("分配内存失败\r\n");
return;
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
//在指定元素之后插入新结点
void insert_2(Node *head, int i, int Newdate)
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre->pstnext && j < i)
pre = pre->pstnext;
j++;
if(j == i)
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
printf("分配内存失败\r\n");
return;
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
else
printf("插入位置不存在\r\n");
//删除指定结点
void Delete_1(Node *head, int i3)
Node *p = head, *pre = NULL;
int j = 0;
while(NULL != p && j < i3)
pre = p;
p = p->pstnext;
j++;
if(NULL == p)
printf("删除位置不存在\r\n");
else
pre->pstnext = p->pstnext;
free(p);
//指定删除单链表中某个数据,并统计删除此数据的个数
int Delete_2(Node *head, int Delete_date)
int count = 0;
Node *p = head, *q;
while(NULL != p->pstnext)
q = p->pstnext;
if(q->nDate == Delete_date)
p->pstnext = q->pstnext;
free(q);
++count;
else
p = q;
return count;
//链表逆置
void Reverse_list(Node *head)
Node *q, *s;
if(NULL == head->pstnext || NULL == head->pstnext->pstnext)
return;
q = head->pstnext->pstnext;
head->pstnext->pstnext = NULL;
while(NULL != q)
s = q->pstnext;
q->pstnext = head->pstnext;
head->pstnext = q;
q = s;
//单链表的连接
void connect_list(Node *head, Node *head_New)
Node *p = head;
while(NULL != p->pstnext)
p = p->pstnext;
p->pstnext = head_New->pstnext;
//单链表销毁
void destroy_list(Node* head)
while (NULL != head)
Node* temp = head;
head = head->pstnext;
free(temp);
void main()
int date, num; //待查找数据
int i3; //指定删除元素的位置
int i1, i2, Newdate_1, Newdate_2; //待插入的新数据
int Delete_date, k; //待删除的数据与其个数
Node *Head = NULL; //定义头结点
Node *Head_New = NULL;
//链表建立
Head = creat();
printf("输出建立的单链表\r\n");
output(Head);
//单链表测长
printf("单链表长度为\r\n");
length(Head);
//链表按值查找
printf("请输入待查找的数据\r\n");
scanf("%d", &date);
research_Date(Head, date);
//链表按序号查找
printf("请输入待查找序号\r\n");
scanf("%d", &num);
research_Number(Head, num);
//在指定第i1个元素之前插入新元素Newdate
printf("在指定第i个元素之前插入新元素Newdate");
printf("请输入i与元素且以逗号间隔\r\n");
scanf("%d,%d", &i1, &Newdate_1);
insert_1(Head, i1, Newdate_1);
printf("插入后新链表\r\n");
output(Head);
//在指定第i2个元素之后插入新元素Newdate
printf("在指定第i个元素之后插入新元素Newdate");
printf("请输入i与元素且以逗号间隔\r\n");
scanf("%d,%d", &i2, &Newdate_2);
insert_2(Head, i2, Newdate_2);
printf("插入后新链表\r\n");
output(Head);
//指定删除i3元素
printf("删除元素的位置\r\n");
scanf("%d", &i3);
Delete_1(Head, i3);
printf("删除后新链表\r\n");
output(Head);
//指定删除单链表中某个数据,并统计删除此数据的个数
printf("请输入待删除的元素\r\n");
scanf("%d", &Delete_date);
k = Delete_2(Head, Delete_date);
printf("删除后新链表\r\n");
output(Head);
printf("删除指定元素在链表中的个数为:");
printf("%d\r\n", k);
//单链表逆置
Reverse_list(Head);
printf("逆置后输出\r\n");
output(Head);
//单链表的连接
printf("建立一个新链表\r\n");
Head_New = creat();
printf("输出新链表");
output(Head);
printf("将新链表连接到原来链表的尾部并输出\r\n");
connect_list(Head, Head_New);
output(Head);
destroy_list(Head);
return;
另外,团IDC网上有许多产品团购,便宜有口碑 参考技术B struct A
char a[20];
struct A *next;
a;
struct A *apo; //首节点
int charu(struct A *s) //插入
struct A p = apo->next;
while(p)
if(!strcmp(p->a, s->a))
return 0;
p = p->next;
p = apo->next;
s->next = p->next;
p->next = s;
return 1;
int shanchu(struct *s) //删除
struct A p = apo->next;
struct A m;
m = p;
while(p)
if(!strcmp(p->a, s->a))
if(p == apo->next) //如果是第一个节点
app->next = p->next;
free(p);
else
m->next = p->next;
free(p);
return 0;
else
m = p;
p = p->next;
return 1;
参考技术C 建立一个单链表,实现插入与删除功能的代码如下:
///单链表
#include<iostream>
using namespace std;
typedef int elemtype; //数据类型模版
struct Lnode //结点
elemtype data;
Lnode *next;
;
///建表
void creat_Link(Lnode &head)
Lnode *p,*q;
int n;
p=new Lnode;
head=p;
cout<<"输入链表长度:"<<endl;
cin>>n;
cout<<"输入数据:"<<endl;
cin>>p->data;
q=p;
for(int i=1;i<=n-1;i++)
p=new Lnode;
//cout<<"输入数据:";
cin>>p->data;
q->next=p;
q=p;
q->next=NULL;
///表的输出
void output_Link(Lnode *&head)
if(head==NULL)
cout<<"空链表!"<<endl;
return;
Lnode *q;
q=head;
//cout<<"此链表为:";
while(q!=NULL)
cout<<q->data<<" ";
q=q->next;
cout<<endl;
///表的插入
void insert_Link(Lnode *&head)
int i;
cout<<"输入要插入的位置:";
cin>>i;
Lnode *q,*iq;
q=head;
for(int j=1;j<i;j++)
iq=q;
q=q->next;
cout<<"输入插入的数据:";
Lnode *p;
p=new Lnode;
cin>>p->data;
p->next=iq->next;
iq->next=p;
cout<<endl;
///表的数据删除
void Delete_Link(Lnode *&head)
cout<<"输入删除的位置:";
int i;
cin>>i;
if(i==1)
head=head->next;
else
Lnode *p,*q;
q=head;
for(int j=1;j<i;j++)
p=q;
q=q->next;
p->next=q->next;
delete q;
cout<<endl;
int main()
Lnode *head;
head=NULL;
creat_Link(head);
insert_Link(head);
output_Link(head);
Delete_Link(head);
output_Link(head);
return 0;
[扩展]
以“结点的序列”表示线性表称作线性链表(单链表),链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。
分享
本回答专业性由科学教育分类达人 甄好斌认证
其他类似问题
2011-12-20单链表的建立 插入 删除 查询1
2011-04-25编写一个完整的程序,实现单链表的建立、插入、删除、输出等基本...15
2010-11-14单链表的建立 插入 删除1
2010-04-26用c++处理单链表创建,插入,删除等9
2007-11-291单链表的定义、创建、插入和删除操作,将数据元素显示出来。13
更多关于单链表的插入和删除的问题>>
为您推荐:
2008-10-25 21:30提问者采纳
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct Node)
typedef struct Node
int data;
struct Node *next;
node;
int n;
node *Creat(void)
int data;
node *head;
node * p1=(node*)malloc(LEN);
node * p2=(node*)malloc(LEN);
head=NULL;
n=0;
puts("Please input a num:");
scanf("%d",&p1->data);
while(p1->data!=0)
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(node*)malloc(LEN);
scanf("%d",&p1->data);
free(p1);
p1=NULL;
p2->next=NULL;
return head;
void Print(node *head)
node *p;
p=head;
if(head==NULL)
printf("No Records?!");
return;
else
do
printf("%d-->",p->data);
p=p->next;
while(p!=NULL);
printf("\n");
node* Insert(node *head)
int data;
node *p0=(node*)malloc(LEN),*p1,*p2;
p1=head;
if(head==NULL)
printf("No Records?!");
return NULL;
puts("Please input a num to insert:");
scanf("%d",&p0->data);
while(p1->data<p0->data&&p1->next!=NULL)
p2=p1;
p1=p1->next;
if(p1->data>=p0->data)
if(p1==head)
head=p0;
else
p2->next=p0;
p0->next=p1;
else
printf("Not find!\n");
return head;
node *Delete(node *head)
node *p0,*p1,*p2,*p3;
p0=(node*)malloc(LEN);
if(head==NULL)
printf("No Reacord?!");
return NULL;
puts("Please input a num to delete:");
scanf("%d",&p0->data);
while(head->data==p0->data)
head=head->next;
p1=head;
if(head->next!=NULL)
do
p2=p1;
p1=p1->next;
while(p1!=NULL&&p1->data==p0->data)
p3=p1;
p1=p1->next;
free(p3);
p2->next=p1;
while(p1!=NULL);
return head;
int main()
node *head;
head=Creat();
Print(head);
head=Insert(head);
Print(head);
head=Delete(head);
Print(head);
return 0;
Please input a num:
2 3 4 5 6 0
2-->3-->4-->5-->6-->
Please input a num to insert:
4
2-->3-->4-->4-->5-->6-->
Please input a num to delete:
5
2-->3-->4-->4-->6-->
Press any key to continue
VC6.0
单链表的基本操作操作,类C语言
带头结点的有序单链表的插入、删除、打印等基本操作,要求做文字菜单
#include < iostream >#include < string >
using namespace std;#define NULL 0struct list
string name;
long num;
string sex;
list * next;
;
int n;void main()
list * creat(void);
list * del(list * head,long num);
list * insert(list * head,list * stud);
void print(list * head);
list * head,* list_;
long del_num;
bool quit_del = false;
bool quit_insert = false;
cout << "Menu: " << endl;
cout << "1--创建链表" << endl;
cout << "2--删除结点" << endl;
cout << "3--添加结点" << endl;
cout << "4--察看链表" << endl;
int n = 0;
cin >> n;
if (n != 1)
cout << "没有结点,请创建" << endl << endl;
n = 1;
to:switch(n)
case 1:
cout << endl;
cout << "请创建链表(格式: 序号 姓名 姓别(F or M)) -_-||" << endl;
head = creat();
cout << endl << "请输入 1,2,3,4(Menu),键入其它终止 _";
break;
case 2:
quit_del = false;
cout << endl << " -_-|| 请输入要删除的序号:";
cin >> del_num;
while ( !quit_del )
head = del(head,del_num);
print(head);
cout << "继续? *** y ***** n ***";
char param_del;
cin >> param_del;
if ( param_del == \'y\')
cout << "-_-|| 请输入要删除的序号: ";
cin >> del_num;
else if( param_del != \'n\')
cout << "输入有误,无法判断,返回菜单" << endl;
quit_del = true;
break;
else
quit_del = true;
break;
cout << endl << "请输入 1,2,3,4(Menu),键入其它终止 _";
break;
case 3:
quit_insert = false;
cout << endl << "-_-|| 请输入要添加的结点: " << endl;
list_ = new list;
cin >> list_->num >> list_->name >> list_->sex;
while ( !quit_insert )
head = insert(head,list_);
cout << "继续? *** y ***** n ***";
char param_insert;
cin >> param_insert;
if ( param_insert == \'y\')
cout << endl << "-_-|| 请输入要添加的结点: " << endl;
list_ = new list;
cin >> list_->num >> list_->name >> list_->sex;
else if ( param_insert != \'n\' )
cout << "输入有误,无法添加,返回菜单" << endl;
quit_insert = true;
break;
else
quit_insert = true;
break;
cout << endl << "请输入 1,2,3,4(Menu),键入其它终止 _";
break;
case 4:
print(head);
cout << endl << "请输入 1,2,3,4(Menu),键入其它终止 _";
break;
default:
cout << "呃,又出错误了" << endl;
cin >> n;
if (n == 1)
cout << "结点已被创建,无法重复" << endl;
n = 4;
while ( n == 2 || n == 3 || n == 4)
goto to;
list * creat(void)
bool quit_creat = false;
list * head;
list * p1, * p2;
n = 0;
p1 = p2 = new list;
cin >> p1->num >> p1->name >> p1->sex;
head = NULL;
while ( !quit_creat )
n = n + 1;
if (n == 1)
head = p1;
else p2->next = p1;
p2 = p1;
cout << "继续? *** y ***** n ***";
char param_creat;
cin >> param_creat;
if (param_creat == \'y\')
p1 = new list;
cin >> p1->num >> p1->name >> p1->sex;
else if (param_creat != \'n\')
cout << "输入有误,无法完成继续创建,返回菜单" << endl;
quit_creat = true;
break;
else
quit_creat = true;
break;
p2->next = NULL;
return (head);
list * del(list * head,long num)
list * p1, * p2;
if (head == NULL)
cout << "该链表为空,无法删除-_-||" << endl;
return (head);
p1 = head;
while (num != p1->num && p1->next != NULL)
p2 = p1;
p1 = p1->next;
if (num == p1->num)
if (p1 == head)
head = p1->next;
else
p2->next = p1->next;
delete p1;
cout << "o(∩_∩)o 删除: " << num << endl;
n = n - 1;
else cout << endl << "没有 -_-|| 找到: " << num << endl;
return (head);
list * insert(list * head,list * stud)
list * p0,* p1, * p2;
p1 = head;
p0 = stud;
if (head == NULL)
head = p0;
p0->next = NULL;
else
while ((p0->num > p1->num) && (p1->next != NULL))
p2 = p1;
p1 = p1->next;
if (p0->num <= p1->num)
if (head == p1)
head =p0;
else
p2->next = p0;
p0->next = p1;
else
p1->next = p0;
p0->next = NULL;
n = n + 1;
return (head);
void print(list * head)
list * p;
cout << endl << "这" << n << "条记录顺序如下: " << endl;
p = head;
if (head != NULL)
do
cout << p->num << " " << p->name << " " << p->sex << endl;
p = p->next;
while(p != NULL);
//以前做的一个..应该符合你的要求 参考技术A 数据结构书上有!在网上有电子版的 你找找看
以上是关于数据结构代码(用C语言) 单链表的插入和删除的主要内容,如果未能解决你的问题,请参考以下文章