单链表的创建及基本操作

xiaoxiao2021-02-28  44

链表作为数据结构中最容易理解的结构,我们需要对它的基本操作非常熟悉

结点结构:

typedef struct Node { int data; struct Node *next; }Node,*snode;

基本操作:

初始化:

//初始化 snode init() { snode head=(snode)malloc(sizeof(Node)); if(!head) return NULL; head->next=NULL;    return head; }

建立链表:

//头插法建立单链表 snode create1(int n) { snode head,p; head=(snode)malloc(sizeof(Node)); //申请头结点空间 head->next=NULL; //初始化一个空链表 printf("请输入%d个结点的值",n); for(int i=0;i<n;i++) { p=(snode)malloc(sizeof(Node)); scanf("%d",&p->data); p->next=head->next; //将结点插入到表头head-->|2|-->|1|-->NULL head->next=p; } return head; } //尾插法建立单链表 snode create2(int n) { snode head,p1,p2; head=(snode)malloc(sizeof(Node)); //申请头结点空间 head->next=NULL; //初始化一个空链表 p1=head; //p1始终指向终端结点,开始时指向头结点 printf("请输入%d个结点的值:",n); for(int i=0;i<n;i++) { p2=(snode)malloc(sizeof(Node)); scanf("%d",&p2->data); p1->next=p2; p1=p2; //将结点插入到表头head-->|1|-->|2|-->NULL } p1->next=NULL; return head; }

插入结点:

//插入结点到指定位置 snode insert(snode head,int pos,int n) { int j=0; snode pre,s; //pre为前驱结点 pre=head; while(pre&&j<pos-1) { pre=pre->next; //查找第i个位置的前驱结点 j++; } s=(snode)malloc(sizeof(Node)); s->data=n; s->next=pre->next; pre->next=s; return head; }

删除结点:

//删除指定值的所有结点 snode delete1(snode head,int n) { snode p1,p2; p1=head; while(p1->next) { if(p1->next->data==n) { p2=p1->next; p1->next=p1->next->next; free(p2); continue; } p1=p1->next; } return head; }

测试代码:

#include<iostream> #include<cstdio> #include<cstdlib> using namespace std; typedef struct Node { int data; struct Node* next; }node,*snode; //chushihua snode init() { snode head=(snode)malloc(sizeof(Node)); if(!head) return NULL; head->next=NULL; return head; } //建立链表 snode create1(int n) { snode head,a; int j=0; head=(snode)malloc(sizeof(Node)); //创建头结点 head->next=NULL; for(int i=0;i<n;i++) { a=(snode)malloc(sizeof(Node)); printf("请输入第%d个结点的值:",++j); scanf("%d",&a->data); a->next=head->next; head->next=a; //尾插法 } return head; } //头插法 snode create2(int n) { snode head,a,b; int j=0; head=(snode)malloc(sizeof(Node)); //创建头结点 head->next=NULL; a=head; //a始终指向最后一个结点,开始时指向head for(int i=0;i<n;i++) { b=(snode)malloc(sizeof(Node)); printf("请输入第%d个结点的值:",++j); scanf("%d",&b->data); a->next=b; a=b; } a->next=NULL; return head; } //插入结点到指定位置 snode insert(snode head,int pos,int n) { snode pre,s; int j=0; pre=head; while(pre&&j<pos-1) { pre=pre->next; //pre用于保存pos位置结点的前驱结点 j++; } s=(snode)malloc(sizeof(Node)); s->data=n; s->next=pre->next; pre->next=s; return head; } //删除指定值的所有结点 snode delete1(snode head,int n) { snode p1,p2; p1=head; while(p1->next) { if(p1->next->data==n) { p2=p1->next; p1->next=p1->next->next; free(p2); continue; } p1=p1->next; } return head; } int main() { snode head,p; int n,b,c,d,e; printf("输入要创建的结点数:"); scanf("%d",&n); printf("使用尾插法1,头插法2创建:"); scanf("%d",&b); switch(b) { case 1: head=create1(n); break; case 2: head=create2(n); break; default: break; } p=head->next; while(p) { printf("-",p->data); p=p->next; } printf("\n请输入要插入的结点的位置和值:"); scanf("%d%d",&c,&d); head=insert(head,c,d); p=head->next; while(p) { printf("-",p->data); p=p->next; } printf("\n请输入要删除的结点的值:"); scanf("%d",&e); head=delete1(head,e); p=head->next; while(p) { printf("-",p->data); p=p->next; } return 0; }
转载请注明原文地址: https://www.6miu.com/read-2613994.html

最新回复(0)