// 链表.cpp : 定义控制台应用程序的入口点。 //
#include "stdafx.h" #include "malloc.h" #define maxSize 10 //单链表节点定义 typedef struct LNode { char data; struct LNode *next; }LNode; //插入数据 在第locate个节点位置插入 void Listinsert(LNode *&L,int locate,int x) { LNode *p,*s; p=L; int j=0; while(p!=NULL&&j<locate-1) { p=p->next; j++; } s=(LNode*)malloc(sizeof(LNode)); s->data=x;
s->next=p->next; p->next=s; } //删除节点 void ListDel(LNode *&L,int locate) { LNode *p,*q; int j=0; p=L; while(p->next!=NULL&&j<locate-1) { p=p->next; j++; } q=p->next; p->next=q->next; free(q);
} void show(LNode *L) { LNode *p=L->next; while(p!=NULL) { printf("%d ",p->data); p=p->next; } } void CreatelistR(LNode *&L,int a[],int n) { LNode *s,*r; L=(LNode*)malloc(sizeof(LNode)); L->next=NULL; r=L; for(int i=0;i<n;i++) { s=(LNode*)malloc(sizeof(LNode)); s->data=a[i]; r->next=s; r=s; } r->next=NULL; } int _tmain(int argc, _TCHAR* argv[]) { LNode *L; int a[]={2,4,5,7,8,20}; CreatelistR(L,a,6); //ListDel(L,1); Listinsert(L,1,-1); Listinsert(L,1,-2); Listinsert(L,1,-3); Listinsert(L,1,-4); Listinsert(L,1,-6); Listinsert(L,1,-7); show(L); }