Problem E: 建立链表(线性表)
生活随笔
收集整理的這篇文章主要介紹了
Problem E: 建立链表(线性表)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Problem E: 建立鏈表(線性表)
Time Limit: 1 Sec??Memory Limit: 128 MBSubmit: 323??Solved: 207
Description
(線性表)設(shè)鍵盤輸入n個(gè)英語單詞,輸入格式為n, w1, w2, …,wn,其中n表示隨后輸入英語單詞個(gè)數(shù),試編一程序,建立一個(gè)單向鏈表,實(shí)現(xiàn):如果單詞重復(fù)出現(xiàn),則只在鏈表上保留一個(gè)。Input
4
now come?now please?
Output
now come?please
Sample Input
3 go come keepSample Output
go come keepHINT
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef char ElemType; typedef struct Node {ElemType data[100];struct Node *next; }SqList;void CreateList(SqList *&L) {L=(SqList *)malloc(sizeof(SqList));L->next=NULL; } void zhuanhuan(SqList *&L) {SqList *p=L->next,*q,*pre,*r;while(p!=NULL){q=p->next;pre=p;while(q!=NULL){if(strcmp(p->data,q->data)==0){r=q;pre->next=q->next;q=q->next;free(r);}else{pre=q;q=q->next;}}p=p->next;} } void print(SqList *L) {SqList *p=L->next;while(p!=NULL){printf("%s ",p->data);p=p->next;} } int main() {SqList *p;int n,i;char a[100][100];scanf("%d",&n);for(i=0;i<n;i++)scanf("%s",a[i]);CreateList(p);SqList *r=p,*s;for(i=0;i<n;i++){s=(SqList *)malloc(sizeof(SqList));strcpy(s->data,a[i]);r->next=s;r=s;}r->next=NULL;zhuanhuan(p);print(p); }總結(jié)
以上是生活随笔為你收集整理的Problem E: 建立链表(线性表)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL与PHP连接
- 下一篇: 转载:35岁前成功的12条黄金法则