有n个元素,我们把他们用链表表示,执行一些操作在某个位置添加一个数,或者删除某个位置的数;
输入合法的代码:
#include<iostream> #include<stdio.h> #include<stdlib.h> using namespace std; struct node { int date; struct node *next; }; node *CreatList(node *head, int n); void TravList(node *head); node *AddList(node *head, int n, int pos, int num); node *DelList(node *head, int n, int pos); int main() { int n, pos, num; node *head; while(scanf("%d", &n)!=EOF) { head = (node*)malloc(sizeof(node)); head = CreatList(head, n); TravList(head); scanf("%d %d",&pos, &num);///在第pos个位置插入num; head = AddList(head, n+1, pos, num); TravList(head); scanf("%d", &pos);///删除第pos个位置的数; head = DelList(head, n+1, pos); TravList(head); } return 0; } node *CreatList(node *head, int n) { head->next = NULL; for(int i=1; i<=n; i++) { node *p; p = (node *)malloc(sizeof(node)); scanf("%d", &p->date); p->next = head->next; head->next = p; } return head; } void TravList(node *head) { node *real; real = head->next; while(real != NULL) { printf("%d ", real->date); real = real->next; } printf(" "); } node *AddList(node *head, int n, int pos, int num) { node *pre, *p, *q; pre = head; p = pre->next; int i=1; while(i<pos) { pre = p; p = p->next; i++; } q = (node*)malloc(sizeof(node)); q->date = num; q->next = pre->next; pre->next = q; return head; } node *DelList(node *head, int n, int pos) { node *pre, *p; pre = head; p = pre->next; int i=1; while(i<pos) { pre = p; p = p->next; i++; } pre->next = p->next; free(p); return head; } /* 5 1 2 3 4 5 3 10 1 5 4 3 2 1 5 4 10 3 2 1 4 10 3 2 1 */
对所有输入都可以的代码:
#include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 typedef int Status; typedef struct Lnode { int data; struct Lnode *next; } Lnode,*Linklist; int m=sizeof(Lnode); Linklist Creatlist(int n); Linklist CreatlistRear(int n); void Trealist(Linklist L); Status Listdel(Linklist L,int k); Status ListAdd(Linklist L,int num,int x); Linklist Creatlist(int n)///从表的前面插入; { Linklist head,p; int i; head=(Linklist)malloc(m); head->next=NULL; for(i=1;i<=n;i++) { p=(Linklist)malloc(m); scanf("%d",&p->data); p->next=head->next; head->next=p; } return head; } Linklist CreatlistRear(int n)///从表的后面插入; { Linklist rear,p,head; int i; head=(Linklist)malloc(m); head->next=NULL; rear=head; for(i=0;i<n;i++) { p=(Linklist)malloc(m); scanf("%d",&p->data); rear->next=p; rear=p; } rear->next=NULL; return head; } void Trealist(Linklist L)///遍历单链表; { Linklist p; p=L->next; while(p) { printf("%d ",p->data); p=p->next; } printf(" "); } Status Listdel(Linklist L,int k)///删除单链表的元素; { int j=1; Linklist p=L,q=L->next; while(q&&j<k) { p=q; q=q->next; j++; } if((q==0 && j<=k) || k<1 ) return ERROR; p->next=q->next; free(q); return OK; } Status ListAdd(Linklist L,int num,int x)///添加单链表的元素; { Linklist p,q,s; p=L; q=p->next; int j=1; while(q&&j<x) { p=q; q=q->next; j++; } if((q==0&&j<x)||x<1) return ERROR; s=(Linklist)malloc(m); s->data=num; s->next=p->next; p->next=s; return OK; } int main() { int n,k,num,x; while(scanf("%d",&n)!=EOF) { Linklist L; //L=Creatlist(n); L=CreatlistRear(n);//两种建立链表的方法; Trealist(L); printf("请输入要删除的位置 "); scanf("%d",&k); int m=Listdel(L,k); if(m==1) printf("删除成功 "); else printf("删除失败 "); Trealist(L); printf("请输入要插入的数和位置 "); scanf("%d %d",&num,&x); m=ListAdd(L,num,x); if(m==1) printf("插入成功 "); else printf("插入失败 "); Trealist(L); } return 0; }
包括菜单的:
/* Time: 2018/6/4 */ #include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 typedef int Status; typedef struct Lnode { int data; struct Lnode *next; }Lnode, *Linklist; Linklist CreateList(int n);///从表的前面插入; Linklist CreatlistRear(int n);///从表的后面插入; void TreaveList(Linklist head);///遍历单链表; Status Add_Ele(Linklist head, int pos, int num);///添加单链表的元素; Status Del_Ele(Linklist head, int pos);///删除单链表的元素; Status Find_Ele(Linklist head, int pos, int *e); Status Alter_Ele(Linklist head, int pos, int num); int main() { Linklist head; int n, num, pos, op; Status sta; printf("---------------请输入初始链表元素个数:--------------- "); scanf("%d", &n); printf("--------------请输入初始链表的%d个元素:-------------- ", n); head = CreateList(n);//接收函数的返回值,指向新链表的头 printf("---------------------初始链表:----------------------- "); TreaveList(head);//打印单链表 printf("----------输入下面序号对此单链表进行相应操作:-------- "); printf("---------------------1:添加元素---------------------- "); printf("---------------------2:删除元素---------------------- "); printf("---------------------3:查找元素---------------------- "); printf("---------------------4:修改元素---------------------- "); printf("-----------------------0:退出------------------------ "); while(1) { printf("请输入操作序号..."); scanf("%d", &op); if(op == 0) break; else if(op == 1) { printf("请输入需要添加的数和位置(链表下标从1开始,输入的两个数用空格隔开),请输入... "); scanf("%d %d", &num, &pos); sta = Add_Ele(head, pos, num); if(sta == OK) { printf("插入成功 "); printf("插入新元素后的链表为:"); TreaveList(head);//打印单链表 } else printf("插入失败,请检查输入是否合法. "); } else if(op == 2) { printf("请输入需要删除元素的位置(链表下标从1开始),请输入... "); scanf("%d", &pos); sta = Del_Ele(head, pos); if(sta == OK) { printf("删除成功 "); printf("删除后的链表为:"); TreaveList(head);//打印单链表 } else printf("删除失败,请检查输入是否合法. "); } else if(op == 3) { printf("请输入需要查找元素的下标(链表下标从1开始),请输入... "); scanf("%d", &pos); int e; sta = Find_Ele(head, pos, &e); if(sta == OK) { printf("查找成功 "); printf("该位置元素为:%d ", e); } else printf("查找失败,请检查输入是否合法. "); } else if(op == 4) { printf("请输入需要修改的位置和数(链表下标从1开始,输入的两个数用空格隔开),请输入... "); scanf("%d %d", &pos, &num); sta = Alter_Ele(head, pos, num); if(sta == OK) { printf("修改成功 "); printf("修改后的链表为:"); TreaveList(head);//打印单链表 } else printf("修改失败,请检查输入是否合法. "); } printf(" "); } return 0; } Linklist CreateList(int n)//头插法 { int i; Linklist head, s; head = (Linklist)malloc(sizeof(Lnode)); head->next = NULL; for(i=1; i<=n; i++) { s = (Linklist)malloc(sizeof(Lnode)); scanf("%d", &s->data); s->next = NULL; s->next = head->next; head->next = s; } return head; } Linklist CreatlistRear(int n)///从表的后面插入; { Linklist rear,p,head; int i; head = (Linklist)malloc(sizeof(Lnode)); head->next=NULL; rear = head; for(i=0; i<n; i++) { p = (Linklist)malloc(sizeof(Lnode)); scanf("%d", &p->data); p->next = NULL; rear->next = p; rear = p; } return head; } void TreaveList(Linklist head) { int flag = 0; Linklist p = head->next; while(p) { flag = 1; printf("%d ", p->data); p = p->next; } if(flag == 0) printf("链表为空!"); printf(" "); } Status Add_Ele(Linklist head, int pos, int num) { int i=0; Linklist p = head, s; while(p && i<pos-1) { i++; p = p->next; } if( pos < 1 || !p) return ERROR; s = (Linklist)malloc(sizeof(Lnode)); s->data = num; s->next = p->next; p->next = s; return OK; } Status Del_Ele(Linklist head, int pos) { int i=1; Linklist p = head->next, pre = head; while(i<pos && p) { i++; pre = p; p = p->next; } if(pos < 1 || !p) return ERROR; pre->next = p->next; free(p); return OK; } Status Find_Ele(Linklist head, int pos, int *e) { int i = 1; Linklist p = head->next; while(i<pos && p) { i++; p = p->next; } if(!p || pos<1) return ERROR; *e = p->data; return OK; } Status Alter_Ele(Linklist head, int pos, int num) { Linklist p = head->next; int i = 1; while(i<pos && p) { i++; p = p->next; } if(!p || pos<1) return ERROR; p->data = num; return OK; }