Linked list

Singly Linked list

In a singly linked list, we omit the prev pointer in each element
Address of the head node give us access of the complete list

Array VS linked list

Array Linked list
Cost of accessing an element Ο(1) Ο(n) in average case
Memory requirement fixed size no unused memory , extra memory for pointer variables, more flexible
Cost of Inserting element at beginning Ο(n) Ο(1)
Inserting element at the end ① Ο(1) if array isn't full. ② Ο(n) if array is full. Ο(n)
Inserting element at ith element Ο(n) Ο(n)

How to create a linked list

Implement in C :
1.create a node first:

struct Node
{
    int data;
    struct Node* next;   
}* head;            

2.Put your element in the list:

void Put(int x)
{
    struct Node* temp =(struct Node*)malloc(sizeof(struct Node));  //create a node
    temp->data=x;  //put in the data
    temp->next=head;  //make next node as head
    head=temp;  //let head point to the next node
}

3.Traverse the linked list
psudocode first:

node* temp=A
while( temp -> link != NULL)
{
   temp=temp -> link;
   print" temp ->data
}

In C code:

void Display()
{
    struct Node* temp=head;
    printf("Current list is ");
    while(temp !=NULL)  //traverse
    {
        printf("%d",temp->data);
        temp=temp->next;
    }
    printf("\n");
}

4.Free memory
psudocode first:

node = head              
while node != null:     
    temp = node         
    node = node.next    
    free temp          
head = null 

In C code:

void Free(struct Node*head)
{
    struct Node*temp;
    while(head != NULL)  
    {
        temp=head;  
        head=head->next;  
        free(temp);  
    }
    head=NULL;  
}

View complete C code of create linked list here.

Inserting into a linked list

LIST -Insert (L, x)
next [x ]←head [L]
if head [L]≠NIL 
then prev [head [L ]]←x 
head [L]←x 
prev [x]←NIL

Implement in C:

void Insert(int data,int n)
{
    struct Node* temp1=(struct Node*)malloc(sizeof(struct Node));
    temp1->data=data;
    temp1->next=NULL;
    if(n == 1)  
    {
        temp1->next=head;  
        head=temp1;  
        return;
    }
    struct Node* temp2=(struct Node*)malloc(sizeof(struct Node));
    temp2=head;
    int i;
    for(i=0; i<n-2; i++)  
    {
        temp2 = temp2->next;
    }
    temp1->next = temp2->next;
    temp2->next = temp1;
}

View complete C code of insertion here.

Searching a linked list

List _ Search(L,  k)
x ←head[L]
   while x≠NIL and key[x]≠k
   do x←next[x]
return x

It takesΘ(n) time in the worst case.
**Implement in C: **

void Search(int n)
{
    while(head != NULL)
    {
        if(head ->data == n)
        {
            printf("element found\n");
            return;
        }
        head =head->next;
    }
    printf("element don't exist in this list");
}

View complete C code of Search particular element in linked list here.

Delete

Delete at nth position :
void Delete(int n)
{
    struct Node* temp1=head;  
    if(n==1)
    {
        head=temp1->next;   
        free(temp1);  
        return;
    }
    int i;
    for(i=0; i<n-2; i++)
    {
        temp1=temp1->next;  
    }
    struct Node*temp2 = temp1-> next;  
    temp1->next = temp2->next;  
    free(temp2);   
}

**View complete Delete code here. **

Sort linked list

We use merge sort to sort linked list
1.Split your list into two parts


void split(struct node* Node,struct node **front,struct node **back)
{
    struct node* a;
    struct node* b;
    if(Node==NULL || Node->next==NULL)
    {
        *front=Node;
        *back=NULL;
    }
    else
    {
        a=Node;
        b=Node->next;
        while(b != NULL)
        {
            b=b->next;
            if(b!= NULL)
            {
                a=a->next;
                b=b->next;
            }
        }
        *front=Node;
        *back =a->next;
        a->next= NULL;
    }
}
struct node* merge(struct node *a,struct node *b)
{
    struct node* list=NULL;
    if(a==NULL)
    {
        return b;
    }
    else if(b==NULL)
    {
        return a;
    }
    if(a->data > b->data)
    {
        list=b;
        list->next=merge(a,b->next);
    }
    else
    {
        list=a;
        list->next=merge(a->next,b);
    }
    return list;
}
void mergesort(struct node** Node)
{
    head=*Node;
    struct node* a=NULL;
    struct node* b=NULL;

    if(head==NULL || head->next== NULL)
    {
        return;
    }
    split(head,&a,&b);
    mergesort(&a);
    mergesort(&b);
    *Node=merge(a,b);
}

Complete code here

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,527评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,314评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,535评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,006评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,961评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,220评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,664评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,351评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,481评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,397评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,443评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,123评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,713评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,801评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,010评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,494评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,075评论 2 341

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,298评论 0 23
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,716评论 0 33
  • 周五的晚上,高峰期,实在不好打车。她想取消订单的那一秒,系统分配了一辆车,2.6公里开过来,还都是红色的拥堵路段。...
    良仔lz阅读 117评论 0 0
  • 柴静的《看见》是从她刚进央视的时候,历经10年风雨成长,所写出来的一本书。在这本书中我们不光能了解柴静这个人,还能...
    钱莱爱读书阅读 435评论 0 1
  • 艾苏搬着小板凳坐在母亲的身边,母亲正用一把大铁剪在给公鸡放血。公鸡的脖子和翅膀被母亲的大手牢牢地握着,鲜红的血从脖...
    斑斑的四喜丸子阅读 408评论 0 2