无头结点单链表
#include typedef char ElemType; typedef struct LNode { ElemType data; struct LNode *next; } LNode, *LinkList; int ListLength(LinkList L); ElemType GetElem(LinkList L,int i); void InsertList(LinkList &L,ElemType x,int i); int DeleteElem(LinkList &L,int i); void DisplayList(LinkList L); LinkList LA; void main() { LA=NULL; ElemType cs; int len=0; InsertList(LA,'a',1); InsertList(LA,'B',2); InsertList(LA,'C',2); cs=GetElem(LA,3); len=ListLength(LA); DisplayList(LA); } int ListLength(LinkList L) { int n=0; struct LNode *q=L; if(L==NULL) cout<<\"链表为空\"< } return n; } ElemType GetElem(LinkList L,int i) { int j=1; struct LNode *q=L; while(jnext; j++; } if(q!=null) return(q->data); else cout<<\"位置参数不正确\"< int n=0; struct LNode *q=L; while(q!=null&&q->data!=x) { q=q->next; n++; } if(q==null) return(-1); else return(n+1); } void InsertList(LinkList &L,ElemType x,int i) { int j=1; struct LNode *s,*q; s=new LNode; s->data=x; q=L; if(i==1) { L=s; s->next=q; } else { while(j int j=1; struct LNode *q=L,*t; if(i==1) { t=q; L=L->next; } else { while(j cout<<\"位置参数不正确!\"< struct LNode *q; q=L; cout<<\"链表元素: \"; if(q==null) cout<<\"链表为空\"< cout< while(q->next!=null) { cout< cout< cout< 带头结点单链表 #include typedef char ElemType; typedef struct LNode { ElemType data; struct LNode*next; }LNode, *LinkList; int ListLength(LinkList L); ElemType GetElem(LinkList L,int i); void InsertList(LinkList &L,ElemType x,int i); int DeleteElem(LinkList &L,int i); void DisplayList(LinkList L); LinkList LA; void main() { LA=(LinkList)malloc(sizeof(LNode)); LA->next=NULL; ElemType cs; int len=0; InsertList(LA,'a',1); InsertList(LA,'B',2); InsertList(LA,'C',2); cs=GetElem(LA,3); len=ListLength(LA); DeleteElem(LA,2); DisplayList(LA); } int ListLength(LinkList L) { int n=0; struct LNode *q=L->next; if(L->next==NULL) cout<<\"链表为空 \"< q=q->next; } return n; } ElemType GetElem(LinkList L, int i) { int j=1; struct LNode *q=L->next; while (j if(q!=null) return (q->data); else cout<<\"位置参数不正确!\"< int n=0; struct LNode *q=L->next; while(q!=null&&q->data!=x) { q=q->next; n++; } if(q==null) return (-1); else return (n+1); } void InsertList(LinkList &L,ElemType x,int i) { int j=1; struct LNode *s,*q; s=new LNode; s->data=x; q=L->next; if(i==1) { L->next=s; s->next=q; } else { while (j j++; } if(j==i-1) { s->data=x; s->next=q->next; q->next=s; } else cout<<\"位置参数不正确!\"< int j=1; struct LNode *q=L->next,*t; if(i==1) { t=L->next; L=L->next; } else { while (j t=q->next; q->next=t->next; } else cout<<\"位置参数不正确!\"< struct LNode *q; q=L->next; cout<<\"链表元素:\"; if(q==null) cout<<\"链表为空\"< while (q->next!=null) { cout< cout< 约瑟夫问题——指向最后一个节点 #include struct node *next; } ListNode; typedef ListNode *LinkList; void main() { LinkList R; int n,k; LinkList initRing(int n,LinkList R); LinkList DeleteDeath(int n,int k,LinkList R); cout<<\"总人数n= ,报数上限k= \" < R=DeleteDeath(n,k,R); } LinkList initRing(int n,LinkList R) { ListNode *p,*q; int i; R=q=new ListNode; for (i=1;i q->data=n; q->next=R; R=p; return R; } LinkList DeleteDeath(int n,int k,LinkList R) { int i,j; ListNode *p,*q; p=R; for(i=1;i<=n;i++) { for(j=1;j<=k-1;j++) p=p->next ; q=p->next ; p->next=q->next ; cout< 约瑟夫问题——指向第一个节点 #include struct node *next; } ListNode; typedef ListNode *LinkList; void main() { LinkList R; int n,k; LinkList initRing(int n,LinkList R); LinkList DeleteDeath(int n,int k,LinkList R); cout<<\"总人数n= ,报数上限k= \" < R=DeleteDeath(n,k,R); } LinkList initRing(int n,LinkList R) { ListNode *p,*q; int i; q=q->next; R=q=new ListNode; for (i=1;i q->data=n; q->next=R; R=p; return R; } LinkList DeleteDeath(int n,int k,LinkList R) { int i,j; ListNode *p,*q; p=R; for(i=1;i<=n;i++) { for(j=1;j<=k-1;j++) p=p->next ; q=p->next ; p->next=q->next ; cout< cout< 因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- awee.cn 版权所有 湘ICP备2023022495号-5
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务