/* *author:zylg project:double link list * fuction introduce * createLinklist() * displayLinklist(linklist* head) * insertLinklist(linklist* head,char ch,int x) * deleteLinklist(linklist* head,int n) *注意:1.線性表的最後一個節點的next由於不存在,因此必須賦值NULL * */ #include<stdlib.h> #include <iostream> #include<string> #include<iomanip> using namespace std; typedef char datatype; typedef struct dnode { datatype data; struct dnode *prior,*next; }dlinklist; void displayLinklist(dlinklist *head) { while(head->next!=NULL) { cout<<head->next->data; head->next=head->next->next; } } dlinklist *createLinklist() { dlinklist *head,*s,*r; head=(dlinklist *)malloc(sizeof(dlinklist)); r=head; datatype ch; ch=cin.get(); while(ch!='\n') { s=(dlinklist*)malloc(sizeof(dlinklist)); s->data=ch; r->next=s; s->prior=r; r=s; ch=cin.get(); } r->next=NULL; return head; } dlinklist *insertLinklist(dlinklist* head,datatype ch,int x) { int i=1;//計數器 dlinklist *s,*temp;//s替head遍歷地址,temp用來插入 s=head->next; s->prior=head; temp=(dlinklist*)malloc(sizeof(dlinklist)); temp->data=ch; while(s!=NULL) { if(x<=0){return NULL;} if(x==1){temp->next=s;temp->prior=head;s->prior=temp;head->next=temp;return head;} if(i==x-1) { temp->next=s->next;temp->prior=s;s->next->prior=temp;s->next=temp;return head; } s=s->next; i++; } return NULL; } dlinklist *deletelinklist(dlinklist *head,int n) { int i=1; dlinklist *s; s=head->next; while(s!=NULL) { if(i==n){s->prior->next=s->next;s->next->prior=s->prior;free(s);return head;} s=s->next; ++i; } return head; } dlinklist *reverselinklist(dlinklist *head) { //若是想取得相反的東西,那能夠從最後讀數據 } int main() { dlinklist *link=createLinklist(); insertLinklist(link,'y',1); deletelinklist(link,1); displayLinklist(link); return 0; }
#include<stdio.h> #include<stdlib.h> typedef char datatype; typedef struct dnode { datatype data; struct dnode *prior,*next; }dlinklist; dlinklist *creatdouble(dlinklist *head) { dlinklist *r, *s; char ch; head = malloc(sizeof(dlinklist)); r = head; ch = getchar(); while (ch != '\n') { s = malloc(sizeof(dlinklist)); s->data = ch; r->next = s; s->prior = r; r = s; ch=getchar(); } r->next = NULL; return head; } datatype get(dlinklist *head,int n) { int i = 1; dlinklist *s; s = head->next; while (s!= NULL&&i<n) { s = s->next; i++; } if (i == n) { return s; } else { return NULL; } } dlinklist* insert(dlinklist *head,int x,datatype ch) { dlinklist *s,*q; int i = 1; s = head->next; s->prior = head; q = malloc(sizeof(dlinklist)); q->data = ch; while (s != NULL&&i <= x) { if (x == i) { q->next = s;q->prior = s->prior;s->prior->next = q;s->prior = q;break; } i++; s = s->next; } if (x == i) { return head; } else { printf("insert error.\n");return NULL; } } dlinklist* delete(dlinklist *head, int x) { dlinklist *s; int i = 1; s = head->next; s->prior = head; while (s != NULL&&i <= x) { if (x == i) { s->prior->next = s->next;s->next->prior = s->prior;free(s);break; } i++;s = s->next; } if (x == i){return head;} else { printf("delete error.\n");return NULL; } } void print(dlinklist *head) { dlinklist *s; s = head->next; while (s != NULL) { printf("%c", s->data); s = s->next; } putchar(10); } void main() { dlinklist q; system("color 0a"); q=*creatdouble(&q); print(&q); //q = *delete(&q,1);print(&q); //q = *insert(&q, 3,'w');print(&q); }